using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Memcached.ClientLibrary;
namespace MemcachedHelper
{
public class Memcached
{
public static MemcachedClient _Memcache = new MemcachedClient();
//取值
public static Object Get(String key)
{
//这里初始化服务器IP地址,可写在配置文件这里读取即可,注意这里是个字符串数组
这里如果是多个服务器(即服务器集群,只需依次在数组中讲服务器IP端口添加进去就完成了),在Memcached中服务器集群就这么实现了,个人觉得太牛逼了,至于远离由于时间关系有空在说吧
string[] serverlist = { "127.0.0.1:11211" };
//初始化池
string poolName = "MemcacheIOPool";
SockIOPool pool = SockIOPool.GetInstance(poolName);
pool.SetServers(serverlist);
pool.InitConnections = 1;
pool.MinConnections = 1;
pool.MaxConnections = 500;
pool.SocketConnectTimeout = 1000;
pool.SocketTimeout = 3000;
pool.MaintenanceSleep = 30;
pool.Failover = true;
pool.Nagle = false;
pool.Initialize();//容器初始化
_Memcache.PoolName = poolName;
_Memcache.EnableCompression = false;
return _Memcache.Get(key);
}
//存值
public static bool Set(string key,Object value)
{
try
{
//这里初始化服务器IP地址,可写在配置文件这里读取即可,注意这里是个字符串数组
这里如果是多个服务器(即服务器集群,只需依次在数组中讲服务器IP端口添加进去就完成了),在Memcached中服务器集群就这么实现了,个人觉得太牛逼了,至于远离由于时间关系有空在说吧
string[] serverlist = { "127.0.0.1:11211" };
//初始化池
string poolName = "MemcacheIOPool";
SockIOPool pool = SockIOPool.GetInstance(poolName);
pool.SetServers(serverlist);
pool.InitConnections = 1;
pool.MinConnections = 1;
pool.MaxConnections = 500;
pool.SocketConnectTimeout = 1000;
pool.SocketTimeout = 3000;
pool.MaintenanceSleep = 30;
pool.Failover = true;
pool.Nagle = false;
pool.Initialize();//容器初始化
_Memcache.PoolName = poolName;
_Memcache.EnableCompression = false;
_Memcache.EnableCompression = false;
_Memcache.Set(key, value,System.DateTime.Now.AddMinutes(20));
return true;
}
catch (Exception ex)
{
throw;
}
}
}
[Serializable]
public class MyClass
{
string a;
public string A
{
get { return a; }
set { a = value; }
}
String b;
public String B
{
get { return b; }
set { b = value; }
}
}
}
由于时间关系直接给出简单代码了,只为说明使用方式.
客户端调用就很简单了 直接调用帮助类的相关方法:
MyClass m=new MyClass();
m.A="1";
m.B="2";
MemcachedHelper.Memcached.Set("Test1", m);
MyClass m1 = new MyClass();
m1=MemcachedHelper.Memcached.Get("Test1") as MyClass;
MessageBox.Show(m1.A.ToString()+m1.B.ToString());