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

最新文章

  1. POJ1026 Cipher(置换的幂运算)
  2. 《IT蓝豹》PlayNewsStandDemo资讯类新闻客户端框架
  3. 常用mimetype
  4. IOS实现小型计算器
  5. Ubuntu安装Adobe Reader
  6. C#反射 入门学习 01
  7. Srping - bean的依赖注入(Dependency injection)
  8. 前端时间戳timestamp相关总结:
  9. [Luogu2617]Dynamic Ranking
  10. Java9相关资料(JShell简易教程等)
  11. jsp自动编译机制
  12. Djiango初识
  13. [LeetCode] 74. Search a 2D Matrix_Medium tag: Binary Search
  14. python urllib和urllib3包使用(转载于)
  15. 《CDN 之我见》原理篇——CDN的由来与调度
  16. shell 变量定义使用
  17. 【openjudge】 CDQZ challenge 4
  18. 如何查询端口号和网址的ip地址?
  19. Windows Server 2012 R2搭建IIS服务器
  20. 【MATLAB】读取和写入文本文件

热门文章

  1. 如何测试远端TCP和UDP端口是否开放
  2. think python chapter2
  3. Angular5学习笔记 - 集成Bootstrap、Jquery、Tether(三)
  4. 自然语言处理--nltk安装及wordnet使用详解
  5. idea右键单击没有 svn选项处理办法
  6. 第十课 go语言函数
  7. How to clear fmadm log or FMA faults log (ZT)
  8. 类型:Jquery;问题:jquery调用后台带参数方法;结果:利用JQuery的$.ajax()可以很方便的调用asp.net的后台方法。
  9. Linux系统下Oracle执行SQL脚本后中文出现乱码解决方法
  10. xcode 编译错误找不到 libz.dylib