Example #1
0
        public async Task <TItem> GetOrCreate(MultiKey key, Func <BitArray, int, TItem> createItem)
        {
            var hash = key.GetHashCode();

            if (!_cache.TryGetValue(hash, out TItem cacheEntry))
            {
                var itemLock = _locks.GetOrAdd(hash, k => new SemaphoreSlim(1, 1));
                await itemLock.WaitAsync();

                try
                {
                    if (!_cache.TryGetValue(hash, out cacheEntry))
                    {
                        cacheEntry = createItem(key.Key1, key.Key2);
                        _cache.Set(hash, cacheEntry, entryOptions);
                    }
                }
                finally
                {
                    itemLock.Release();
                }
            }

            return(cacheEntry);
        }
Example #2
0
 public bool Equals(MultiKey other)
 {
     return(GetHashCode().Equals(other.GetHashCode()));
 }