关于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. php 需熟练掌握的几个函数

    class Test { } $obj = new Test; 一.__construct() {}  构造函数 二.__destroy() {} 析构函数 三.__get() {} 试图读取一个并不 ...

  2. nginx重新加载配置

    1.kill -HUP `cat /usr/local/nginx/logs/nginx.pid` 2./usr/local/nginx/sbin/nginx -s reload

  3. TDirectory.GetLogicalDrives获取本地逻辑驱动器

    使用函数: System.IOUtils.TDirectory.GetLogicalDrives class function GetLogicalDrives: TStringDynArray; s ...

  4. ObjectCopy

    对象的传参用的是传引用,但开发中通常不允许对传入参数进行修改.因此对象拷贝很常用,Python提供一个很方便的对象拷贝方法 如代码: __author__ = 'mengxuan' import co ...

  5. wpf:DataGrid使用

    xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" ...

  6. RSA算法原理及实现

    参考资料: 阮哥的日志:http://www.ruanyifeng.com/blog/2013/06/rsa_algorithm_part_one.html http://www.ruanyifeng ...

  7. linux下使用NFS挂载文件系统

    转自linux如何使用NFS挂载文件系统 设备:一台服务器和一台客户端,这里我们把装在PC机上的RedHat作为服务器,而客户端则是嵌入式linux开发板. 环境:开发板已启动,连接好串口和网线,串口 ...

  8. ThreadLocal学习

    1.简介: 类ThreadLocal<T>,为变量提供了线程本地化副本.对于用ThreadLocal维护的变量,当前线程中的副本不同于它在其他线程中的副本,每个线程通过ThreadLoca ...

  9. 【POJ3169 】Layout (认真的做差分约束)

    Layout   Description Like everyone else, cows like to stand close to their friends when queuing for ...

  10. FFMPEG之TimeBase成员理解

    http://blog.csdn.net/supermanwg/article/details/14521869