public oCacheResult SearchDynamic(oCacheRequest request) { cacheLock.EnterReadLock(); try { try { IEnumerable <T> rs = null; if (string.IsNullOrWhiteSpace(request.Conditions)) { rs = innerCache; } else { rs = innerCache.Where(request.Conditions); } int countResult = rs.Count(); if (!string.IsNullOrWhiteSpace(request.OrderbyName)) { rs = rs.OrderBy(request.OrderbyName); } if (request.PageNumber == 1 && request.PageSize > 0) { rs = rs.Take(request.PageSize); } else if (request.PageNumber > 0 && request.PageSize > 0) { rs = rs.Skip(request.PageSize * (request.PageNumber - 1)).Take(request.PageSize); } dynamic[] arr = rs.Distinct().Cast <dynamic>().ToArray(); return(new oCacheResult(request).ToOk(arr, this.Count, countResult)); } catch (Exception ex) { return(new oCacheResult(request).ToFailException(ex.Message, "CacheSynchronized.cs > SearchDynamic(oCacheRequest request)")); } } finally { cacheLock.ExitReadLock(); } }
public string searchDynamicReplyCacheKey(oCacheRequest request) { string key = Guid.NewGuid().ToString(); if (string.IsNullOrWhiteSpace(request.RequestId)) { request.RequestId = key; } else { key = request.RequestId; } oCacheResult rs = SearchDynamic(request); ObjectCache cache = MemoryCache.Default; cache.Set(key, rs, new CacheItemPolicy()); return(key); }
public oCacheResult(oCacheRequest request) : base() { this.Request = request; }
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)); }