1. 程式人生 > >Memcached使用與糾錯(附代碼和相關dll)

Memcached使用與糾錯(附代碼和相關dll)

獲取數據 set null ilo string sin TP AC AR

今天沒事研究一下,誰想到遇到了幾個dll找不到,網上也不好找到,索性功夫不負有心人。貼出代碼和相關的dll

Memcached代碼:(網上都是的,很多人都保存了這個代碼)

using Memcached.ClientLibrary;
using System;

namespace Framework.MemCached
{
    public class MemCacheHelper
    {
        private static readonly MemcachedClient mc = null;

        static MemCacheHelper()
        {
            
//最好放在配置文件中 string[] serverlist = { "127.0.0.1:11211", "10.0.0.132:11211" }; //初始化池 SockIOPool pool = SockIOPool.GetInstance(); pool.SetServers(serverlist); pool.InitConnections = 3; pool.MinConnections = 3; pool.MaxConnections
= 5; pool.SocketConnectTimeout = 1000; pool.SocketTimeout = 3000; pool.MaintenanceSleep = 30; pool.Failover = true; pool.Nagle = false; pool.Initialize(); // 獲得客戶端實例 mc = new MemcachedClient(); mc.EnableCompression
= false; } /// <summary> /// 存儲數據 /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <returns>執行結果</returns> public static bool Set(string key, object value) { return mc.Set(key, value); } /// <summary> /// 存儲數據 /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <param name="time">過期日期</param> /// <returns>執行結果</returns> public static bool Set(string key, object value, DateTime time) { return mc.Set(key, value, time); } /// <summary> /// 獲取數據 /// </summary> /// <param name="key"></param> /// <returns>緩存數據</returns> public static object Get(string key) { return mc.Get(key); } /// <summary> /// 刪除 /// </summary> /// <param name="key"></param> /// <returns>true:刪除成功;false:刪除失敗.</returns> public static bool Delete(string key) { if (mc.KeyExists(key)) { return mc.Delete(key); } return false; } } }

測試代碼:(很簡單,隨便寫的)

protected void Button1_Click(object sender, EventArgs e)
        {
            //add cache
            bool result = Framework.MemCached.MemCacheHelper.Set("user", "jay");
            Response.Write(result);
        }
        protected void Button2_Click(object sender, EventArgs e)
        {
            //get cache
            object r = Framework.MemCached.MemCacheHelper.Get("user");
            if (r != null)
            {
                Response.Write(r.ToString());
            }
            else
            {
                Response.Write("error");
            }
        }
        protected void Button3_Click(object sender, EventArgs e)
        {
            //delete cache
            bool r = Framework.MemCached.MemCacheHelper.Delete("user");
            Response.Write(r);
        }

主要是找到對應版本的dll不好找,網上很多代碼,但幾乎都沒有附帶資源:https://pan.baidu.com/s/1-1kMXxIc-Hsv5neUIV5jgw

研究過程:

1.下載:http://static.runoob.com/download/memcached-1.4.5-amd64.zip 解壓。
2.使用管理員身份執行以下命令將 memcached 添加來任務計劃表中:
cmd.exe的位置:C:\Windows\System32
右鍵》以管理員身份運行(不然會報錯:拒絕訪問):
安裝memcached 在命令行裏輸入:schtasks /create /sc onstart /tn memcached /tr "‘G:\memcached-amd64\memcached.exe‘ -m 512"
註意:你需要使用你本機的路徑替代 G:\memcached-amd64\memcached.exe。
註意:-m 512 意思是設置 memcached 最大的緩存配置為512M。
註意:我們可以通過使用 "G:\memcached-amd64\memcached.exe -h" 命令查看更多的參數配置,不過都是英文,翻譯吧。
3.安裝完成後,啟動服務:G:\memcached-amd64\memcached.exe -d start
4.如果需要刪除 memcached 的任務計劃可以執行以下命令:schtasks /delete /tn memcached
5.一切就緒後,開始進行測試,發現仍然沒法添加緩存,cmd裏執行這個:
sc create "Memcached11211" binPath= "G:\memcached-amd64\memcached.exe -d runservice -p 11211" DisplayName= "Memcached11211" start= auto
具體是為什麽,沒查到呢還。
我是這裏看到的:https://blog.csdn.net/swjtu_yhz/article/details/60132572
我查過的文章:
http://www.runoob.com/memcached/window-install-memcached.html
http://www.cnblogs.com/caokai520/p/4390646.html
http://www.cnblogs.com/liangwenchao-912/p/5529000.html
https://www.cnblogs.com/minily/p/7456322.html
https://blog.csdn.net/sweetlei/article/details/78719822

Memcached使用與糾錯(附代碼和相關dll)