public async Task Appand(string key, T obj) { var cachedItems = await _cache.Get<List<T>>(key); if (cachedItems == null) cachedItems = new List<T>(); else if (cachedItems.Any(x => x.Id == obj.Id)) { var item = cachedItems.First(x => x.Id == obj.Id); cachedItems.Remove(item); } cachedItems.Add(obj); await _cache.Remove(key); await _cache.Set(key, cachedItems, null); }
public override bool Lock(string serviceKey, int expiryFromSeconds) { bool isLocked = false; try { string isAnyLocked = _redisStore.Get(serviceKey); if (String.IsNullOrEmpty(isAnyLocked)) { isLocked = _redisStore.Set(serviceKey, _token, expiryFromSeconds); // When occurs any network problem within Redis, it could be provided consistent locking with secondary handler. if (SecondaryLockHandler != null) { Task.Factory.StartNew(() => SecondaryLockHandler.Lock(serviceKey, expiryFromSeconds)); } } } catch { if (SecondaryLockHandler != null) { isLocked = SecondaryLockHandler.Lock(serviceKey, expiryFromSeconds); } } return(isLocked); }
public T Get <T>(string key, int cacheSeconds = 60) { Check.NotNullOrEmpty(key, nameof(key)); var data = _memoryStore.Get <T>(key); if (!Equals(data, default)) { return(data); } data = _redisStore.Get <T>(key); if (!Equals(data, default)) { UpdateAndPublish(key, data, cacheSeconds); } return(data); }
public async Task <T> Get(Repositories.IBaseRepository <T> _templates, string key) { string[] keys = key.Split('_'); var template = await _redisStore.Get <T>(key); if (template == null) { var item = await _templates.Get(keys[0]); if (item.Version != keys[1] && !string.IsNullOrWhiteSpace(keys[1])) { item.TemplateBody = item.Versions.FirstOrDefault(x => x.VersionNumber == keys[1]).TemplateBody; } return(item); } return(template); }