private void Save <TResult>(CacheParams par, TResult data) { bool exists = true; if (!localCache.ContainsKey(par.Key)) { lock (cacheLock) if (!localCache.ContainsKey(par.Key)) { exists = false; CachedObject <TResult> item = new CachedObject <TResult> { Data = data, Tags = par.Tags, ExpirationDate = DateTime.UtcNow.AddMinutes(par.Duration), }; localCache.Add(par.Key, item); } } if (exists) { CachedObject <TResult> item = localCache[par.Key] as CachedObject <TResult>; lock (item.Lock) { if (item.Data is IDisposable disposable) { disposable.Dispose(); } item.Data = data; item.Tags = par.Tags; item.ExpirationDate = DateTime.UtcNow.AddMinutes(par.Duration); } } }
public void Set <TService>( CachedServiceBase <TService> cached, CacheParams par, Action <CachedServiceBase <TService> > setData) where TService : class { setData(cached); Invalidate(par); }
public async Task SetAsync <TService>( CachedServiceBase <TService> cached, CacheParams par, Func <CachedServiceBase <TService>, Task> setData) where TService : class { await setData(cached); Invalidate(par); }
public void Set <TService, TResult>( CachedServiceBase <TService> cached, CacheParams par, TResult value, Action <CachedServiceBase <TService>, TResult> setData) where TService : class { setData(cached, value); Save(par, value); }
public async Task SetAsync <TService, TResult>( CachedServiceBase <TService> cached, CacheParams par, TResult value, Func <CachedServiceBase <TService>, TResult, Task> setData) where TService : class { await setData(cached, value); Save(par, value); }
private void Invalidate(CacheParams par) { if (localCache.ContainsKey(par.Key)) { lock (cacheLock) if (localCache.ContainsKey(par.Key)) { CachedObject item = localCache[par.Key] as CachedObject; if (item is IDisposable disposable) { disposable.Dispose(); } localCache.Remove(par.Key); } } }
public void Set <TService>( CachedServiceBase <TService> cached, CacheParams par, Action <CachedServiceBase <TService> > setData) where TService : class => memoryCache.Set(cached, par, service => { setData(service); if (this is IRemoteCache remote0) { remote0.Invalidate(par); } else if (this is IRemoteCacheAsync remoteAsync0) { Task task = remoteAsync0.InvalidateAsync(par); task.Wait(); } });
public void Set <TService, TResult>( CachedServiceBase <TService> cached, CacheParams par, TResult value, Action <CachedServiceBase <TService>, TResult> setData) where TService : class => memoryCache.Set(cached, par, value, (service, value) => { setData(service, value); if (this is IRemoteCache remote0) { remote0.Save(par, value); } else if (this is IRemoteCacheAsync remoteAsync0) { Task task = remoteAsync0.SaveAsync(par, value); task.Wait(); } });
public TResult Get <TService, TResult>( CachedServiceBase <TService> cached, CacheParams par, Func <CachedServiceBase <TService>, TResult> getData) where TService : class => memoryCache.Get(cached, par, service => { TResult data = default; if (this is IRemoteCache remote) { if (remote.Contains(par.Key)) { data = remote.Load <TResult>(par); return(data); } } else if (this is IRemoteCacheAsync remoteAsync) { if (remoteAsync.Contains(par.Key)) { Task <TResult> task = remoteAsync.LoadAsync <TResult>(par); task.Wait(); data = task.Result; return(data); } } else { throw new InvalidOperationException($"Manager {GetType().FullName} has is not implemented."); } data = getData(cached); if (this is IRemoteCache remote0) { remote0.Save(par, data); } else if (this is IRemoteCacheAsync remoteAsync0) { Task task = remoteAsync0.SaveAsync(par, data); task.Wait(); } return(data); });
public async Task <TResult> GetAsync <TService, TResult>( CachedServiceBase <TService> cached, CacheParams par, Func <CachedServiceBase <TService>, Task <TResult> > getData) where TService : class { if (localCache.ContainsKey(par.Key)) { lock (cacheLock) if (localCache.ContainsKey(par.Key)) { CachedObject item = localCache[par.Key] as CachedObject; if (!item.IsExpired) { return((TResult)item.Data); } } } TResult data = await getData(cached); Save(par, data); return(data); }
public async Task <TResult> GetAsync <TService, TResult>( CachedServiceBase <TService> cached, CacheParams par, Func <CachedServiceBase <TService>, Task <TResult> > getData) where TService : class => await memoryCache.GetAsync(cached, par, async service =>