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;
/// <summary>
/// 构造方法
/// </summary>
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;
}
/// <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);
}
}
}
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("<br>_______________________________________<br>");
sb.AppendLine("Memcached Stats:");
sb.AppendLine("<br>_______________________________________<br>");
foreach (DictionaryEntry de in ht)
{
Hashtable info = (Hashtable)de.Value;
foreach (DictionaryEntry de2 in info)
{
sb.AppendLine(de2.Key.ToString() + ": " + de2.Value.ToString() + "<br>");
}
}
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("<br>_______________________________________<br>");
if (mc.KeyExists("cache"))
{
sb.AppendLine("缓存cache已存在");
}
else
{
mc.Set("cache", "写入缓存时间:" + DateTime.Now.ToString());
sb.AppendLine("缓存已成功写入到cache");
}
sb.AppendLine("<br>_______________________________________<br>");
sb.AppendLine("读取缓存内容如下:<br>");
sb.AppendLine(mc.Get("cache").ToString());
//测试缓存过期
sb.AppendLine("<br>_______________________________________<br>");
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("<br>_______________________________________<br>");
sb.AppendLine("Memcached Stats:");
sb.AppendLine("<br>_______________________________________<br>");
foreach (DictionaryEntry de in ht)
{
Hashtable info = (Hashtable)de.Value;
foreach (DictionaryEntry de2 in info)
{
sb.AppendLine(de2.Key.ToString() + ": " + de2.Value.ToString() + "<br>");
}
}
Response.Write(sb.ToString());
}
public void setTable()
{
StringBuilder sb = new StringBuilder();
DataTable dt = Common.DataBase.GetTableRegion();
sb.AppendLine("---------------------------------<br>");
sb.AppendLine("DataTable 条数:"+dt.Rows.Count+"<br>");
Common.MemcacheX mc = Common.MemcacheX.Instance();
if (mc.ContainKey("dt"))
{
sb.AppendLine("缓存dt已存在");
}
else
{
bool b = mc.Set<DataTable>("dt", dt);
sb.AppendLine("DataTable保存到MC中返回结果:" + b + "<br>");
}
sb.AppendLine("---------------------------------<br>");
Response.Write(sb.ToString());
}
public void getTable()
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("---------------------------------<br>");
Common.MemcacheX mc = Common.MemcacheX.Instance();
DataTable dt = mc.Get<DataTable>("dt");
if (dt != null)
{
sb.AppendLine("cache 中读取到"+dt.Rows.Count+"条记录<br>");
}
else
{
sb.AppendLine("cache 中没有读到数据<br>");
}
sb.AppendLine("---------------------------------<br>");
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