关于C#操作redis公共类,网上有很多版本,每个版本我都看了,发觉还是不够完美,都存在一个问题,只能操作单一的缓存数据库
redis指令支持上,这里可以自己去扩展,下面分享下我近期封装的一个redis操作类
要用到ServiceStack.Redis V3版,不要用V4 ,v4已经商业化了,有6000次限制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ServiceStack.Redis; namespace ZhengXin.Tools
{
public class RedisUtils
{
/// <summary>
/// 默认过期时间 为秒
/// </summary>
private static int secondsTimeOut = 30 * 60; #region @Royal改进部分,能操作16个库 private static readonly Dictionary<int, PooledRedisClientManager> pools = new Dictionary<int, PooledRedisClientManager>(); static RedisUtils()
{
string path = ConfigHelper.GetConfigKeyValue("RedisPath");
string[] readWriteHosts = new string[] { path };
string[] readOnlyHosts = new string[] { path };
//初始化16个缓存库
for (int i = 0; i <= 15; i++)
{
var model = new PooledRedisClientManager(readWriteHosts, readOnlyHosts, new RedisClientManagerConfig
{
MaxWritePoolSize = 50, // “写”链接池链接数
MaxReadPoolSize = 50, // “读”链接池链接数
AutoStart = true,
}, i, 100, 20);
pools.Add(i, model);
}
} public static PooledRedisClientManager ChooseRedisInstance(int database)
{
if (database > 16) return pools[0]; return pools[database];
} public static void Lpush(string key, string value, int database = 0)
{
using (IRedisClient redis = ChooseRedisInstance(database).GetClient())
{
redis.PushItemToList(key, value);
}
} public static string Lpop(string key, int database = 0)
{
using (IRedisClient redis = ChooseRedisInstance(database).GetClient())
{
return redis.PopItemFromList(key);
}
} public static List<string> List(string key, int database = 0)
{
using (IRedisClient redis = ChooseRedisInstance(database).GetClient())
{
return redis.GetAllItemsFromList(key);
}
} public static bool ItemSet<T>(string key, T t, int timeout = 0, int db = 2)
{
using (IRedisClient RClient = ChooseRedisInstance(db).GetClient())
{
if (timeout >= 0)
{
if (timeout > 0)
{
secondsTimeOut = timeout;
}
}
return RClient.Add<T>(key, t, TimeSpan.FromMinutes(secondsTimeOut));
} } public static T ItemGet<T>(string key, int db = 2) where T : class
{
using (IRedisClient redis = ChooseRedisInstance(db).GetClient())
{
return redis.Get<T>(key);
}
}
}
}

  

自己封装的C#操作redis公共类的更多相关文章

  1. 自己封装的poi操作Excel工具类

    自己封装的poi操作Excel工具类 在上一篇文章<使用poi读写Excel>中分享了一下poi操作Excel的简单示例,这次要分享一下我封装的一个Excel操作的工具类. 该工具类主要完 ...

  2. Java操作Redis工具类

    依赖 jar 包 <dependency> <groupId>redis.clients</groupId> <artifactId>jedis< ...

  3. Jedis 操作 Redis 工具类

    配置类 pom.xml pom.xml 里配置依赖 <dependency> <groupId>redis.clients</groupId> <artifa ...

  4. Redis操作Set工具类封装,Java Redis Set命令封装

    Redis操作Set工具类封装,Java Redis Set命令封装 >>>>>>>>>>>>>>>>& ...

  5. Redis操作List工具类封装,Java Redis List命令封装

    Redis操作List工具类封装,Java Redis List命令封装 >>>>>>>>>>>>>>>> ...

  6. Redis操作Hash工具类封装,Redis工具类封装

    Redis操作Hash工具类封装,Redis工具类封装 >>>>>>>>>>>>>>>>>> ...

  7. Redis操作字符串工具类封装,Redis工具类封装

    Redis操作字符串工具类封装,Redis工具类封装 >>>>>>>>>>>>>>>>>>& ...

  8. 最全的Java操作Redis的工具类,使用StringRedisTemplate实现,封装了对Redis五种基本类型的各种操作!

    转载自:https://github.com/whvcse/RedisUtil 代码 ProtoStuffSerializerUtil.java import java.io.ByteArrayInp ...

  9. StackExchange.Redis帮助类解决方案RedisRepository封装(基础配置)

    本文版权归博客园和作者吴双本人共同所有,转载和爬虫,请注明原文地址.http://www.cnblogs.com/tdws/p/5815735.html 写在前面 这不是教程,分享而已,也欢迎园友们多 ...

随机推荐

  1. android alipay(移动支付,异步通知发起失败,但是支付成功)

    问题1:移动支付 demo测试,支付成功,但是异步通知没发起,help notify_url 需要服务器地址,不是本地网址 问题2:这回 支付成功了.也返回到return_url了.但是页面显示验证失 ...

  2. PHP利器-WAMPServer

    为了配置memcacheAdmin,牵涉到搭建PHP的环境,使用的是WAMPServer,安装之后,需要对apache进行配置, 端口,需要将wamp\bin\apache\Apache2.2.21\ ...

  3. extjs中第一次访问有效,第二次访问出现部分组件无法显示的,动态改变组件的label值的方法,ExtJs中组件最好少使用ID属性(推荐更多使用Name属性)

    在公司做的一个OA项目中,曾经就遇到了这样的一个问题:(我是在jsp中的div中将js render到div中去的)第一次访问此界面的时候,formpanel上的组件能正常显示,不刷新整个页面的前提下 ...

  4. Django下TemplateDoesNotExist 异常的解决方法:

    在settings中添加代码如下获取templates路径: import os import os.path BASE_DIR = os.path.dirname(os.path.dirname(_ ...

  5. bzoj 2251: [2010Beijing Wc]外星联络 后缀数组

    2251: [2010Beijing Wc]外星联络 Time Limit: 30 Sec  Memory Limit: 256 MBSubmit: 424  Solved: 232[Submit][ ...

  6. 李洪强iOS开发Swift篇—02_变量和常量

    李洪强iOS开发Swift篇—02_变量和常量 一.语言的性能 (1)根据WWDC的展示 在进行复杂对象排序时Objective-C的性能是Python的2.8倍,Swift的性能是Python的3. ...

  7. 【Xamarin挖墙脚系列:Android最重要的命令工具ADB】

    原文:[Xamarin挖墙脚系列:Android最重要的命令工具ADB] adb工具提供了很好的基于命令的对系统的控制. 以前说过,安卓的本质是运行在Linux上的虚机系统.在Linux中,对系统进行 ...

  8. 使用Java正则表达式的分组解析身份证的年月日

    根据Java的Pattern和Matcher类通过分组解析出身份证的年月日: public class GetDateInIdCard { public static void main(String ...

  9. BZOJ_1001_狼抓兔子_(平面图求最小割+对偶图求最短路)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1001 1001: [BeiJing2006]狼抓兔子 Time Limit: 15 Sec   ...

  10. Constructing Roads

    Constructing Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...