下载客户端的3个dll,
ICSharpCode.SharpZipLib.dll,log4net.dll,Memcached.ClientLibrary.dll
新建一个简单控制台应用程序:
class Program
{
private static void Main(string[] args)
{
SockIOPool pool = SockIOPool.GetInstance();
string[] servers = {"127.0.0.1:11211"};
pool.SetServers(servers);
pool.MinConnections = 3;
pool.MaxConnections = 5;
pool.InitConnections = 3;
pool.SocketConnectTimeout = 5000;
pool.Initialize();
MemcachedClient client = new MemcachedClient();
client.EnableCompression = false;
Console.WriteLine("-----------------------Memcached Set 设置值--------------------------");
client.Set("key1", "value1");
Console.WriteLine(client.Get("key1"));
Console.WriteLine("-----------------------Memcached Add 设置值--------------------------");
client.Add("key2", "value2");
Console.WriteLine(client.Get("key2"));
client.Set("key2", "value1 value2");
Console.WriteLine(client.Get("key2"));
Console.WriteLine("-----------------------Memcached Replace 设置值--------------------------");
client.Replace("key2", "value3");
Console.WriteLine(client.Get("key2"));
Console.WriteLine("-----------------------Memcached 键值是否存在--------------------------");
if (client.KeyExists("key2"))
{
Console.WriteLine("键key2 存在");
}
if (client.KeyExists("hechen") == false)
{
Console.WriteLine("键hechen 不存在");
}
Console.WriteLine("-----------------------Memcached 删除数据--------------------------");
client.Add("key4", "value4");
Console.WriteLine("key4==>" + client.Get("key4"));
client.Delete("key4");
if (!client.KeyExists("key4"))
{
Console.WriteLine("key4 已将删除");
}
Console.WriteLine("-----------------------Memcached 数据过期--------------------------");
client.Add("key5", "value5", DateTime.Now.AddMilliseconds(5000));
Console.WriteLine(client.Get("key5"));
System.Threading.Thread.Sleep(6000);
Console.WriteLine("过期: " + client.Get("key5"));
//自定义实例化封装,接口
ICache cache = Memcache.Instance();
Console.WriteLine(cache.ContainKey("hechen"));
Student student = new Student() {ID = 1, Name = "hechen"};
Console.WriteLine(cache.Add<Student>("hechen", student));
Console.WriteLine(cache.ContainKey("hechen"));
Student stu = cache.Get<Student>("hechen");
Console.WriteLine(stu.ID + ":" + stu.Name);
cache.Add("qa", "ddddddddd");
cache.Remove();
Student stu1 = cache.Get<Student>("hechen");
if (stu1 != null)
{
Console.WriteLine(stu1.ID + ":" + stu1.Name);
}
Console.WriteLine(cache.Get("qa"));
}
}
自定义实例化的接口ICache.cs代码:
public interface ICache
{
bool ContainKey(string argKey);
bool Add(string argKey,object argValue);
bool Add(string argKey, object argValue, DateTime argDateExpiration);
bool Add<T>(string argKey, T entity) where T : class;
bool Add<T>(string argKey, T entity, DateTime argDateExpiration) where T : class;
bool Set(string argKey, object argValue);
bool Set(string argKey, object argValue, DateTime argDateExpiration);
bool Set<T>(string argKey, T entity) where T : class;
bool Set<T>(string argKey, T entity, DateTime argDateExpiration) where T : class;
bool Replace(string argKey,object argValue);
bool Replace(string argKey,object argValue,DateTime argDateExpiration);
bool Replace<T>(string argKey, T entity) where T : class;
bool Replace<T>(string argKey, T entity, DateTime argDateExpiration) where T : class;
object Get(string argKey);
T Get<T>(string argKey);
bool Remove(string argKey);
bool Remove(string argKey, DateTime argDateExpiration);
bool Remove();
bool Remove(ArrayList servers);
}
Memcache.cs代码如下:
public class Memcache:ICache
{
private MemcachedClient client;
private static Memcache memcache;
/// <summary>
/// 构造方法
/// </summary>
protected Memcache()
{
SockIOPool pool = SockIOPool.GetInstance();
string[] servers = { "127.0.0.1:11211" };
pool.SetServers(servers);
pool.MinConnections = 3;
pool.MaxConnections = 5;
pool.InitConnections = 3;
pool.SocketConnectTimeout = 5000;
pool.Initialize();
this.client = new MemcachedClient();
client.EnableCompression = false;
}
public static Memcache Instance()
{
if (memcache == null)
{
memcache = new Memcache();
}
return memcache;
}
/// <summary>
/// 判断是否包含某个键
/// </summary>
/// <param name="argKey">键值</param>
/// <returns></returns>
public bool ContainKey(string argKey)
{
return client.KeyExists(argKey);
}
/// <summary>
/// 添加缓存数据
/// </summary>
/// <param name="argKey">键值</param>
/// <param name="argValue">存储值</param>
/// <returns></returns>
public bool Add(string argKey, object argValue)
{
return client.Add(argKey,argValue);
}
/// <summary>
/// 添加缓存数据
/// </summary>
/// <param name="argKey">键值</param>
/// <param name="argValue">存储值</param>
/// <param name="argDateExpiration">过期时间</param>
/// <returns></returns>
public bool Add(string argKey, object argValue, DateTime argDateExpiration)
{
return client.Add(argKey, argValue, argDateExpiration);
}
/// <summary>
/// 添加缓存数据
/// </summary>
/// <typeparam name="T">存储对象类型</typeparam>
/// <param name="argKey">键值</param>
/// <param name="entity">存储值</param>
/// <returns></returns>
public bool Add<T>(string argKey, T entity) where T : class
{
return client.Add(argKey, entity);
}
/// <summary>
/// 添加缓存数据
/// </summary>
/// <typeparam name="T">存储对象类型</typeparam>
/// <param name="argKey">键值</param>
/// <param name="entity">存储值</param>
/// <param name="argDateExpiration">过期时间</param>
/// <returns></returns>
public bool Add<T>(string argKey, T entity, DateTime argDateExpiration) where T : class
{
return client.Add(argKey, entity, argDateExpiration);
}
/// <summary>
/// 添加缓存数据,如果存在则替换原有数据
/// </summary>
/// <param name="argKey">键值</param>
/// <param name="argValue">存储值</param>
/// <returns></returns>
public bool Set(string argKey, object argValue)
{
if (ContainKey(argKey))
{
return false;
}
return client.Set(argKey, argValue);
}
/// <summary>
/// 添加缓存数据,如果存在则替换原有数据
/// </summary>
/// <param name="argKey">键值</param>
/// <param name="argValue">存储值</param>
/// <param name="argDateExpiration">过期时间</param>
/// <returns></returns>
public bool Set(string argKey, object argValue, DateTime argDateExpiration)
{
if (ContainKey(argKey))
{
return false;
}
return client.Set(argKey, argValue, argDateExpiration);
}
/// <summary>
/// 添加缓存数据,如果存在则替换原有数据
/// </summary>
/// <typeparam name="T">存储对象类型</typeparam>
/// <param name="argKey">键值</param>
/// <param name="entity">存储值</param>
/// <returns></returns>
public bool Set<T>(string argKey, T entity) where T : class
{
if (ContainKey(argKey))
{
return false;
}
return client.Set(argKey, entity);
}
/// <summary>
/// 添加缓存数据,如果存在则替换原有数据
/// </summary>
/// <typeparam name="T">存储对象类型</typeparam>
/// <param name="argKey">键值</param>
/// <param name="entity">存储值</param>
/// <param name="argDateExpiration">过期时间</param>
/// <returns></returns>
public bool Set<T>(string argKey, T entity, DateTime argDateExpiration) where T : class
{
if (ContainKey(argKey))
{
return false;
}
return client.Set(argKey, entity, argDateExpiration);
}
/// <summary>
/// 替换原有缓存
/// </summary>
/// <param name="argKey">键值</param>
/// <param name="argValue">存储值</param>
/// <returns></returns>
public bool Replace(string argKey, object argValue)
{
return client.Replace(argKey,argValue);
}
/// <summary>
/// 替换原有缓存
/// </summary>
/// <param name="argKey">键值</param>
/// <param name="argValue">存储值</param>
/// <param name="argDateExpiration">过期时间</param>
/// <returns></returns>
public bool Replace(string argKey, object argValue, DateTime argDateExpiration)
{
return client.Replace(argKey,argValue,argDateExpiration);
}
/// <summary>
/// 替换原有缓存
/// </summary>
/// <typeparam name="T">存储类型</typeparam>
/// <param name="argKey">键值</param>
/// <param name="entity">存储值</param>
/// <returns></returns>
public bool Replace<T>(string argKey, T entity) where T : class
{
return client.Replace(argKey,entity);
}
/// <summary>
/// 替换原有缓存
/// </summary>
/// <typeparam name="T">存储类型</typeparam>
/// <param name="argKey">键值</param>
/// <param name="entity">存储值</param>
/// <param name="argDateExpiration">过期时间</param>
/// <returns></returns>
public bool Replace<T>(string argKey, T entity, DateTime argDateExpiration) where T : class
{
return client.Replace(argKey, entity,argDateExpiration);
}
/// <summary>
/// 获得缓存数据
/// </summary>
/// <param name="argKey">键值</param>
/// <returns></returns>
public object Get(string argKey)
{
return client.Get(argKey);
}
/// <summary>
/// 获得缓存数据
/// </summary>
/// <typeparam name="T">返回数据类型</typeparam>
/// <param name="argKey">键值</param>
/// <returns></returns>
public T Get<T>(string argKey)
{
T entity=default(T);
entity = (T)client.Get(argKey);
return entity;
}
/// <summary>
/// 移除一个缓存数据
/// </summary>
/// <param name="argKey">键值</param>
/// <returns></returns>
public bool Remove(string argKey)
{
return client.Delete(argKey);
}
/// <summary>
/// 移除一个缓存数据
/// </summary>
/// <param name="argKey">键值</param>
/// <param name="argDateExpiration">数据过期时间</param>
/// <returns></returns>
public bool Remove(string argKey, DateTime argDateExpiration)
{
return client.Delete(argKey,argDateExpiration);
}
/// <summary>
/// 移除所有缓存数据
/// </summary>
/// <returns></returns>
public bool Remove()
{
return client.FlushAll();
}
/// <summary>
/// 移除所有缓存数据
/// </summary>
/// <param name="servers">服务器地址</param>
/// <returns></returns>
public bool Remove(ArrayList servers)
{
return client.FlushAll(servers);
}
}
运行结果如图: