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作为缓存服务器的更多相关文章

  1. HAProxy 的负载均衡服务器,Redis 的缓存服务器

    问答社区网络 StackExchange 由 100 多个网站构成,其中包括了 Alexa 排名第 54 的 StackOverflow.StackExchang 有 400 万用户,每月 5.6 亿 ...

  2. Redis 作为缓存服务器的配置

    随着redis的发展,越来越多的架构用它取代了memcached作为缓存服务器的角色,它有几个很突出的特点:1. 除了Hash,还提供了Sorted Set, List等数据结构2. 可以持久化到磁盘 ...

  3. 用Redis作为缓存服务器,加快数据库操作速度

    https://zh.wikipedia.org/wiki/Redis http://www.jianshu.com/p/01b37cdb3f33

  4. Windows下Redis缓存服务器的使用 .NET StackExchange.Redis Redis Desktop Manager

    Redis缓存服务器是一款key/value数据库,读110000次/s,写81000次/s,因为是内存操作所以速度飞快,常见用法是存用户token.短信验证码等 官网显示Redis本身并没有Wind ...

  5. Django分析之使用redis缓存服务器

    时间长没有更新了,这段时间一直忙着一个项目,今天就记录一个现在经常会用到的技术吧. redis相信大家都很熟悉了,和memcached一样是一个高性能的key-value数据库,至于什么是缓存服务器, ...

  6. redis作为mysql的缓存服务器(读写分离,通过mysql触发器实现数据同步)

    一.redis简介Redis是一个key-value存储系统.和Memcached类似,为了保证效率,数据都是缓存在内存中.区别的是redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录 ...

  7. redis来共享各个服务器的session,并同时通过redis来缓存一些常用的资源,加快用户获得请求资源的速度(转)

    时间过得真快,再次登录博客园来写博,才发现距离上次的写博时间已经过去了一个月了,虽然是因为自己找了实习,但这也说明自己对时间的掌控能力还是没那么的强,哈哈,看来还需不断的努力啊!(这里得特别说明一下本 ...

  8. redis作为mysql的缓存服务器(读写分离) (转)

    一.redis简介Redis是一个key-value存储系统.和Memcached类似,为了保证效率,数据都是缓存在内存中.区别的是redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录 ...

  9. Windows下Redis缓存服务器的使用 .NET StackExchange.Redis Redis Desktop Manager 转发非原创

    Windows下Redis缓存服务器的使用 .NET StackExchange.Redis Redis Desktop Manager   Redis缓存服务器是一款key/value数据库,读11 ...

随机推荐

  1. Python 函数 memoryview()

    memoryview() 函数返回给定参数的内存查看对象(Momory view). 所谓内存查看对象,是指对支持缓冲区协议的数据进行包装,在不需要复制对象基础上允许Python代码访问.返回元组列表 ...

  2. Lombok 简单入门

    原文地址:Lombok 简单入门 博客地址:http://www.extlight.com 一.前言 Lombok 是一个 Java 库,它作为插件安装至编辑器中,其作用是通过简单注解来精简代码,以此 ...

  3. ubuntu :安装好了搜狗输入法但是没法用

    用 im-config 命令打开一个配置器窗口 默认是ibus,需要修改为 fcitx 重启 ubuntu 系统就可以了.

  4. STM32学习笔记之__attribute__ ((at())绝对定位分析

    STM32也会遇到这样的绝对定位的问题如下: uint8_t   UART_RX_BUF[1024]   __attribute__ ((at(0X20001000)));   //就是将串口接收的数 ...

  5. Android多线程断点下载的代码流程解析

    Step 1:创建一个用来记录线程下载信息的表 创建数据库表,于是乎我们创建一个数据库的管理器类,继承SQLiteOpenHelper类 重写onCreate()与onUpgrade()方法 DBOp ...

  6. 阻塞队列之二:LinkedTransferQueue

    一.LinkedTransferQueue简介 TransferQueue是一个继承了BlockingQueue的接口,并且增加若干新的方法.LinkedTransferQueue是TransferQ ...

  7. 操作系统-服务器-百科:Windows Server

    ylbtech-操作系统-服务器-百科:Windows Server Windows Server是微软在2003年4月24日推出的Windows 的服务器操作系统,其核心是Microsoft Win ...

  8. Centos6-7安装Python3.5

    可以看到我们现在是2.7.5的,现在我安装一个3.5版本的 安装python3之前首先安装ssl开发库,否则会造成python3的ssl库都无法使用!!! yum install openssl op ...

  9. css3实现气泡效果的聊天框

    因为CSS3尚未形成标准,所以现行的浏览器对于css3支持不太一致,某些特性需要加上浏览器前缀 css属性的浏览器前缀 前缀 渲染引擎 使用该引擎的浏览器 -khtml- KHTML Konquero ...

  10. 020:Buffer Pool 、压缩页、CheckPoint、Double Write、Change Buffer

    一. 缓冲池(Buffer Pool) 1.1 缓冲池介绍 每次读写数据都是通过 Buffer Pool : 当Buffer Pool 中没有用户所需要的数据时,才去硬盘中获取: 通过 innodb_ ...