91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

c# memcached緩存獲取所有緩存鍵的方法步驟

發布時間:2021-09-10 17:12:13 來源:億速云 閱讀:101 作者:chen 欄目:開發技術

這篇文章主要講解了“c# memcached緩存獲取所有緩存鍵的方法步驟”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“c# memcached緩存獲取所有緩存鍵的方法步驟”吧!

使用組件

memcached 1.2.6

.net 類庫 memcacheddotnet_clientlib-1.1.5

1.增加memcacheddotnet_clientlib-1.1.5代碼

下載好組件后,用vs打開.net類庫memcacheddotnet_clientlib-1.1.5,打開MemCachedClient.cs,增加如下方法:

復制代碼 代碼如下:


public Hashtable Stats(ArrayList servers, string command)
        {

            // get SockIOPool instance
            SockIOPool pool = SockIOPool.GetInstance(_poolName);

            // return false if unable to get SockIO obj
            if (pool == null)
            {
                //if(log.IsErrorEnabled)
                //{
                //    log.Error(GetLocalizedString("unable to get socket pool"));
                //}
                return null;
            }

            // get all servers and iterate over them
            if (servers == null)
                servers = pool.Servers;

            // if no servers, then return early
            if (servers == null || servers.Count <= 0)
            {
                //if(log.IsErrorEnabled)
                //{
                //    log.Error(GetLocalizedString("stats no servers"));
                //}
                return null;
            }

            // array of stats Hashtables
            Hashtable statsMaps = new Hashtable();

            for (int i = 0; i < servers.Count; i++)
            {

                SockIO sock = pool.GetConnection((string)servers[i]);
                if (sock == null)
                {
                    //if(log.IsErrorEnabled)
                    //{
                    //    log.Error(GetLocalizedString("unable to connect").Replace("$$Server$$", servers[i].ToString()));
                    //}
                    continue;
                }

                // build command
                if (command == null || command.Length == 0)
                {
                    command = "stats\r\n";
                }
                else
                {
                    command = command + "\r\n";
                }

                try
                {
                    sock.Write(UTF8Encoding.UTF8.GetBytes(command));
                    sock.Flush();

                    // map to hold key value pairs
                    Hashtable stats = new Hashtable();

                    // loop over results
                    while (true)
                    {
                        string line = sock.ReadLine();
                        //if(log.IsDebugEnabled)
                        //{
                        //    log.Debug(GetLocalizedString("stats line").Replace("$$Line$$", line));
                        //}

                        if (line.StartsWith(STATS))
                        {
                            string[] info = line.Split(' ');
                            string key = info[1];
                            string val = info[2];

                            //if(log.IsDebugEnabled)
                            //{
                            //    log.Debug(GetLocalizedString("stats success").Replace("$$Key$$", key).Replace("$$Value$$", val));
                            //}

                            stats[key] = val;

                        }
                        else if (line.StartsWith("ITEM"))
                        {

                            string[] info = line.Split('[');
                            string key = info[0].Split(' ')[1];
                            string val = "[" + info[1];

                            stats[key] = val;
                        }
                        else if (END == line)
                        {
                            // finish when we get end from server
                            //if(log.IsDebugEnabled)
                            //{
                            //    log.Debug(GetLocalizedString("stats finished"));
                            //}
                            break;
                        }

                        statsMaps[servers[i]] = stats;
                    }
                }
                catch//(IOException e)
                {
                    //if(log.IsErrorEnabled)
                    //{
                    //    log.Error(GetLocalizedString("stats IOException"), e);
                    //}

                    try
                    {
                        sock.TrueClose();
                    }
                    catch//(IOException)
                    {
                        //if(log.IsErrorEnabled)
                        //{
                        //    log.Error(GetLocalizedString("failed to close some socket").Replace("$$Socket$$", sock.ToString()));
                        //}
                    }

                    sock = null;
                }

                if (sock != null)
                    sock.Close();
            }

            return statsMaps;
        }

2 文章中有GetStats方法,將它修改如下:

復制代碼 代碼如下:


/// <summary>
        /// 獲取服務器端緩存的數據信息
        /// </summary>
        /// <param name="serverArrayList">要訪問的服務列表</param>
        /// <param name="statsCommand">此參數的功能暫時無效</param>
        /// <param name="param">此參數的功能暫時無效</param>
        /// <returns>返回信息</returns>
        public static IList<string> GetStats(IList<string> serverArrayList, MemcachedStats statsCommand, string param)
        {
            IList<string> statsArray = new List<string>();
            if (param == null)
                param = "";
            else
            {
                param = param.Trim().ToLower();
            }

            string commandstr = "stats";
            //轉換stats命令參數
            switch (statsCommand)
            {
                case MemcachedStats.Reset: { commandstr = "stats reset"; break; }
                case MemcachedStats.Malloc: { commandstr = "stats malloc"; break; }
                case MemcachedStats.Maps: { commandstr = "stats maps"; break; }
                case MemcachedStats.Sizes: { commandstr = "stats sizes"; break; }
                case MemcachedStats.Slabs: { commandstr = "stats slabs"; break; }
                case MemcachedStats.Items: { commandstr = "stats items"; break; }//此處原先是返回stats
                case MemcachedStats.CachedDump:
                    {
                        string[] statsparams = param.Split(' ');
                        if (statsparams.Length == 2)
                            if (param.IsIntArr(' '))// Utils.IsNumericArray(statsparams)
                                commandstr = "stats cachedump  " + param;

                        break;
                    }
                case MemcachedStats.Detail:
                    {
                        if (string.Equals(param, "on") || string.Equals(param, "off") || string.Equals(param, "dump"))
                            commandstr = "stats detail " + param.Trim();

                        break;
                    }
                default: { commandstr = "stats"; break; }
            }

            ArrayList arr = new ArrayList(serverArrayList.ToArray());

            Hashtable stats = MemcachedManager.CacheClient.Stats(arr, commandstr);

            foreach (string key in stats.Keys)
            {
                statsArray.Add("server:__:" + key);//此處也改了
                Hashtable values = (Hashtable)stats[key];
                foreach (string key2 in values.Keys)
                {
                    statsArray.Add(key2 + ":" + values[key2]);
                }
            }
            return statsArray;
        }

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;
       }

調用方法

復制代碼 代碼如下:


IList<string> list = MemcachedManager.GetAllKeys();
            foreach (var item in list)
            {
                Response.Write(item + "<br />");
            }

感謝各位的閱讀,以上就是“c# memcached緩存獲取所有緩存鍵的方法步驟”的內容了,經過本文的學習后,相信大家對c# memcached緩存獲取所有緩存鍵的方法步驟這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

得荣县| 旌德县| 炎陵县| 鲁甸县| 安塞县| 四会市| 乌海市| 阳城县| 吉木萨尔县| 灌云县| 区。| 广宁县| 辽阳市| 镇坪县| 凌海市| 江津市| 新源县| 南皮县| 原平市| 通榆县| 晋宁县| 资阳市| 永平县| 梧州市| 新丰县| 肇州县| 汾西县| 梁河县| 禹州市| 辛集市| 保靖县| 定州市| 普宁市| 法库县| 泸水县| 慈溪市| 黄平县| 长汀县| 攀枝花市| 闽侯县| 砚山县|