*****缓存存储配置项(读缓存和appsetting配直节点的问题 )*****
MemCache经常用于数据不用经常改动的数据 不用每次都去数据库中读取,可以防止在系统的缓存中,缓存在内存中读取速度快,但是memcache(是可以分布的缓存)没有保存机制,服务器挂掉,memcache丢失
系统的配置节放在了appsettings配置节下 <add key=”mingzi” value=”assas” />
ConfigurationManager.appsetting[“mingzi”];获取
CacheHelper
public class CaCheHelper
{
/// <summary>
/// 获取cache
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public static void set(string key, string value)
{
System.Web.Caching.Cache cache = HttpRuntime.Cache;
cache[key] = value;
}
/// <summary>
/// 设置cache
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static object Get(string key)
{
System.Web.Caching.Cache cache = HttpRuntime.Cache;
return cache[key];
}
/// <summary>
/// 移除cache
/// </summary>
/// <param name="key"></param>
public static void Remove(string key)
{
System.Web.Caching.Cache cache = HttpRuntime.Cache;
cache.Remove(key);
}
}
MemCacheHelper
private static readonly MemcachedClient mc = null;
static MemCacheHelper()
{
string[] serverlist = { "127.0.0.1:11211" }; //把ip和端口号定义在webConfig
//初始化池
SockIOPool pool = SockIOPool.GetInstance();
pool.SetServers(serverlist);
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();
// 获得客户端实例
mc = new MemcachedClient();
mc.EnableCompression = false;
}
/// <summary>
/// 设置
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public static void Set(string key, object value)
{
mc.Set(key, value);
}
/// <summary>
/// 获取缓存
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static object Get(string key)
{
return mc.Get(key);
}
/// <summary>
/// 设置缓存并且制定过期时间
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="dt"></param>
public static void Set(string key, object value, DateTime dt)
{
mc.Set(key, value, dt);
}
/// <summary>
/// 删除缓存
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static bool Delete(string key)
{
if (mc.KeyExists(key))
{
return mc.Delete(key);
}
return false;
}
}
//读取的时候先进行判断 如果缓存数据中有这个配置节那么就直接返回 如果没有这个配置节那么就从数据库中读取 并且返回 同时把从数据库中读取的数据放入缓存中
public string GetValue(string key)
{
//加上缓存依赖项 在更新数据的时候别忘了remove缓存
//object obj = Common.CaCheHelper.Get("setting_" + key);//第一次没有所以从数据库中取出
//if (obj == null)
//{
// var setting = this.DbSession.SettingsDal.LoadEntities(c => c.Name == key).FirstOrDefault();
// if (setting != null)
// {
// Common.CaCheHelper.set("setting_" + key, setting.Value);
// //读取出来数据后放入缓存
// //返回对应的值
// return setting.Value;
// }
//}
//return obj.ToString();
object o = BookShop.Common.MemCacheHelper.Get("setting_" + key);
if (o == null)
{
var setting = this.DbSession.SettingsDal.LoadEntities(c => c.Name == key).FirstOrDefault();
if (setting != null)
{
BookShop.Common.MemCacheHelper.Set("setting_" + key, setting.Value);
return setting.Value;
}
}
return o.ToString();
自定义缓存Memcache 以及Cache