.net/c# memcached缓存获取所有缓存键的方法步骤(3)


/// <summary>
       /// 获取所有缓存键
       /// </summary>
       /// <returns></returns>
       public static IList<string> GetAllKeys()
       {
           IList<int> idList = new List<int>();
           IList<string> list = MemcachedManager.GetStats(MemcachedManager.ServerList, MemcachedStats.Items, null);
           foreach (var item in list)
           {
               string[] tmpArr = item.Split(':');
               if (tmpArr.Length > 1)
               {
                   int itemID = 0;
                   if (tmpArr[1] == "__") continue;

                   int.TryParse(tmpArr[1], out itemID);
                   if (itemID <= 0) continue;

                   bool find = false;
                   foreach (int item1 in idList)
                   {
                       if (item1 == itemID)
                       {
                           find = true;
                           break;
                       }
                   }

                   if (!find)
                   {
                       idList.Add(itemID);
                   }
               }
           }

           IList<string> keys = new List<string>();
           foreach (int item in idList)
           {
               IList<string> cachearr = MemcachedManager.GetStats(MemcachedManager.ServerList, MemcachedStats.CachedDump, item + " 0");
               foreach (string itemCache in cachearr)
               {
                   string[] tmpArr = itemCache.Split(':');
                   if (tmpArr.Length > 1)
                   {
                       if (tmpArr[1] == "__")
                       {
                           continue;
                       }

                       keys.Add(tmpArr[0]);
                   }
               }
           }

           return keys;
       }

调用方法

复制代码 代码如下:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjgyjy.html