예제 #1
0
 public void CanSetCache()
 {
     Assert.False(_caching.ContainsKey("test"));
     _caching.SetValue("test", "Some value", TimeSpan.FromDays(1));
     _storage.As <ICacheStorage>().Verify(x => x.SetValue("test", "Some value", TimeSpan.FromDays(1)), Times.Once);
     Assert.True(_caching.ContainsKey("test"));
 }
예제 #2
0
        /// <summary>
        /// Enforce the manager to add/update cache from <see cref="CacheFactory"/>
        /// </summary>
        /// <param name="key"></param>
        /// <param name="factory"></param>
        /// <returns></returns>
        public async Task <CachedData> CacheAsync(string key, CacheFactory factory, bool lockData)
        {
            CachedData data = new CachedData(key);
            await data.Semaphore.WaitAsync();

            CachedData caching = null;

            lock (lockObj)
            {
                if (Caching.ContainsKey(key))
                {
                    caching = Caching[key];
                }
                else
                {
                    data.Lock();
                    Caching.Add(key, data);
                }
            }

            if (caching != null)
            {
                await caching.Semaphore.WaitAsync();

                caching.Semaphore.Release();
                return(caching);
            }

            using (var fs = AllocateTemporaryFile(factory, out var path))
            {
                await factory.Factory(fs);

                data.Path = path;
                data.Size = fs.Length;

                TotalSize += fs.Length;
                TotalCount++;
            }

            CachedData last = null;

            lock (lockObj)
            {
                Caching.Remove(key);

                if (Caches.ContainsKey(key))
                {
                    last = Caches[key];
                }

                data.Disposed += DataOnDisposed;
                Caches[key]    = data;
            }

            last?.Dispose();

            CheckCaches();

            if (!lockData)
            {
                data.Unlock();
            }
            data.Semaphore.Release();

            Cached?.Invoke(this, new CachedEventArgs(data));

            return(data);
        }