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 ...
随机推荐
- Cookie简单实例
Cookie简单实例 1.创建CookieServlet package com.servlet.study; import java.io.IOException; import java.io.P ...
- iOS设备是否越狱的判断代码
苹果是非常看重产品的安全性的,所以给用户设计了一套复杂的安全机制.这让喜爱自由,崇尚一切开放的程序员们极度不爽,于是越狱就成了苹果和黑客们反复斗法的场所.总体来说,越狱可以让我们随意安装.共享应用,但 ...
- travis-cli 使用
1. 添加项目 登录 travis 选择对应项目即可 2. 添加持续集成文件 .travis.yml language: node_js node_js: - "node" bef ...
- 织梦DedeCMS模板通用安装方法
在网络上有很多可以免费下载的DEDECMS模板,下载之后如何安装是很多新手的共同问题.下面将结合我个人使用DEDECMS模板的一些心得,讲一下DEDE模板安装的通用方法. 1.下载一个模板之后要判断一 ...
- 使用模块化编译缩小 apk 体积
libcocos2dlua.so编译出来有11M多,其中包含了很多不需要的模块,模块化编译,把不需要用到的模块弄成0,体积就小了. 如: 修改D:\codeide\sDiShu2formm\frame ...
- c++重在运算符
运算符函数的定义与其他函数的定义类似,惟一的区别是运算符函数的函数名是由关键字operator和其后要重载的运算符符号构成的.运算符函数定义的一般格式如下: <返回类型说明符> opera ...
- 【Python教程】《零基础入门学习Python》(小甲鱼)
[Python教程]<零基础入门学习Python>(小甲鱼) 讲解通俗易懂,诙谐. 哈哈哈. https://www.bilibili.com/video/av27789609
- Oracle 之 AIO (异步io)
Linux 异步 I/O (AIO)是 Linux 内核中提供的一个增强的功能.它是Linux 2.6 版本内核的一个标准特性,AIO 背后的基本思想是允许进程发起很多 I/O 操作,而不用阻塞或等 ...
- Redis客户端基本命令
更多命令请进入官网查询:https://redis.io/commands 一.基础命令 1.连接服务端 redis-cli 或 redis-cli -h ip地址 -p 端口 2.选择数据库 Red ...
- JavaFX 之窗口跳转(一)
一.前言 笔者此处不讲JavaFX的基础API,只针对笔者工作时遇到的问题进行记录与总结. 零基础的网友可以访问 http://www.javafxchina.net/blog/docs/tutori ...