C# 通过ServiceStack 操作Redis——String类型的使用及示例
明志德道 人气:01.引用Nuget包 ServiceStack.Redis
我这里就用别人已经封装好的Reids操作类来和大家一起参考了下,看看怎么使用ServiceStack.Redis 操作Redis
RedisConfigInfo--redis配置文件信息
/// <summary> /// redis配置文件信息 /// 也可以放到配置文件去 /// </summary> public sealed class RedisConfigInfo { /// <summary> /// 可写的Redis链接地址 /// format:ip1,ip2 /// /// 默认6379端口 /// </summary> public string WriteServerList = "127.0.0.1:6379"; /// <summary> /// 可读的Redis链接地址 /// format:ip1,ip2 /// </summary> public string ReadServerList = "127.0.0.1:6379"; /// <summary> /// 最大写链接数 /// </summary> public int MaxWritePoolSize = 60; /// <summary> /// 最大读链接数 /// </summary> public int MaxReadPoolSize = 60; /// <summary> /// 本地缓存到期时间,单位:秒 /// </summary> public int LocalCacheTime = 180; /// <summary> /// 自动重启 /// </summary> public bool AutoStart = true; /// <summary> /// 是否记录日志,该设置仅用于排查redis运行时出现的问题, /// 如redis工作正常,请关闭该项 /// </summary> public bool RecordeLog = false; }
RedisManager --Redis管理中心 创建Redis链接
/// <summary> /// Redis管理中心 创建Redis链接 /// </summary> public class RedisManager { /// <summary> /// redis配置文件信息 /// </summary> private static RedisConfigInfo RedisConfigInfo = new RedisConfigInfo(); /// <summary> /// Redis客户端池化管理 /// </summary> private static PooledRedisClientManager prcManager; /// <summary> /// 静态构造方法,初始化链接池管理对象 /// </summary> static RedisManager() { CreateManager(); } /// <summary> /// 创建链接池管理对象 /// </summary> private static void CreateManager() { string[] WriteServerConStr = RedisConfigInfo.WriteServerList.Split(','); string[] ReadServerConStr = RedisConfigInfo.ReadServerList.Split(','); prcManager = new PooledRedisClientManager(ReadServerConStr, WriteServerConStr, new RedisClientManagerConfig { MaxWritePoolSize = RedisConfigInfo.MaxWritePoolSize, MaxReadPoolSize = RedisConfigInfo.MaxReadPoolSize, AutoStart = RedisConfigInfo.AutoStart, }); } /// <summary> /// 客户端缓存操作对象 /// </summary> public static IRedisClient GetClient() { return prcManager.GetClient(); } }
RedisBase-- 是redis操作的基类,继承自IDisposable接口,主要用于释放内存
/// <summary> /// RedisBase类,是redis操作的基类,继承自IDisposable接口,主要用于释放内存 /// </summary> public abstract class RedisBase : IDisposable { public IRedisClient iClient { get; private set; } /// <summary> /// 构造时完成链接的打开 /// </summary> public RedisBase() { iClient = RedisManager.GetClient(); } //public static IRedisClient iClient { get; private set; } //static RedisBase() //{ // iClient = RedisManager.GetClient(); //} private bool _disposed = false; protected virtual void Dispose(bool disposing) { if (!this._disposed) { if (disposing) { iClient.Dispose(); iClient = null; } } this._disposed = true; } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } public void Transcation() { using (IRedisTransaction irt = this.iClient.CreateTransaction()) { try { irt.QueueCommand(r => r.Set("key", 20)); irt.QueueCommand(r => r.Increment("key", 1)); irt.Commit(); // 提交事务 } catch (Exception ex) { irt.Rollback(); throw ex; } } } /// <summary> /// 清除全部数据 请小心 /// </summary> public virtual void FlushAll() { iClient.FlushAll(); } /// <summary> /// 保存数据DB文件到硬盘 /// </summary> public void Save() { iClient.Save();//阻塞式save } /// <summary> /// 异步保存数据DB文件到硬盘 /// </summary> public void SaveAsync() { iClient.SaveAsync();//异步save } }
nuget包是外国人写的,在国内并没有完整的中文文档,也没有专门的人来翻译、封装它,所以上面的代码方法不是很全,还有很多api方法需要自己去官网找然后自己封装。
在这里,上面的封装我就放一边,还是给大家演示ServiceStack原生的API如何使用
2. string 类型的使用
//1.存入键值对 bool a = client.Set("key_name", "value_11"); //2. 根据key获取值 string data1= client.GetValue("key_name"); //3. 在原有的value上进行追加 long data2 = client.AppendToValue("key_name", "value_11"); // 4.获取值的长度 var data3=client.GetStringCount("key_name"); //5. 数值自增/减,返回自增、自减后的值 client.Set("小明分数", 100); //自增20,可以自增负值 var data4= client.IncrementValueBy("小明分数", 20); //自减50 var data5 = client.DecrementValueBy("小明分数", 50); //6. 插入实体和读取实体 UserInfo userInfo = new UserInfo() { Id = 3, Age = 50, Name = "zxl", Pwd = "123456" }; client.Set("UserInfo_Id_3", userInfo); UserInfo data6 = client.Get<UserInfo>("UserInfo_Id_3"); //7. 一次性添加多个key-value集合 Dictionary<string, string> dic = new Dictionary<string, string>() { { "101", Guid.NewGuid().ToString("N")}, { "102", Guid.NewGuid().ToString("N")}, { "103", Guid.NewGuid().ToString("N")}, { "104", Guid.NewGuid().ToString("N")}, { "105", Guid.NewGuid().ToString("N")}, { "106", Guid.NewGuid().ToString("N")} }; client.SetAll(dic); //8.获取多个key的 value值集合 List<string> keys = new List<string>(){ "101", "103", "105" }; List<string> data8= client.GetValues(keys);
//9. 重命名key client.Rename("106", "1066"); //10. 设置key的过期时间(30秒后自动销毁) bool b2= client.Expire("102", 30); //11. 删除单个key bool d1 = client.Remove("101");//删除成功,返回true bool d2 = client.Remove("ffff"); //删除不存在的数据,返回false
//12. 删除多个key IEnumerable<string> delete_keys = new List<string> { "101", "103", "105" }; client.RemoveAll(delete_keys);
//13.清除全部数据 请小心 client.FlushAll();
加载全部内容