Redis作为缓存服务器
1、ICache的Redis实现没有放在'Framework.Cache/Logic'中。如果是以前,我会认为这样不好。我会这样做,'Framework.Cache'项目引用Redis项目或直接从Nuget安装Redis,
把实现定义在‘Framework.Cache/Logic’中,这样结构看起来很顺眼,‘这算是高内聚么!!!’。不过自从接触了IOC后,现在这样的结构似乎也很合理,没什么违和感。
这就好似把‘IRepository’和‘Repositories’,一个放在Domain,一个放在Infrastructure
2、当前比较稳定的Redis客户端(开源的程序包)有ServiceStack.Redis 和 StackExchange.Redis。我都用了一下,ServiceStatck的
比较好用,不过我感觉后者的性能应该会稍好点
3、关于ServiceStack.Redis中的接口,IRedisClient,ICacheClient,IRedisTypedClient<T>,IEntityStore<T>,IEntityStore。
这些“乱七八糟”的接口和CRUD都有关系,中的有些方法看似好像是重复的,后续希望能整理出一篇结合源码,理论点的文章
一、Framework.Cache
接口ICache,定义缓存操作开放的方法
public interface ICache
{
/// <summary>
/// Gets all entries in the cache
/// </summary>
IEnumerable<KeyValuePair<string, object>> Entries { get; } /// <summary>
/// Gets a cache item associated with the specified key or adds the item
/// if it doesn't exist in the cache.
/// </summary>
/// <typeparam name="T">The type of the item to get or add</typeparam>
/// <param name="key">The cache item key</param>
/// <param name="baseMethod">Func which returns value to be added to the cache</param>
/// <returns>Cached item value</returns>
T Get<T>(string key, Func<T> baseMethod); /// <summary>
/// Gets a cache item associated with the specified key or adds the item
/// if it doesn't exist in the cache.
/// </summary>
/// <typeparam name="T">The type of the item to get or add</typeparam>
/// <param name="key">The cache item key</param>
/// <param name="baseMethod">Func which returns value to be added to the cache</param>
/// <param name="cacheTime">Expiration time in minutes</param>
/// <returns>Cached item value</returns>
T Get<T>(string key, Func<T> baseMethod, int cacheTime); /// <summary>
/// Gets a value indicating whether an item associated with the specified key exists in the cache
/// </summary>
/// <param name="key">key</param>
/// <returns>Result</returns>
bool Contains(string key); /// <summary>
/// Removes the value with the specified key from the cache
/// </summary>
/// <param name="key">/key</param>
void Remove(string key);
}
二、基于‘ServiceStack.Redis’的缓存实现
public class SSRedisCache : ICache
{
private const string REGION_NAME = "$#SSRedisCache#$";
private const int _DefaultCacheTime = ;
private readonly static object s_lock = new object(); private IRedisClient GetClient()
{
return RedisManager.GetClient();
} private IRedisClient GetReadOnlyClient()
{
return RedisManager.GetReadOnlyClient();
} public IEnumerable<KeyValuePair<string, object>> Entries
{
get { throw new NotImplementedException(); }
} public T Get<T>(string key, Func<T> baseMethod)
{
return Get<T>(key, baseMethod, _DefaultCacheTime);
} public T Get<T>(string key, Func<T> baseMethod, int cacheTime)
{
using (var redisClient = GetClient())
{
key = BuildKey(key); if (redisClient.ContainsKey(key))
{
return redisClient.Get<T>(key);
}
else
{
lock (s_lock)
{
if (!redisClient.ContainsKey(key))
{
var value = baseMethod();
if (value != null) //请区别null与String.Empty
{
redisClient.Set<T>(key, value, TimeSpan.FromMinutes(cacheTime));
//redisClient.Save();
}
return value;
}
return redisClient.Get<T>(key);
}
}
}
} public bool Contains(string key)
{
using (var redisClient = GetReadOnlyClient())
{
return redisClient.ContainsKey(BuildKey(key));
}
} public void Remove(string key)
{
using (var redisClient = GetClient())
{
redisClient.Remove(BuildKey(key));
}
} private string BuildKey(string key)
{
return string.IsNullOrEmpty(key) ? null : REGION_NAME + key;
} }
三、基于‘StackExchange.Redis’的缓存实现
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="MsgPack.Cli" version="0.6.8" targetFramework="net45" />
<package id="StackExchange.Redis" version="1.0.488" targetFramework="net45" />
<package id="StackExchange.Redis.Extensions.Core" version="1.3.2.0" targetFramework="net45" />
<package id="StackExchange.Redis.Extensions.MsgPack" version="1.3.2.0" targetFramework="net45" />
</packages>
public class SERedisCache : ICache
{
private const string REGION_NAME = "$#SERedisCache#$";
private const int _DefaultCacheTime = ;
private readonly static object s_lock = new object(); private StackExchangeRedisCacheClient GetClient()
{
return new StackExchangeRedisCacheClient(RedisServer.Connection, new MsgPackObjectSerializer());
} public IEnumerable<KeyValuePair<string, object>> Entries
{
get { throw new NotImplementedException(); }
} public T Get<T>(string key, Func<T> baseMethod)
{
return Get(key, baseMethod, _DefaultCacheTime);
} public T Get<T>(string key, Func<T> baseMethod, int cacheTime)
{
using (var cacheClient = GetClient())
{
key = BuildKey(key); if (cacheClient.Exists(key))
{
return cacheClient.Get<T>(key);
}
else
{
lock (s_lock)
{
if (!cacheClient.Exists(key))
{
var value = baseMethod();
if (value != null) //请区别null与String.Empty
{
cacheClient.Add<T>(key, value, TimeSpan.FromMinutes(cacheTime));
}
return value;
}
return cacheClient.Get<T>(key);
}
}
}
} public bool Contains(string key)
{
using (var cacheClient = GetClient())
{
return cacheClient.Exists(BuildKey(key));
}
} public void Remove(string key)
{
using (var cacheClient = GetClient())
{
cacheClient.Remove(BuildKey(key));
}
} private string BuildKey(string key)
{
return string.IsNullOrEmpty(key) ? null : REGION_NAME + key;
} }
完整代码(稍后) 会在另一篇文章关于Web API中附上
Redis作为缓存服务器的更多相关文章
- HAProxy 的负载均衡服务器,Redis 的缓存服务器
问答社区网络 StackExchange 由 100 多个网站构成,其中包括了 Alexa 排名第 54 的 StackOverflow.StackExchang 有 400 万用户,每月 5.6 亿 ...
- Redis 作为缓存服务器的配置
随着redis的发展,越来越多的架构用它取代了memcached作为缓存服务器的角色,它有几个很突出的特点:1. 除了Hash,还提供了Sorted Set, List等数据结构2. 可以持久化到磁盘 ...
- 用Redis作为缓存服务器,加快数据库操作速度
https://zh.wikipedia.org/wiki/Redis http://www.jianshu.com/p/01b37cdb3f33
- Windows下Redis缓存服务器的使用 .NET StackExchange.Redis Redis Desktop Manager
Redis缓存服务器是一款key/value数据库,读110000次/s,写81000次/s,因为是内存操作所以速度飞快,常见用法是存用户token.短信验证码等 官网显示Redis本身并没有Wind ...
- Django分析之使用redis缓存服务器
时间长没有更新了,这段时间一直忙着一个项目,今天就记录一个现在经常会用到的技术吧. redis相信大家都很熟悉了,和memcached一样是一个高性能的key-value数据库,至于什么是缓存服务器, ...
- redis作为mysql的缓存服务器(读写分离,通过mysql触发器实现数据同步)
一.redis简介Redis是一个key-value存储系统.和Memcached类似,为了保证效率,数据都是缓存在内存中.区别的是redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录 ...
- redis来共享各个服务器的session,并同时通过redis来缓存一些常用的资源,加快用户获得请求资源的速度(转)
时间过得真快,再次登录博客园来写博,才发现距离上次的写博时间已经过去了一个月了,虽然是因为自己找了实习,但这也说明自己对时间的掌控能力还是没那么的强,哈哈,看来还需不断的努力啊!(这里得特别说明一下本 ...
- redis作为mysql的缓存服务器(读写分离) (转)
一.redis简介Redis是一个key-value存储系统.和Memcached类似,为了保证效率,数据都是缓存在内存中.区别的是redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录 ...
- Windows下Redis缓存服务器的使用 .NET StackExchange.Redis Redis Desktop Manager 转发非原创
Windows下Redis缓存服务器的使用 .NET StackExchange.Redis Redis Desktop Manager Redis缓存服务器是一款key/value数据库,读11 ...
随机推荐
- 呵呵sql
INSERT INTO fnd_document_folder_structure_t (folder_name,parent_folder_id,company_type_id,inv_flag, ...
- Mac上获取文件md5 值
mac 上获取一个文件的md5值如下 在terminal 上输入下面命令行即可: 方法一: //备注 AccountPassword/check 是全路径 也可以相对路径,我这里是相对路径,用来测试用 ...
- Appium 定位方法例子(4)
有朋友留言反应定位不到元素,没错,船长也为这个一直在头疼,我用的App是原生安卓+webService+h5类型的,定位虽然没问题,但是在进行操作的时候各种不通过……真的很头疼啊……我这里说的“操作” ...
- 最最基本的SQL常用命令
2015-12-01 18:08:52 1.启动/关闭mysql 开始菜单搜索cmd,右击,以管理员身份运行,输入net start mysql启动mysql,输入net stop mysql关闭my ...
- 解决openoffice进程异常退出的办法:
实现以守护进程,定时检测openoffice是否退出,如果进程不存在,通过脚本将openoffice起起来即可. 具体操作步骤: 第一步: 将openoffice.sh脚本放置在root目录下面, ...
- python-生成测试报告-然后自动发送邮件
前两篇单独介绍了生成测试报告和自动发送邮件,那么现在把两者整合到一起:生成测试报告后然后自动发送邮件,这里只是简单的整合实现功能,其实还可以优化的,先用吧,后面再慢慢优化 先看下目录,其实目录还是一样 ...
- Python——深浅Copy
1. 赋值 赋值:指向同一块内存地址,所以同时改变 l1 = [1,2,3] l2 = l1 l1.append('a') print(l1,l2) # [1, 2, 3, 'a'] [1, 2, 3 ...
- elastic search6.2.2 实现用户搜索记录查询(去重、排序)
elastic search6.2.2 实现搜索记录查询 ,类似新浪微博这种,同样的搜索记录后面时间点的会覆盖前面的(主要思路:关键词去重,然后按时间排序) 先创建索引 //我的搜索 PUT my_s ...
- wdlinux中apache配置反向代理模块
想要在.htaccess中开启反向代理功能都不行[apache中没有mod_proxy模块] .htaccess 文件内容如下 RewriteEngine On RewriteBase / Rewri ...
- 论BOM管理的若干重要问题
在制造业信息化建设过程中,BOM管理的意义已经被广泛认同与重视.BOM管理是企业产品数据管理领域要解决的核心问题,对制造企业的信息流和业务流程具有重要影响,可以说企业BOM数据架构已是企业架构的一部分 ...