标签:
1.下载Memcached For Windows
URL:http://code.jellycan.com/files/memcached-1.2.6-win32-bin.zip
2.管理员身份运行cmd
3.在Windows中安装Memcached服务:memcached.exe -d install(那么,对应卸载命令为:memcached.exe -d uninstall)
4.启动 net start "Memcached Server"
5.停止服务 net stop "Memcached Server"
6.
下载文件:http://pan.baidu.com/s/1w9Q8I
memcached clientlib项目地址:http://sourceforge.net/projects/memcacheddotnet/
解压该包,里面有1.1和2.0两个版本的,这里我们使用2.0版本的。(在压缩包中的目录地址为:\memcacheddotnet_clientlib-1.1.5\memcacheddotnet\trunk\clientlib\src\clientlib\bin\2.0\Release)
7.创建控制台项目
8.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Memcached.ClientLibrary;
namespace testMemcached1
{
class Program
{
static void Main(string[] args)
{
// Memcached服务器列表
// 如果有多台服务器,则以逗号分隔,例如:"192.168.80.10:11211","192.168.80.11:11211"
string[] serverList = { "192.168.1.116:11211" };
// 初始化SocketIO池
string poolName = "MyPool";
SockIOPool sockIOPool = SockIOPool.GetInstance(poolName);
// 添加服务器列表
sockIOPool.SetServers(serverList);
// 设置连接池初始数目
sockIOPool.InitConnections = 3;
// 设置连接池最小连接数目
sockIOPool.MinConnections = 3;
// 设置连接池最大连接数目
sockIOPool.MaxConnections = 5;
// 设置连接的套接字超时时间(单位:毫秒)
sockIOPool.SocketConnectTimeout = 1000;
// 设置套接字超时时间(单位:毫秒)
sockIOPool.SocketTimeout = 3000;
// 设置维护线程运行的睡眠时间:如果设置为0,那么维护线程将不会启动
sockIOPool.MaintenanceSleep = 30;
// 设置SockIO池的故障标志
sockIOPool.Failover = true;
// 是否用nagle算法启动
sockIOPool.Nagle = false;
// 正式初始化容器
sockIOPool.Initialize();
// 获取Memcached客户端实例
MemcachedClient memClient = new MemcachedClient();
// 指定客户端访问的SockIO池
memClient.PoolName = poolName;
// 是否启用压缩数据:如果启用了压缩,数据压缩长于门槛的数据将被储存在压缩的形式
memClient.EnableCompression = false;
Console.WriteLine("----------------------------测试开始----------------------------");
//// 01.简单的添加与读取操作
//memClient.Set("test1", "chm1");
//Console.WriteLine("test1:{0}", memClient.Get("test1"));
//// 02.先添加后修改再读取操作
//memClient.Set("test2", "chm2");
//Console.WriteLine("test2:{0}", memClient.Get("test2"));
//memClient.Set("test2", "chm22");
//Console.WriteLine("test2:{0}", memClient.Get("test2"));
//memClient.Replace("test2", "chm222");
//Console.WriteLine("test2:{0}", memClient.Get("test2"));
//// 03.判断Key值是否存在
//if (memClient.KeyExists("test2"))
//{
// Console.WriteLine("Key:test2 存在");
//}
//// 04.删除指定Key值的数据
//memClient.Add("test3", "chm3");
//Console.WriteLine("test3:{0}", memClient.Get("test3"));
//memClient.Delete("test3");
//if (!memClient.KeyExists("test3"))
//{
// Console.WriteLine("Key:test3 不存在");
//}
//// 05.设置数据过期时间:5秒后过期
//memClient.Add("test4", "chm4", DateTime.Now.AddSeconds(5));
//Console.WriteLine("test4:{0}", memClient.Get("test4"));
//Console.WriteLine("请等待3秒钟");
//System.Threading.Thread.Sleep(3000);
//Console.WriteLine("test4:{0}", memClient.Get("test4"));
//System.Threading.Thread.Sleep(2000);
//Console.WriteLine("请等待2秒钟");
//if (!memClient.KeyExists("test4"))
//{
// Console.WriteLine("test4 过期了");
//}
//06 测试存储对象
UserInfoEntity uie = new UserInfoEntity() { UserId = 1, UserName = "wt1", UserBirthday = Convert.ToDateTime("1988-09-28") };
memClient.Set("wt", uie);
var obj= memClient.Get("wt");
UserInfoEntity uieGet = obj as UserInfoEntity;
Console.WriteLine(uie.ToString());
Console.WriteLine("----------------------------测试完成----------------------------");
// 关闭SockIO池
sockIOPool.Shutdown();
Console.ReadKey();
}
}
}
标签:
原文地址:http://www.cnblogs.com/caohuimingfa/p/5616598.html