redis——学习之路五(简单的C#使用redis)
redis官方推荐使用的客户端程序
打星星表示推荐使用的客户端程序,一个笑脸表示最近6个月内有过正式活动的。http://redis.io/clients/#c
从这里我们可以判断官方推荐我们使用ServiceSatck.Redis与StackExchange.Redis,首先要注意的是在ServiceStack.Redis在4.0开始商业化收费的。所以我们这里就不做介绍了,我们只使用StackExChange.Redis做一些简单的C#上面的操作。
这是StackExChange.Redis在Github官网上的https://github.com/StackExchange/StackExchange.Redis代码,下面有文档教程大家可以自己去看看,同时我为大家找到了一位博友对上面一些文档教程的翻译,大家可以作为参考使用。http://www.cnblogs.com/deosky
下面是我对StackExCahnge.Redis做的一个简单的例子以及一些简单的处理。
static void Main(string[] args) { #region 简单dome ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("127.0.0.1:6379,password=CeshiPassword"); IDatabase db = redis.GetDatabase(); string value = "name"; db.StringSet("mykey", value); Console.WriteLine(db.StringGet("mykey")); #endregion
#region 使用ConfigurationOptions连接redis服务器 ConfigurationOptions configurationOptions = new ConfigurationOptions() { EndPoints = { { "127.0.0.1", 6379 } }, CommandMap = CommandMap.Create(new HashSet<string>() { "INFO", "CONFIG", "CLUSTER", "PING", "ECHO", "CLIENT" }, available: false), KeepAlive = 180, DefaultVersion = new Version(2, 8, 24), Password = "CeshiPassword" }; IDatabase db1 = redis.GetDatabase(); string value2 = "name2"; db.StringSet("mykey2", value2); Console.WriteLine(db.StringGet("mykey2")); #endregion
Console.ReadKey(); }
以上我参考说明作者的文档一二简单的写出来了,大家可当成测试使用。
下面是本人做的了一些封装
在文档一中作者提到在ConnectionMulitiplexer在内部走了很多事情,所有我们不需要在每一步操作时创建ConnectionMulitiplexer对象。英文原文:
Because the
ConnectionMultiplexer
does a lot, it is designed to be shared and reused between callers. You should not create a ConnectionMultiplexer
per operation. It is fully thread-safe and ready for this usage. In all the subsequent examples it will be assumed that you have a ConnectionMultiplexer
instance stored away for re-use. But for now, let's create one.所以我们创建一个单例来保存一个ConnectionMulitiplexer对象就可以了,同时我把一些配置放在config文件。
public class RedisManager { /// <summary> /// redis配置文件信息 /// </summary> private static RedisConfig RedisConfig = RedisConfig.GetConfig(); private static ConnectionMultiplexer _redis; private static object _locker = new object(); public static ConnectionMultiplexer Manager { get { if (_redis == null) { lock (_locker) { if (_redis != null) return _redis; _redis = GetManager(); return _redis; } } return _redis; } } private static ConnectionMultiplexer GetManager(ConfigurationOptions configurationOptions = null) { if (configurationOptions == null) { configurationOptions = new ConfigurationOptions() { EndPoints = { { RedisConfig.WriteServerConStr } }, CommandMap = CommandMap.Create(new HashSet<string>() { "INFO", "CONFIG", "CLUSTER", "PING", "ECHO", "CLIENT" }, available: false), KeepAlive = RedisConfig.KeepAlive, DefaultVersion = new Version(2, 8, 24), Password = RedisConfig.PassWord }; } return ConnectionMultiplexer.Connect(configurationOptions); } }
public class RedisCacheManager : ICacheManager { public static RedisCacheManager Instance = new RedisCacheManager(); public string Get(string key) { var db = RedisManager.Manager.GetDatabase(); return db.StringGet(key); } public T Get<T>(string key) { var db = RedisManager.Manager.GetDatabase(); return JsonConvert.DeserializeObject<T>(db.StringGet(key)); } public void Set<T>(string key, T data, int cacheTime) { var db = RedisManager.Manager.GetDatabase(); string str = JsonConvert.SerializeObject(data); db.StringSet(key, str, TimeSpan.FromMinutes(cacheTime)); } public void Set(string key, object data, int cacheTime) { var db = RedisManager.Manager.GetDatabase(); string str = JsonConvert.SerializeObject(data); db.StringSet(key, str, TimeSpan.FromMinutes(cacheTime)); } public void Set(string key, object data) { var db = RedisManager.Manager.GetDatabase(); string str = JsonConvert.SerializeObject(data); db.StringSet(key, str); } public bool IsSet(string key) { var db = RedisManager.Manager.GetDatabase(); return db.KeyExists(key); } public void Remove(string key) { var db = RedisManager.Manager.GetDatabase(); db.KeyDelete(key); }
我们只需要在每一个操作时获取要操作的db,这里是否只需要获取一次db就OK呢?这个本人还没有弄明白,如果大家有谁了解的欢迎指正。
本来这篇文章我早就应该写了,由于公司准备搬家到深圳,本人去不了,正准备面试换工作,所以一直没有更新。