• Windows 下配置使用MemCached(转载)


    1. 安装配置MemCached服务端
      1. 下载memcached-1.2.6-win32-bin.zip ,解压后得到memcached.exe,就是memcached的主程序了。比如我们放到MemCached服务器下的C:Program FilesMemCacheD下
      2. 下载安装Memcached Manager ,通过这个来管理memcached的服务端。

        打开MemCacheD Manager,点击 add Server,填写服务器信息。我这里直接在本地安装了memcached。如图,填完后点击apply,成功的话右侧会出现服务器。

        Windows 下配置使用MemCached - qiuguangchun - sandea的个人主页

        点击Add Instance添加memcached实例。这里有一些配置信息。Ip,端口,内存等等,不解释了。点击apply后会提示你是否现在启动,我们这里选 是

        Windows 下配置使用MemCached - qiuguangchun - sandea的个人主页

        成功后发现右侧已经有实例了,到此服务端配置完毕。

        Windows 下配置使用MemCached - qiuguangchun - sandea的个人主页

    2. 客户端调用
      1. 首先需要下载Memcached .NET client Library 客户端类库,解压得到一个memcacheddotnet目录,一堆文件。

      为测试MemCached,我们建立一个web项目。引用Memcached.ClientLibrary.dll,这个dll在 memcacheddotnet runkclientlibsrcclientlibin2.0Release

      1. 使用比较简单,1个存 ,1个取。我们简单弄两页面。

        加上using

    using Memcached.ClientLibrary;

     

    存:

     

    代码:

    protected void Page_Load(object sender, EventArgs e)

    {

    if (!IsPostBack)

    {

    string[] serverlist = { server.Text };

    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();

    }

    }

    protected void SetValue_Click(object sender, EventArgs e)

    {

    MemcachedClient mc = new MemcachedClient();

    mc.EnableCompression = false;

    mc.Set(Key.Text, Value.Text);

    Response.Write("<script>alert('ok')</script>");

    }

     

     

    取:

    代码:

    protected void GetValue_Click(object sender, EventArgs e)

    {

    string[] serverlist = { server.Text };

    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();

    MemcachedClient mc = new MemcachedClient();

    mc.EnableCompression = false;

    string value = (string)mc.Get(Key.Text);

    Value.Text = value;

    }

     

     

    看看效果:

     

    点add存到memcached

     

    Windows 下配置使用MemCached - qiuguangchun - sandea的个人主页

     

    点get。得到结果啦。

    Windows 下配置使用MemCached - qiuguangchun - sandea的个人主页

     

     

    Ok,完毕。你也可以把客户端的代码再封装一下,让调用来的更简单。

  • 相关阅读:
    PHP学习笔记十二【数组排序】
    PHP学习笔记十一【数组】
    PHP学习笔记十【数组】
    PHP学习笔记九【数组二】
    PHP学习笔记八【数组】
    Codeforces 612E
    Codeforces 616E
    codeforce #339(div2)C Peter and Snow Blower
    poj 1113 Mall
    poj 2187 Beauty Contest
  • 原文地址:https://www.cnblogs.com/sandea/p/3294260.html
Copyright © 2020-2023  润新知