|
| 1 | +# BigCache Factory |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +The BigCache Factory provides integration with the [BigCache](https://github.com/allegro/bigcache) library, a fast, concurrent, evicting in-memory cache designed for high-load applications. This factory enables efficient caching capabilities within the Boost framework, optimized for applications that require high performance with minimal GC overhead. |
| 6 | + |
| 7 | +## Features |
| 8 | + |
| 9 | +- **In-Memory Caching**: Fast access to cached data with minimal latency |
| 10 | +- **Configurable Eviction**: Time-based eviction policies with customizable life windows |
| 11 | +- **Sharded Architecture**: Concurrent access with minimal lock contention |
| 12 | +- **Memory Management**: Hard limits on cache size to prevent memory issues |
| 13 | +- **Metrics & Statistics**: Optional statistics collection for monitoring |
| 14 | +- **Logging Integration**: Seamless integration with Boost's logging system |
| 15 | + |
| 16 | +## Usage |
| 17 | + |
| 18 | +### Basic Usage |
| 19 | + |
| 20 | +```go |
| 21 | +package main |
| 22 | + |
| 23 | +import ( |
| 24 | + "context" |
| 25 | + "github.com/xgodev/boost" |
| 26 | + "github.com/xgodev/boost/factory/contrib/allegro/bigcache/v3" |
| 27 | + "github.com/xgodev/boost/wrapper/log" |
| 28 | +) |
| 29 | + |
| 30 | +func main() { |
| 31 | + // Initialize Boost |
| 32 | + boost.Start() |
| 33 | + |
| 34 | + // Create a context with logger |
| 35 | + ctx := log.WithLogger(context.Background(), log.GetLogger()) |
| 36 | + |
| 37 | + // Create a new cache with default settings |
| 38 | + cache, err := bigcache.NewCache(ctx) |
| 39 | + if err != nil { |
| 40 | + log.Errorf("Failed to create cache: %v", err) |
| 41 | + return |
| 42 | + } |
| 43 | + |
| 44 | + // Use the cache |
| 45 | + cache.Set("key", []byte("value")) |
| 46 | + entry, err := cache.Get("key") |
| 47 | + if err != nil { |
| 48 | + log.Errorf("Failed to get entry: %v", err) |
| 49 | + } |
| 50 | + |
| 51 | + log.Infof("Retrieved value: %s", string(entry)) |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +### Custom Configuration |
| 56 | + |
| 57 | +You can customize BigCache parameters through Boost's configuration system: |
| 58 | + |
| 59 | +```go |
| 60 | +// Create a cache with custom configuration path |
| 61 | +cache, err := bigcache.NewCacheWithConfigPath(ctx, "myapp.cache") |
| 62 | +if err != nil { |
| 63 | + log.Errorf("Failed to create cache: %v", err) |
| 64 | + return |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +Or by directly providing options: |
| 69 | + |
| 70 | +```go |
| 71 | +// Create options and modify them |
| 72 | +options, err := bigcache.NewOptions() |
| 73 | +if err != nil { |
| 74 | + log.Errorf("Failed to create options: %v", err) |
| 75 | + return |
| 76 | +} |
| 77 | + |
| 78 | +// Customize options |
| 79 | +options.Shards = 2048 |
| 80 | +options.LifeWindow = 10 * time.Minute |
| 81 | +options.HardMaxCacheSize = 512 // MB |
| 82 | + |
| 83 | +// Create cache with custom options |
| 84 | +cache, err := bigcache.NewCacheWithOptions(ctx, options) |
| 85 | +if err != nil { |
| 86 | + log.Errorf("Failed to create cache: %v", err) |
| 87 | + return |
| 88 | +} |
| 89 | +``` |
| 90 | + |
| 91 | +## Configuration Parameters |
| 92 | + |
| 93 | +| Parameter | Description | Default | |
| 94 | +|-----------|-------------|---------| |
| 95 | +| `shards` | Number of cache shards (must be a power of two) | 1024 | |
| 96 | +| `lifeWindow` | Time after which entry can be evicted | 5 minutes | |
| 97 | +| `cleanWindow` | Interval between removing expired entries | 0 (disabled) | |
| 98 | +| `maxEntriesInWindow` | Max number of entries in life window | 600,000 | |
| 99 | +| `maxEntrySize` | Max size of entry in bytes | 1 MB | |
| 100 | +| `verbose` | Verbose mode for memory allocation information | false | |
| 101 | +| `hardMaxCacheSize` | Limit for cache size in MB (0 = unlimited) | 1 | |
| 102 | +| `statsEnabled` | Enable statistics collection | false | |
| 103 | + |
| 104 | +## Integration with Other Boost Components |
| 105 | + |
| 106 | +The BigCache Factory integrates with: |
| 107 | + |
| 108 | +- **Config Wrapper**: For loading and managing configuration |
| 109 | +- **Log Wrapper**: For logging cache events and errors |
| 110 | +- **Context**: For propagating logger and other values |
| 111 | + |
| 112 | +## Best Practices |
| 113 | + |
| 114 | +1. **Shard Count**: Set the number of shards based on your expected concurrency level. Higher concurrency requires more shards to reduce lock contention. |
| 115 | + |
| 116 | +2. **Life Window**: Choose an appropriate life window based on your data freshness requirements. Shorter windows reduce memory usage but may cause more cache misses. |
| 117 | + |
| 118 | +3. **Entry Size**: Set the max entry size based on your typical cached objects. Setting it too high wastes memory, while setting it too low causes errors when storing larger objects. |
| 119 | + |
| 120 | +4. **Memory Management**: Use `hardMaxCacheSize` to prevent the cache from consuming too much memory, especially in memory-constrained environments. |
| 121 | + |
| 122 | +5. **Clean Window**: Enable periodic cleanup (by setting a positive `cleanWindow` value) for long-running applications to ensure memory is reclaimed. |
| 123 | + |
| 124 | +## Performance Considerations |
| 125 | + |
| 126 | +- BigCache is optimized for high-load scenarios with minimal GC overhead |
| 127 | +- The cache performs best with a large number of small entries |
| 128 | +- For very large objects, consider using a different caching solution |
| 129 | +- Monitor cache statistics in production to fine-tune parameters |
| 130 | + |
| 131 | +## References |
| 132 | + |
| 133 | +- [BigCache GitHub Repository](https://github.com/allegro/bigcache) |
| 134 | +- [Boost Framework Documentation](https://github.com/xgodev/boost) |
0 commit comments