public string updateReplyCacheKey(UPDATE_TYPE type, oCacheModel cacheModel, string valKey, string jsonObject) { string key = Guid.NewGuid().ToString(); oCacheResult rs = Update(type, cacheModel, valKey, jsonObject); ObjectCache cache = MemoryCache.Default; cache.Set(key, rs, new CacheItemPolicy()); return(key); }
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public string updateReplyCacheKey(UPDATE_TYPE type, string valKey, string jsonObject) { return(_store.updateReplyCacheKey(type, _cacheModel, valKey, jsonObject)); }
public bool update(UPDATE_TYPE type, string valKey, string jsonObject) { return(_store.Update(type, _cacheFields, valKey, jsonObject)); }
public oCacheResult Update(UPDATE_TYPE type, oCacheModel cacheModel, string valKey, string jsonObject) { oCacheRequest request = new oCacheRequest(cacheModel.ServiceName, jsonObject) { RequestId = valKey }; try { bool isOk = false; oCacheField fkey = new oCacheField() { }; string fielKey = string.Empty, typeKey = string.Empty; if (type != UPDATE_TYPE.ADD) { if (cacheModel == null || cacheModel.Fields.Length == 0) { return(new oCacheResult(request).ToFailInputNULL("The CacheModel with Fields for EDIT|REMOVE is NULL or empty")); } fkey = cacheModel.Fields.Where(x => x.iskey).SingleOrDefault(); if (fkey == null) { return(new oCacheResult(request).ToFailInputNULL("The CacheModel with Fields for EDIT|REMOVE is NULL or empty")); } else { fielKey = fkey.name; typeKey = fkey.type; } } if (string.IsNullOrWhiteSpace(jsonObject)) { return(new oCacheResult(request).ToFailInputNULL("The Json of item for update is NULL or empty")); } T item = default(T); try { item = JsonConvert.DeserializeObject <T>(jsonObject); } catch (Exception ex1) { return(new oCacheResult(request).ToFailConvertJson(ex1.Message, "Cannot convert Json of item to update")); } T itemOld; switch (type) { case UPDATE_TYPE.ADD: cacheLock.EnterWriteLock(); try { innerCache.Add(item); } finally { cacheLock.ExitWriteLock(); } return(new oCacheResult(request).ToOkEmpty()); case UPDATE_TYPE.DELETE: cacheLock.EnterWriteLock(); try { if (!string.IsNullOrWhiteSpace(fielKey) && !string.IsNullOrWhiteSpace(typeKey)) { fielKey = fkey.name; typeKey = fkey.type; object it = _getItemByKey(fielKey, typeKey, valKey); if (it == null) { return(new oCacheResult(request).ToFailInputNULL("DELETE: Cannot not find item has config FIELD_KEY as follows: NAME = " + fielKey + " & KEY = " + valKey + " & TYPE = " + typeKey)); } else { itemOld = (T)it; if (itemOld != null) { innerCache.Remove(itemOld); } isOk = true; } } } finally { cacheLock.ExitWriteLock(); } if (isOk) { return(new oCacheResult(request).ToOkEmpty()); } else { return(new oCacheResult(request).ToFailNotFound()); } case UPDATE_TYPE.EDIT: cacheLock.EnterWriteLock(); try { if (!string.IsNullOrWhiteSpace(fielKey) && !string.IsNullOrWhiteSpace(typeKey)) { object it = _getItemByKey(fielKey, typeKey, valKey); if (it == null) { return(new oCacheResult(request).ToFailInputNULL("EDIT: Cannot not find item has config FIELD_KEY as follows: NAME = " + fielKey + " & KEY = " + valKey + " & TYPE = " + typeKey)); } else { itemOld = (T)it; if (itemOld != null) { innerCache.Remove(itemOld); } innerCache.Add(item); isOk = true; } } } finally { cacheLock.ExitWriteLock(); } if (isOk) { return(new oCacheResult(request).ToOkEmpty()); } else { return(new oCacheResult(request).ToFailNotFound()); } } } catch (Exception ex) { return(new oCacheResult(request).ToFailException(ex.Message)); } return(new oCacheResult(request)); }