public IActionResult Search(string key, int?pageIndex, int?pageSize, int?appId, bool?ignoreTTL, bool?ignoreNull) { if (!appId.HasValue) { return(Content("[]")); } if (!pageIndex.HasValue) { pageIndex = 1; } if (!pageSize.HasValue) { pageSize = 50; } if (!ignoreTTL.HasValue) { ignoreTTL = true; } if (!ignoreNull.HasValue) { ignoreNull = true; } Model.App app = _appService.Find(appId.Value); if (app == null) { return(Content("[]")); } ICacheService service = new CacheService(); PagedDataJsonMsg result = service.Search(app, key, pageIndex.Value, pageSize.Value, ignoreTTL.Value, ignoreNull.Value); return(Json(result)); }
/// <summary> /// search format key result by params in the cache server /// </summary> /// <param name="app">app information</param> /// <param name="key">the format key to query</param> /// <param name="pageIndex">page index</param> /// <param name="pageSize">result count for one page</param> /// <param name="ignoreType">whether query with cache type, /// true will ignore the type, /// false will search key result one by one with key type result /// (warning:it may cost more time to finish the query)</param> /// <returns></returns> public PagedDataJsonMsg Search(App app, string keyFormat, int pageIndex, int pageSize, bool ignoreTTL = false, bool ignoreNull = false) { var cache = Caching.CacheFactory.Create(Caching.CacheType.Rediscache, app.ConnectionString); var ra = new RangeAnalysis(keyFormat); string format = ra.Format(); List <Ranges> ranges = ra.Analysis(); int keyCount = 0; List <string> keys = new List <string>(); if (ranges.Count > 0) { keys = KeyManager.GetPagedKeys(format, ranges, pageIndex, pageSize); keyCount = KeyManager.GetKeysCount(ranges); } else { keys.Add(keyFormat); keyCount = 1; } List <string> keyResult = null; List <CacheKeyType> typeResult = cache.Type(keys.ToArray()); if (typeResult != null) { keyResult = new List <string>(); for (int i = 0; i < keys.Count; i++) { if (typeResult[i] == CacheKeyType.None) { keyResult.Add(""); } else { keyResult.Add(cache.QueryWithType(keys[i], typeResult[i])); } } } List <TimeSpan?> expireResult = null; if (!ignoreTTL) { expireResult = cache.Expire(keys.ToArray()); } cache.Close(); List <CacheResult> list = null; if (keyResult != null) { list = new List <CacheResult>(); var resultList = keyResult.ToList(); for (int i = 0; i < resultList.Count; i++) { Caching.CacheResult cr = new Caching.CacheResult(); cr.Index = i + 1; cr.Key = keys[i]; cr.Expire = !ignoreTTL && expireResult[i].HasValue ? (expireResult[i].Value.TotalSeconds + "s") : ""; cr.Type = typeResult[i].ToString(); cr.Value = resultList[i]; if (ignoreNull && cr.Type == CacheKeyType.None.ToString()) { continue; } list.Add(cr); } } //if the cache connection string is error //the cache result list will return null //then reset the key count to 0 if (list == null) { keyCount = 0; } PagedDataJsonMsg msg = new PagedDataJsonMsg(MsgConst.OK, "", list, keyCount); return(msg); }