C#使用memcached示例

1安装libevent  ubuntu:apt-get install  名称    suse:yast2 -i 名称   centos: yum install 名称

2安装memcached  ubuntu:apt-get install  名称    suse:yast2 -i 名称   centos: yum install 名称

3启动服务(类似套接字开启监听,查看是否开启就好办了,ps ef|grep memcache or netstat)

4客户端必要配置(依赖性,connectstring等(此处体现在下面设计图上)),根据配置按需连接,准备通信.

以上完成:测试

1:建立winform app

2假设布局如下,两个label,两个textbox,一个button.

 

3:双击按钮,添加如下代码(注意第四步添加的dll引用以及名称空间)

private void btnGetValue_Click(object sender, EventArgs e)

{

    string[] serverlist = { txtServer.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(txtKey.Text);

    MessageBox.Show(value);

}

4:运行,输入memched所在的服务器ip以及memcached对应端口号,(key:value也有类似set get的使用习惯)

我的总结:模仿,定制,适应新的需求.其实这和mysql for .net的流程几乎就是一个模子.

有木有发现一个规律,mysql-server,redis-server,*-server,一一对应*-client(客户端有手动输入命令的终端,有加了个壳的gui,有编码连接的app,本质,都一样,socket通信)

原文地址:https://www.cnblogs.com/aobama/p/4346946.html