Redis缓存、MemCached和.Net内部缓存的切换使用
接口文件:IDataCache.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace Untitls.Common
{
public interface IDataCache
{
/// <summary>
/// 获取缓存
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="key">缓存key</param>
/// <returns></returns>
T Get<T>(string key);
T Get<T>(string key,string depFile); /// <summary>
/// 写入缓存
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="key">缓存key</param>
/// <param name="value">缓存值</param>
/// <returns>返回值,表示:是否写入成功</returns>
bool Set<T>(string key, T value); /// <summary>
/// 写入缓存,设置过期时间点
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="key">缓存key</param>
/// <param name="value">缓存值</param>
/// <param name="expiresAt">过期时间点</param>
/// <returns>返回值,表示:是否写入成功</returns>
bool Set<T>(string key, T value, DateTime expiresAt); /// <summary>
/// 写入缓存,设置过期秒数
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="key">缓存key</param>
/// <param name="value">缓存值</param>
/// <param name="expiresSecond">过期秒数</param>
/// <returns>返回值,表示:是否写入成功</returns>
bool Set<T>(string key, T value, int expiresSecond); bool Set<T>(string key, T value, string depFile); /// <summary>
/// 删除缓存
/// </summary>
/// <param name="key">缓存key</param>
/// <returns></returns>
int Delete(string key); /// <summary>
/// 删除多个缓存
/// </summary>
/// <param name="keys">缓存key数组</param>
/// <returns></returns>
int Delete(string[] keys);
}
}
Reids实现文件:RedisCache.cs 注意 要改造成使用PooledRedisClientManager
using ServiceStack.Redis;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text; namespace Untitls.Common
{
/// <summary>
/// Redis缓存服务器
/// 服务器和客户端下载:
/// https://github.com/MSOpenTech/redis/releases
/// https://github.com/ServiceStack/ServiceStack.Redis
/// </summary>
public class RedisCache : IDataCache
{
private static RedisClient _redis = null;
public static RedisClient redis
{
get
{
if (_redis == null) _redis = new RedisClient("192.168.9.128", );//要开启服务器才能连接
return _redis;
}
} ~RedisCache()
{
//if (_redis != null) _redis.Shutdown();
} /// <summary>
/// 获取缓存
/// </summary>
/// <typeparam name="T">类型(对象必须可序列化,否则可以作为object类型取出再类型转换,不然会报错)</typeparam>
/// <param name="key">缓存key</param>
/// <returns></returns>
public T Get<T>(string key)
{
return redis.Get<T>(key);
}
public T Get<T>(string key, string depFile)
{
string timeKey = key + "_time";
if (redis.Exists(timeKey) > && redis.Exists(key) > )
{
DateTime obj_time = Get<DateTime>(timeKey);
T obj_cache = Get<T>(key);
if (File.Exists(depFile))
{
FileInfo fi = new FileInfo(depFile);
if (obj_time != fi.LastWriteTime)
{
Delete(key);
Delete(timeKey);
return default(T);
}
else return obj_cache;
}
else
{
throw new Exception("文件(" + depFile + ")不存在!");
}
}
else return default(T); } public bool Set<T>(string key, T value)
{
return redis.Set<T>(key, value);
}
public bool Set<T>(string key, T value, DateTime expiresAt)
{
return redis.Set<T>(key, value, expiresAt);
} public bool Set<T>(string key, T value, int expiresSecond)
{
return redis.Set<T>(key, value, DateTime.Now.AddSeconds(expiresSecond));
} public bool Set<T>(string key, T value, string depFile)
{
bool ret1 = redis.Set<T>(key, value);
if (ret1 && File.Exists(depFile))
{
FileInfo fi = new FileInfo(depFile);
DateTime lastWriteTime = fi.LastWriteTime;
return redis.Set<DateTime>(key + "_time", lastWriteTime);
}
return false;
} public int Delete(string key)
{
return redis.Del(key);
}
public int Delete(string[] keys)
{
return redis.Del(keys);
}
public void Dispose()
{
if (_redis != null) _redis.Shutdown();//调用Dispose释放memcached客户端连接
} }
}
实现文件:MemcachedCache.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Memcached.ClientLibrary;
using System.IO; namespace Untitls.Common
{
public class MemcachedCache:IDataCache
{
private MemcachedClient _mc =null;
protected MemcachedClient mc
{
get
{
if(_mc==null) _mc=new MemcachedClient();//初始化一个客户端
return _mc;
}
}
/// <summary>
/// 如果默认不是本地服务,可以额外指定memcached服务器地址
/// </summary>
public static string[] serverList
{
get;
set;
}
private static MemcachedCache _instance = null;
/// <summary>
/// 单例实例对象,外部只能通过MemcachedHelper.instance使用memcached
/// </summary>
public static MemcachedCache instance
{
get
{
if (_instance == null)
{
if (serverList != null && serverList.Length > )
_instance = new MemcachedCache(serverList);
else _instance = new MemcachedCache();
} return _instance;
}
}
SockIOPool pool;
private void start(params string[]servers)
{
string[] serverlist;
if (servers == null || servers.Length < )
{
serverlist = new string[] { "127.0.0.1:11011" }; //服务器列表,可多个
}
else
{
serverlist=servers;
}
pool = SockIOPool.GetInstance(); //根据实际情况修改下面参数
pool.SetServers(serverlist);
pool.InitConnections = ;
pool.MinConnections = ;
pool.MaxConnections = ;
pool.SocketConnectTimeout = ;
pool.SocketTimeout = ;
pool.MaintenanceSleep = ;
pool.Failover = true;
pool.Nagle = false;
pool.Initialize(); // initialize the pool for memcache servers
}
public MemcachedCache(string[] servers)
{
start(servers);
}
public MemcachedCache()
{
start();
}
~MemcachedCache()
{
//if (pool != null) pool.Shutdown();
} public T Get<T>(string key)
{
object data = mc.Get(key);
if (data is T) return (T)data;
else return default(T);
}
public T Get<T>(string key, string depFile)
{
string timeKey = key + "_time";
if (mc.KeyExists(timeKey) && mc.KeyExists(key))
{
DateTime obj_time = Get<DateTime>(timeKey);
T obj_cache = Get<T>(key);
if (File.Exists(depFile))
{
FileInfo fi = new FileInfo(depFile);
if (obj_time != fi.LastWriteTime)
{
Delete(key);
Delete(timeKey);
return default(T);
}
else return obj_cache;
}
else
{
throw new Exception("文件(" + depFile + ")不存在!");
}
}
else return default(T); }
public bool Set<T>(string key, T value)
{
return mc.Set(key, value);
}
public bool Set<T>(string key, T value, DateTime expiresAt)
{
return mc.Set(key, value, expiresAt);
} public bool Set<T>(string key, T value, int expiresSecond)
{
return mc.Set(key, value, DateTime.Now.AddSeconds(expiresSecond));
} public bool Set<T>(string key, T value, string depFile)
{
bool ret1 = mc.Set(key, value);
if (ret1 && File.Exists(depFile))
{
FileInfo fi = new FileInfo(depFile);
DateTime lastWriteTime = fi.LastWriteTime;
return mc.Set(key + "_time", lastWriteTime);
}
return false;
} public int Delete(string key)
{
return mc.Delete(key)?:;
}
public int Delete(string[] keys)
{
int i = ;
foreach (var key in keys)
{
mc.Delete(key);
i++;
}
return i;
}
//在应用程序退出之前,调用Dispose释放memcached客户端连接
public void Dispose()
{
if (pool != null) pool.Shutdown();
}
}
}
实现文件:RuntimeCache.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Caching; namespace Untitls.Common
{
public class RuntimeCache : IDataCache
{ #region 删除缓存
/// <summary>
/// 删除缓存
/// </summary>
/// <param name="CacheKey">键</param>
public int Delete(string CacheKey)
{
HttpRuntime.Cache.Remove(CacheKey);
return ;
}
public int Delete(string[] CacheKeys)
{
int i = ;
foreach (var key in CacheKeys)
{
HttpRuntime.Cache.Remove(key);
i++;
}
return i;
}
#endregion #region 获取缓存,依赖时间
/// <summary>
/// 获取缓存,依赖时间
/// </summary>
/// <param name="CacheKey">键</param>
/// <returns></returns>
public T Get<T>(string CacheKey)
{
object obj_time = HttpRuntime.Cache[CacheKey + "_time"];
object obj_cache = HttpRuntime.Cache[CacheKey];
if (obj_time != null && obj_cache != null)
{
if (Convert.ToDateTime(obj_time) < DateTime.Now)
{
Delete(CacheKey);
Delete(CacheKey + "_time");
return default(T);
}
else return (T)obj_cache;
}
else
{
Delete(CacheKey);
Delete(CacheKey + "_time");
return default(T);
}
}
#endregion #region 获取缓存,依赖文件
/// <summary>
/// 获取缓存,依赖文件
/// </summary>
/// <param name="CacheKey">键</param>
/// <param name="depFile">依赖的文件</param>
/// <returns></returns>
public T Get<T>(string CacheKey, string depFile)
{
object obj_time = HttpRuntime.Cache[CacheKey + "_time"];
object obj_cache = HttpRuntime.Cache[CacheKey];
if (File.Exists(depFile))
{
FileInfo fi = new FileInfo(depFile); if (obj_time != null && obj_cache != null)
{
if (Convert.ToDateTime(obj_time) != fi.LastWriteTime)
{
Delete(CacheKey);
Delete(CacheKey + "_time");
return default(T);
}
else return (T)obj_cache;
}
else
{
Delete(CacheKey);
Delete(CacheKey + "_time");
return default(T);
}
}
else
{
throw new Exception("文件(" + depFile + ")不存在!");
}
}
#endregion #region 简单的插入缓存
/// <summary>
/// 简单的插入缓存
/// </summary>
/// <param name="CacheKey">键</param>
/// <param name="objObject">数据</param>
public bool Set<T>(string CacheKey, T objObject)
{
HttpRuntime.Cache.Insert(CacheKey, objObject);
return true;
}
#endregion #region 有过期时间的插入缓存数据
public bool Set<T>(string CacheKey, T objObject, DateTime expiresAt)
{
HttpRuntime.Cache.Insert(CacheKey, objObject, null, expiresAt, Cache.NoSlidingExpiration);
HttpRuntime.Cache.Insert(CacheKey + "_time", expiresAt, null, expiresAt, Cache.NoSlidingExpiration);//存储过期时间
return true;
}
#endregion #region 插入缓存数据,指定缓存多少秒
public bool Set<T>(string CacheKey, T objObject, int seconds)
{
DateTime expiresAt = DateTime.Now.AddSeconds(seconds);
HttpRuntime.Cache.Insert(CacheKey, objObject, null, expiresAt, Cache.NoSlidingExpiration);
HttpRuntime.Cache.Insert(CacheKey + "_time", expiresAt, null, expiresAt, Cache.NoSlidingExpiration);//存储过期时间
return true;
}
#endregion #region 依赖文件的缓存,文件没改不会过期
/// <summary>
/// 依赖文件的缓存,文件没改不会过期
/// </summary>
/// <param name="CacheKey">键</param>
/// <param name="objObject">数据</param>
/// <param name="depfilename">依赖文件,可调用 DataCache 里的变量</param>
public bool Set<T>(string CacheKey, T objObject, string depfilename)
{
//缓存依赖对象
System.Web.Caching.CacheDependency dep = new System.Web.Caching.CacheDependency(depfilename);
DateTime absoluteExpiration = System.Web.Caching.Cache.NoAbsoluteExpiration;
TimeSpan slidingExpiration = System.Web.Caching.Cache.NoSlidingExpiration;
System.Web.Caching.Cache objCache = HttpRuntime.Cache;
objCache.Insert(
CacheKey,
objObject,
dep,
absoluteExpiration, //从不过期
slidingExpiration, //禁用可调过期
System.Web.Caching.CacheItemPriority.Default,
null);
if (File.Exists(depfilename))
{
FileInfo fi = new FileInfo(depfilename);
DateTime lastWriteTime = fi.LastWriteTime;
HttpRuntime.Cache.Insert(CacheKey + "_time", lastWriteTime, null, absoluteExpiration, slidingExpiration);//存储文件最后修改时间
}
return true;
}
#endregion
}
}
缓存统一入口
public partial class DataCache
{
private static IDataCache _instance = null;
/// <summary>
/// 静态实例,外部可直接调用
/// </summary>
public static IDataCache Instance
{
get
{
if (_instance == null) _instance = new RuntimeCache();//这里可以改变缓存类型
return _instance;
}
}
}
缓存在分布式应用中有很多好处,大家可以合理利用。
Redis缓存、MemCached和.Net内部缓存的切换使用的更多相关文章
- NET分布式缓存Memcached测试体验
原文地址:http://onlyonewt.blog.sohu.com/160168896.html 一直在学习关注大访问量网站的缓存是如何实现,之前看过Memcached的资料,忙于没有时间来真正测 ...
- 缓存 memcached 与 redis
Memcached Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度 ...
- 面试题:缓存Redis与Memcached的比较 有用
Memcached是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载. 它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提供动态.数据库驱动网站的速度. Memca ...
- 分布式缓存技术PK:选择Redis还是Memcached?
作者:田京昆(腾讯后台研发工程师) 来源:腾云阁(https://www.qcloud.com/community/article/129) Memcached和Redis,作为近些年最常用的缓存服务 ...
- redis和memcached缓存
memcached memcache开源的,高性能,高并发分布式内存缓存系统,天生支持集群 memcached下载地址: http://memcached.org/downloads python实现 ...
- 网站缓存技术(Redis、Memcached、Ehcache)
Redis 是什么? 通常而言目前的数据库分类有几种,包括 SQL/NSQL,,关系数据库,键值数据库等等等. 分类的标准也不一,Redis本质上也是一种键值数据库的,但它在保持键值数据库简单快捷特点 ...
- ASP.Net MVC4+Memcached+CodeFirst实现分布式缓存
ASP.Net MVC4+Memcached+CodeFirst实现分布式缓存 part 1:给我点时间,允许我感慨一下2016年 正好有时间,总结一下最近使用的一些技术,也算是为2016年画上一个完 ...
- 第八章 企业项目开发--分布式缓存memcached
注意:本节代码基于<第七章 企业项目开发--本地缓存guava cache> 1.本地缓存的问题 本地缓存速度一开始高于分布式缓存,但是随着其缓存数量的增加,所占内存越来越大,系统运行内存 ...
- 分布式内存对象缓存 memcached
分布式内存对象缓存 许多Web 应用程序都将数据保存到RDBMS中,应用服务器从中读取数据并在浏览器中显示.但随着数据量的增大,访问的集中,就会出现REBMS的负担加重,数据库响应恶化,网站显示延迟等 ...
随机推荐
- unigui MessageDlg方法调用例子
procedure TfrmEmployee.btnDeleteClick(Sender: TObject);var aBool: Boolean;begin inherited; MessageDl ...
- 安装Sass
最近要开始用 Sass 做一些东西.先来记录一下安装过程. 1.确认本机的 Ruby 版本 2.访问网址下载 Sass 最新版本 https://rubygems.org/gems/sass 3.下载 ...
- C#图解教程读书笔记(第1章 C#和.net框架)
C#中的主要需要记住的基础概念 CLR公共语言运行库 CIL中间语言,所有的代码都会编译成中间语言. CLI公共语言基础结构 C#的优点 C#有自动垃圾回收机制
- date之Hi时间的思考
工作中用到需要一个判断当前时间是否在 23:50到1:00之间的一段程序,在和别人的讨论中基本上有以下两种做法 1.分别获取时分进行判断和比较 <?php function check_time ...
- css3 前缀
- perl学习笔记(4)——动态加载
在写perl的时候,如果要应用到各种平台的话,比如linux 和windows,会遇到各种问题,有时就是要根据系统类型来加载各种库,之前写的就是这样的, if($^O eq 'linux'){ use ...
- PHPCMS教程
第一章 模版 参见:http://blog.163.com/zh_astro/blog/static/1842084562011430430419/ 碎片管理: 在模版页面需要添加碎片的位置加上代码{ ...
- jQuery:在一个回调中处理多个请求
我曾经为Mozilla Developer Network 开发一个新功能,它需要加载一个基本的脚本文件的同时加载一个JSON请求.因为我们使用的是jQuery,意味着要使用 jQuery.getSc ...
- poj 3635 Full Tank? ( 图上dp )
题意: 已知每一个点的加油站的油价单位价格(即点权).每条路的长度(边权). 有q个询问.每一个询问包含起点s.终点e和油箱容量. 问从起点走到终点的最小花费.假设不可达输出impossible,否则 ...
- 【剑指offer】递归循环两种方式反转链表
转载请注明出处:http://blog.csdn.net/ns_code/article/details/25737023 本文分别用非递归和递归两种方式实现了链表的反转,在九度OJ上AC. 题目描写 ...