c# asp.net memcached client 调用示例

1. 将Commons.dll,ICSharpCode.SharpZipLib.dll,log4net.dll,Memcached.ClientLibrary.dll放到bin目录
2. 引用Memcached.ClientLibrary.dll
3. 类中引用using Memcached.ClientLibrary;
4. 代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Memcached.ClientLibrary;

namespace TestMemcachedApp
{
class Program
{
[STAThread]
public static void Main(String[] args)
{
string[] serverlist = { "192.168.1.11:11211","192.168.1.12:11211","192.168.1.13:11211" };//服务器可以是多个

//初始化池
SockIOPool pool = SockIOPool.GetInstance();
pool.SetServers(serverlist);//设置连接池可用的cache服务器列表,server的构成形式是IP:PORT(如:127.0.0.1:11211)
pool.InitConnections = 3;//初始连接数
pool.MinConnections = 3;//最小连接数
pool.MaxConnections = 5;//最大连接数
pool.SocketConnectTimeout = 1000;//设置连接的套接字超时
pool.SocketTimeout = 3000;//设置套接字超时读取
pool.MaintenanceSleep = 30;//设置维护线程运行的睡眠时间。如果设置为0,那么维护线程将不会启动,30就是每隔30秒醒来一次

//获取或设置池的故障标志。
//如果这个标志被设置为true则socket连接失败,将试图从另一台服务器返回一个套接字如果存在的话。
//如果设置为false,则得到一个套接字如果存在的话。否则返回NULL,如果它无法连接到请求的服务器。
pool.Failover = true;

pool.Nagle = false;//如果为false,对所有创建的套接字关闭Nagle的算法
pool.Initialize();

// 获得客户端实例
MemcachedClient mc = new MemcachedClient();
mc.EnableCompression = false;

Console.WriteLine("------------测 试-----------");
mc.Set("test", "my value"); //存储数据到缓存服务器,这里将字符串"my value"缓存,key 是"test"

if (mc.KeyExists("test")) //测试缓存存在key为test的项目
{
Console.WriteLine("test is Exists");
Console.WriteLine(mc.Get("test").ToString()); //在缓存中获取key为test的项目
}
else
{
Console.WriteLine("test not Exists");
}


mc.Delete("test"); //移除缓存中key为test的项目

if (mc.KeyExists("test"))
{
Console.WriteLine("test is Exists");
Console.WriteLine(mc.Get("test").ToString());
}
else
{
Console.WriteLine("test not Exists");
}
Console.ReadLine();

SockIOPool.GetInstance().Shutdown(); //关闭池, 关闭sockets
}
}
}


黑色头发:http://heisetoufa.iteye.com/
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值