Creates a sharded revision cache with the given capacity and an optional loader function.
(revCacheOptions *RevisionCacheOptions, backingStores map[uint32]RevisionCacheBackingStore, cacheHitStat, cacheMissStat, cacheNumItemsStat, cacheMemoryStat *base.SgwIntStat)
| 28 | |
| 29 | // Creates a sharded revision cache with the given capacity and an optional loader function. |
| 30 | func NewShardedLRURevisionCache(revCacheOptions *RevisionCacheOptions, backingStores map[uint32]RevisionCacheBackingStore, cacheHitStat, cacheMissStat, cacheNumItemsStat, cacheMemoryStat *base.SgwIntStat) *ShardedLRURevisionCache { |
| 31 | |
| 32 | caches := make([]*LRURevisionCache, revCacheOptions.ShardCount) |
| 33 | // Add 10% to per-shared cache capacity to ensure overall capacity is reached under non-ideal shard hashing |
| 34 | perCacheCapacity := 1.1 * float32(revCacheOptions.MaxItemCount) / float32(revCacheOptions.ShardCount) |
| 35 | revCacheOptions.MaxItemCount = uint32(perCacheCapacity) |
| 36 | var perCacheMemoryCapacity float32 |
| 37 | if revCacheOptions.MaxBytes > 0 { |
| 38 | perCacheMemoryCapacity = float32(revCacheOptions.MaxBytes) / float32(revCacheOptions.ShardCount) |
| 39 | revCacheOptions.MaxBytes = int64(perCacheMemoryCapacity) |
| 40 | } |
| 41 | |
| 42 | for i := 0; i < int(revCacheOptions.ShardCount); i++ { |
| 43 | caches[i] = NewLRURevisionCache(revCacheOptions, backingStores, cacheHitStat, cacheMissStat, cacheNumItemsStat, cacheMemoryStat) |
| 44 | } |
| 45 | |
| 46 | return &ShardedLRURevisionCache{ |
| 47 | caches: caches, |
| 48 | numShards: revCacheOptions.ShardCount, |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | func (sc *ShardedLRURevisionCache) getShard(docID string) *LRURevisionCache { |
| 53 | return sc.caches[sgbucket.VBHash(docID, sc.numShards)] |