集成环境:
系统环境:wind32
开发环境:VS2010 .Net Framework C#
memcached客户端
客户端: http://code.google.com/p/memcached/wiki/ClientsC# 下可用的API(每个客户端API中都有详细的说明和注释)
初始化SockIOPool
SockIOPool是Memcached客户端提供的一个套接字连接池,通俗讲,就是与Memcached服务器端交换数据的对象。SockIOPool在应用程序启动时初始化一次就可以了
(一般写在 静太构造 里就行了)
//服务器端列表
string[] serverlist = { "127.0.0.1:11211" };
//初始化池
SockIOPool sock = SockIOPool.GetInstance();
sock.SetServers(serverlist);//添加服务器列表
sock.InitConnections = 3;//设置连接池初始数目
sock.MinConnections = 3;//设置最小连接数目
sock.MaxConnections = 5;//设置最大连接数目
sock.SocketConnectTimeout = 1000;//设置连接的套接字超时。
sock.SocketTimeout = 3000;//设置套接字超时读取
sock.MaintenanceSleep = 30;//设置维护线程运行的睡眠时间。如果设置为0,那么维护线程将不会启动;
//获取或设置池的故障标志。
//如果这个标志被设置为true则socket连接失败,
//将试图从另一台服务器返回一个套接字如果存在的话。
//如果设置为false,则得到一个套接字如果存在的话。否则返回NULL,如果它无法连接到请求的服务器。
sock.Failover = true; //如果为false,对所有创建的套接字关闭Nagle的算法。
sock.Nagle = false;
sock.Initialize();
public bool Add(string key, object value);
public bool Add(string key, object value, DateTime expiry);
public bool Add(string key, object value, int hashCode);
public bool Add(string key, object value, DateTime expiry, int hashCode);
public long Decrement(string key);
public long Decrement(string key, long inc);
public long Decrement(string key, long inc, int hashCode);
public bool Delete(string key);
public bool Delete(string key, DateTime expiry);
public bool Delete(string key, object hashCode, DateTime expiry);
public bool FlushAll();
public bool FlushAll(ArrayList servers);
public object Get(string key);
public object Get(string key, int hashCode);
public object Get(string key, object hashCode, bool asString);
public long GetCounter(string key);
public long GetCounter(string key, object hashCode);
public Hashtable GetMultiple(string[] keys);
public Hashtable GetMultiple(string[] keys, int[] hashCodes);
public Hashtable GetMultiple(string[] keys, int[] hashCodes, bool asString);
public object[] GetMultipleArray(string[] keys);
public object[] GetMultipleArray(string[] keys, int[] hashCodes);
public object[] GetMultipleArray(string[] keys, int[] hashCodes, bool asString);
public long Increment(string key);
public long Increment(string key, long inc);
public long Increment(string key, long inc, int hashCode);
public bool KeyExists(string key);
public bool Replace(string key, object value);
public bool Replace(string key, object value, DateTime expiry);
public bool Replace(string key, object value, int hashCode);
public bool Replace(string key, object value, DateTime expiry, int hashCode);
public bool Set(string key, object value);
public bool Set(string key, object value, DateTime expiry);
public bool Set(string key, object value, int hashCode);
public bool Set(string key, object value, DateTime expiry, int hashCode);
public Hashtable Stats();
public Hashtable Stats(ArrayList servers);
public bool StoreCounter(string key, long counter);
public bool StoreCounter(string key, long counter, int hashCode);
集成
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Threading;
using Memcached.ClientLibrary;
namespace Common
{
/// <summary>
/// 数据参数
/// </summary>
public class DataParameter
{
/// <summary>
/// 数据名称
/// </summary>
public string DataName { get; set; }
/// <summary>
/// 数据类型
/// </summary>
public DbType DataType { get; set; }
/// <summary>
/// 数据值
/// </summary>
public object DataValue { get; set; }
}
/// <summary>
/// 数据访问处理器
/// </summary>
public class DataHelper
{
protected const string ConnectString = "BizConnectString";
private static MemcachedClient mc = new MemcachedClient();//初始化一个客户端
static DataHelper()
{
string[] serverlist = ConfigUtil.GetString("MemcachedServerIPAndPort", "192.168.79.164:11211").Split('|'); //服务器列表,可多个
SockIOPool pool = SockIOPool.GetInstance();
//根据实际情况修改下面参数
pool.SetServers(serverlist);
pool.InitConnections = 3;
pool.MinConnections = 3;
pool.MaxConnections = 500;
pool.SocketConnectTimeout = 1000;
pool.SocketTimeout = 3000;
pool.MaintenanceSleep = 30;
pool.Failover = true;
pool.Nagle = false;
pool.Initialize(); // initialize the pool for memcache servers
}
#region 通过数据缓存非查询SQL语句执行
/// <summary>
/// 执行非查询SQL语句
/// </summary>
/// <param name="procedureName">存储过程名称</param>
public static DataSet ExecuteProcedureDataSetByMemCache(string procedureName, DateTime expiry)
{
return ExecuteProcedureDataSetByMemCache(procedureName, null, ConnectString, expiry);
}
/// <summary>
/// 执行非查询SQL语句
/// </summary>
/// <param name="procedureName">存储过程名称</param>
/// <param name="connectString">数据库连接串</param>
public static DataSet ExecuteProcedureDataSetByMemCache(string procedureName, string connectString
, DateTime expiry)
{
return ExecuteProcedureDataSetByMemCache(procedureName, null, connectString, expiry);
}
/// <summary>
/// 执行非查询SQL语句
/// </summary>
/// <param name="procedureName">存储过程名称</param>
/// <param name="paras">存储过程参数</param>
public static DataSet ExecuteProcedureDataSetByMemCache(string procedureName, DataParameter[] paras
, DateTime expiry)
{
return ExecuteProcedureDataSetByMemCache(procedureName, paras, ConnectString, expiry);
}
/// <summary>
/// 执行非查询SQL语句
/// </summary>
/// <param name="procedureName">存储过程名称</param>
/// <param name="paras">存储过程参数</param>
/// <param name="connectString">数据库连接串</param>
public static DataSet ExecuteProcedureDataSetByMemCache(string procedureName, DataParameter[] paras
, string connectString, DateTime expiry)
{
StringBuilder keys = new StringBuilder();
keys.Append(procedureName);
if (paras != null)
{
for (int i = 0; i < paras.Length; i++)
{
keys.AppendFormat("{0}={1}|", paras[i].DataName, paras[i].DataValue);
Thread.Sleep(0);
}
}
DataSet ds = (DataSet)mc.Get(keys.ToString());
if (ds != null)
{
return ds;
}
Database db = DatabaseFactory.CreateDatabase(connectString);
DbCommand cmd = db.GetStoredProcCommand(procedureName);
if (paras != null)
{
for (int i = 0; i < paras.Length; i++)
{
db.AddInParameter(cmd, paras[i].DataName, paras[i].DataType, ReplaceNull(paras[i].DataValue));
Thread.Sleep(0);
}
}
ds = db.ExecuteDataSet(cmd);
mc.Set(keys.ToString(), ds);
return ds;
}
#endregion
}
}