using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Memcached.ClientLibrary;
using System.Collections;
namespace WL.Web.Common
{
public class MemcacheX
{
private MemcachedClient client;
private static MemcacheX memcache;
///
/// 构造方法
///
protected MemcacheX()
{
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 MemcacheX Instance()
{
if (memcache == null)
{
memcache = new MemcacheX();
}
return memcache;
}
///
/// 判断是否包含某个键
///
/// 键值
///
public bool ContainKey(string argKey)
{
return client.KeyExists(argKey);
}
///
/// 添加缓存数据
///
/// 键值
/// 存储值
///
public bool Add(string argKey, object argValue)
{
return client.Add(argKey, argValue);
}
///
/// 添加缓存数据
///
/// 键值
/// 存储值
/// 过期时间
///
public bool Add(string argKey, object argValue, DateTime argDateExpiration)
{
return client.Add(argKey, argValue, argDateExpiration);
}
///
/// 添加缓存数据
///
/// 存储对象类型
/// 键值
/// 存储值
///
public bool Add(string argKey, T entity) where T : class
{
return client.Add(argKey, entity);
}
///
/// 添加缓存数据
///
/// 存储对象类型
/// 键值
/// 存储值
/// 过期时间
///
public bool Add(string argKey, T entity, DateTime argDateExpiration) where T : class
{
return client.Add(argKey, entity, argDateExpiration);
}
///
/// 添加缓存数据,如果存在则替换原有数据
///
/// 键值
/// 存储值
///
public bool Set(string argKey, object argValue)
{
if (ContainKey(argKey))
{
return false;
}
return client.Set(argKey, argValue);
}
///
/// 添加缓存数据,如果存在则替换原有数据
///
/// 键值
/// 存储值
/// 过期时间
///
public bool Set(string argKey, object argValue, DateTime argDateExpiration)
{
if (ContainKey(argKey))
{
return false;
}
return client.Set(argKey, argValue, argDateExpiration);
}
///
/// 添加缓存数据,如果存在则替换原有数据
///
/// 存储对象类型
/// 键值
/// 存储值
///
public bool Set(string argKey, T entity) where T : class
{
if (ContainKey(argKey))
{
return false;
}
return client.Set(argKey, entity);
}
///
/// 添加缓存数据,如果存在则替换原有数据
///
/// 存储对象类型
/// 键值
/// 存储值
/// 过期时间
///
public bool Set(string argKey, T entity, DateTime argDateExpiration) where T : class
{
if (ContainKey(argKey))
{
return false;
}
return client.Set(argKey, entity, argDateExpiration);
}
///
/// 替换原有缓存
///
/// 键值
/// 存储值
///
public bool Replace(string argKey, object argValue)
{
return client.Replace(argKey, argValue);
}
///
/// 替换原有缓存
///
/// 键值
/// 存储值
/// 过期时间
///
public bool Replace(string argKey, object argValue, DateTime argDateExpiration)
{
return client.Replace(argKey, argValue, argDateExpiration);
}
///
/// 替换原有缓存
///
/// 存储类型
/// 键值
/// 存储值
///
public bool Replace(string argKey, T entity) where T : class
{
return client.Replace(argKey, entity);
}
///
/// 替换原有缓存
///
/// 存储类型
/// 键值
/// 存储值
/// 过期时间
///
public bool Replace(string argKey, T entity, DateTime argDateExpiration) where T : class
{
return client.Replace(argKey, entity, argDateExpiration);
}
///
/// 获得缓存数据
///
/// 键值
///
public object Get(string argKey)
{
return client.Get(argKey);
}
///
/// 获得缓存数据
///
/// 返回数据类型
/// 键值
///
public T Get(string argKey)
{
T entity = default(T);
entity = (T)client.Get(argKey);
return entity;
}
///
/// 移除一个缓存数据
///
/// 键值
///
public bool Remove(string argKey)
{
return client.Delete(argKey);
}
///
/// 移除一个缓存数据
///
/// 键值
/// 数据过期时间
///
public bool Remove(string argKey, DateTime argDateExpiration)
{
return client.Delete(argKey, argDateExpiration);
}
///
/// 移除所有缓存数据
///
///
public bool Remove()
{
return client.FlushAll();
}
///
/// 移除所有缓存数据
///
/// 服务器地址
///
public bool Remove(ArrayList servers)
{
return client.FlushAll(servers);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using yww.Utils;
using System.Collections;
using Memcached.ClientLibrary;
using System.Text;
namespace WL.Web
{
public partial class test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
}
}
private void myload()
{
//分布Memcachedf服务IP 端口
string[] servers = { "192.168.1.83:11211" };
//初始化池
SockIOPool pool = SockIOPool.GetInstance();
pool.SetServers(servers);
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 Memcached.ClientLibrary.MemcachedClient();
mc.EnableCompression = false;
Hashtable ht = mc.Stats();
StringBuilder sb = new StringBuilder();
sb.AppendLine("
_______________________________________
");
sb.AppendLine("Memcached Stats:");
sb.AppendLine("
_______________________________________
");
foreach (DictionaryEntry de in ht)
{
Hashtable info = (Hashtable)de.Value;
foreach (DictionaryEntry de2 in info)
{
sb.AppendLine(de2.Key.ToString() + ": " + de2.Value.ToString() + "
");
}
}
Response.Write(sb.ToString());
}
public void clear()
{
string[] servers = { "192.168.1.83:11211" };
//初始化池
SockIOPool pool = SockIOPool.GetInstance();
pool.SetServers(servers);
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 Memcached.ClientLibrary.MemcachedClient();
mc.EnableCompression = false;
mc.Delete("dt");
Response.Write("清空缓存成功");
}
public void tt()
{
//分布Memcachedf服务IP 端口
string[] servers = { "192.168.1.83:11211" };
//初始化池
SockIOPool pool = SockIOPool.GetInstance();
pool.SetServers(servers);
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 Memcached.ClientLibrary.MemcachedClient();
mc.EnableCompression = false;
StringBuilder sb = new StringBuilder();
//写入缓存
sb.AppendLine("写入缓存测试:");
sb.AppendLine("
_______________________________________
");
if (mc.KeyExists("cache"))
{
sb.AppendLine("缓存cache已存在");
}
else
{
mc.Set("cache", "写入缓存时间:" + DateTime.Now.ToString());
sb.AppendLine("缓存已成功写入到cache");
}
sb.AppendLine("
_______________________________________
");
sb.AppendLine("读取缓存内容如下:
");
sb.AppendLine(mc.Get("cache").ToString());
//测试缓存过期
sb.AppendLine("
_______________________________________
");
if (mc.KeyExists("endCache"))
{
sb.AppendLine("缓存endCache已存在,过期时间为:" + mc.Get("endCache").ToString());
}
else
{
mc.Set("endCache", DateTime.Now.AddMinutes(1).ToString(), DateTime.Now.AddMinutes(1));
sb.AppendLine("缓存已更新写入到endCache,写入时间:" + DateTime.Now.ToString() + " 过期时间:" + DateTime.Now.AddMinutes(1).ToString());
}
//分析缓存状态
Hashtable ht = mc.Stats();
sb.AppendLine("
_______________________________________
");
sb.AppendLine("Memcached Stats:");
sb.AppendLine("
_______________________________________
");
foreach (DictionaryEntry de in ht)
{
Hashtable info = (Hashtable)de.Value;
foreach (DictionaryEntry de2 in info)
{
sb.AppendLine(de2.Key.ToString() + ": " + de2.Value.ToString() + "
");
}
}
Response.Write(sb.ToString());
}
public void setTable()
{
StringBuilder sb = new StringBuilder();
DataTable dt = Common.DataBase.GetTableRegion();
sb.AppendLine("---------------------------------
");
sb.AppendLine("DataTable 条数:"+dt.Rows.Count+"
");
Common.MemcacheX mc = Common.MemcacheX.Instance();
if (mc.ContainKey("dt"))
{
sb.AppendLine("缓存dt已存在");
}
else
{
bool b = mc.Set("dt", dt);
sb.AppendLine("DataTable保存到MC中返回结果:" + b + "
");
}
sb.AppendLine("---------------------------------
");
Response.Write(sb.ToString());
}
public void getTable()
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("---------------------------------
");
Common.MemcacheX mc = Common.MemcacheX.Instance();
DataTable dt = mc.Get("dt");
if (dt != null)
{
sb.AppendLine("cache 中读取到"+dt.Rows.Count+"条记录
");
}
else
{
sb.AppendLine("cache 中没有读到数据
");
}
sb.AppendLine("---------------------------------
");
Response.Write(sb.ToString());
}
protected void btn_save_Click(object sender, EventArgs e)
{
setTable();
}
protected void btn_load_Click(object sender, EventArgs e)
{
getTable();
}
protected void btn_clear_Click(object sender, EventArgs e)
{
clear();
}
protected void btn_stats_Click(object sender, EventArgs e)
{
myload();
}
}
}
https://files.cnblogs.com/files/ainidewen/资料.rar