MyCache.cs
using System;
using System.Web;
using System.Data;
using System.Web.Caching;
using System.Collections;
using Memcached.ClientLibrary;
/**//// <summary>
/// 搜集编写者losingrose
/// QQ:303864496
/// MSN:losingrose@21cn.com
/// http://losingrose.cnblogs.com
/// Cache操作类
/// </summary>
public static class MyCache
{
/**//*
DelegateLists.string_2fill del = new DelegateLists.string_2fill(bll.TopDormCo_Str);
object[] prams = new object[2];
prams[0] = "Products.aspx";
prams[1] = "15";
dorms.Text = MyCache.Cache_Select("Dorms" + MySecurity.QueryString("coid"), 0, del, prams).ToString();
*/
static Cache cache = HttpContext.Current.Cache;
public static bool Memcached = bool.Parse(System.Web.Configuration.WebConfigurationManager.AppSettings["Memcached"]);
static MemcachedClient mc = new MemcachedClient();
static double default_buffminutes = Convert.ToDouble(System.Web.Configuration.WebConfigurationManager.AppSettings["default_buffminutes"]);
//static double default_buffminutes = 5;
static MyCache()
{
//判断是否启动Memcached
if (Memcached)
{
string server = System.Web.Configuration.WebConfigurationManager.AppSettings["MemServer"];
String[] serverlist ={ server };
// initialize the pool for memcache servers
SockIOPool pool = SockIOPool.GetInstance("shenghuohui");
pool.SetServers(serverlist);
pool.Initialize();
mc = new MemcachedClient();
mc.PoolName = "shenghuohui";
mc.EnableCompression = false;
}
}
/**//// <summary>
/// 取得目前Cache列表
/// </summary>
/// <returns></returns>
public static string[] Cache_List()
{
string[] str = new string[cache.Count];
int i = 0;
foreach (DictionaryEntry item in cache)
{
str = item.Key.ToString();
i++;
}
return str;
}
/**//// <summary>
/// 添加一个新的Cache对象
/// </summary>
/// <param name="key">键名</param>
/// <param name="value">待保存的值</param>
/// <param name="buffminutes">缓存时间(按分钟计),为0则读取配置文件设定的时间</param>
public static void Cache_Insert(string key, object value, double buffminutes)
{
if (Memcached)
{
if (buffminutes == 0)
buffminutes = default_buffminutes;
mc.Set(key, value,DateTime.Now.AddMinutes(buffminutes));
}
else
{
if (buffminutes == 0)
buffminutes = default_buffminutes;
cache.Insert(key, value, null, DateTime.Now.AddMinutes(buffminutes), TimeSpan.Zero);
}
}
/**//// <summary>
/// 移出一个Cache对象
/// </summary>
/// <param name="key">键名</param>
public static void Cache_Remove(string key)
{
if (Memcached)
{
mc.Delete(key);
}
else
{
cache.Remove(key);
}
}
/**//// <summary>
/// 获取Cache对象,如果对象不存在,则调用相关事件委托填充并返回Cache的值。
/// </summary>
/// <param name="key">键名</param>
/// <param name="buffminutes">缓存时间(按分钟计),为0则读取配置文件设定的时间</param>
/// <param name="method">填充的方法委托</param>
/// <param name="parms">方法所需要的参数列表</param>
/// <returns>返回Cache的值</returns>
public static object Cache_Select(string key, double buffminutes, Delegate method, params object[] parms)
{
object x;
if (Memcached)
{
x=mc.Get(key);
if(x==null)
{
if (buffminutes == 0)
buffminutes = default_buffminutes;
object o = method.DynamicInvoke(parms);
if (o != null)
{
Cache_Insert(key, o, buffminutes);
x = o;
}
else
return method.DynamicInvoke(parms);
}
}
else
{
x = cache.Get(key);
if (x == null)
{
if (buffminutes == 0)
buffminutes = default_buffminutes;
object o = method.DynamicInvoke(parms);
if (o != null)
{
Cache_Insert(key, o, buffminutes);
x = o;
}
else
return method.DynamicInvoke(parms);
}
}
return x;
}
/**//// <summary>
/// 直接获取Cache
/// </summary>
/// <param name="key">键名</param>
/// <returns></returns>
public static object Cache_Select(string key)
{
if (Memcached)
return mc.Get(key);
else
return cache.Get(key);
}
/**//// <summary>
/// 清楚所有Cache对象
/// </summary>
public static void Cache_Clear()
{
if (Memcached)
mc.FlushAll();
else
{
foreach (DictionaryEntry item in cache)
{
cache.Remove(item.Key.ToString());
}
}
}
}
DelegateLists.cs
using System;
using System.Web;
using System.Data;
using System.Collections;
/**//// <summary>
/// 搜集编写者losingrose
/// QQ:303864496
/// MSN:losingrose@21cn.com
/// http://losingrose.cnblogs.com
/// 委托方法定义
/// </summary>
public class DelegateLists
{
public delegate DataTable dataset_0fill();
public delegate DataTable dataset_1fill(string a);
public delegate DataTable dataset_2fill(string a, string b);
public delegate DataTable dataset_3fill(string a, string b, string c);
public delegate DataTable dataset_4fill(string a, string b, string c, string d);
public delegate string string_0fill();
public delegate string string_1fill(string a);
public delegate string string_2fill(string a, string b);
public delegate string string_3fill(string a, string b, string c);
public delegate string string_4fill(string a, string b, string c, string d);
public delegate object alluser(int userType, int userId);
User Methods#region User Methods
public delegate UserInfo userinfo(int userid);
public delegate ShopInfo shopinfo(int shopid);
#endregion
Shop Methods#region Shop Methods
public delegate DataTable GetAllReCommendKind();
#endregion
}