C# 使用 ServiceStack.Redis 自由切换db

之前系统需要引入nodb  想了想最后还是选了 redis

百度一下找了一个RedisCacheHelper.cs 拿过来就用了。

用的时候呢发现一些问题

1.这个方法里面没有找到自由切换库的地方。

2.可不可以通过模糊搜索得到 key 或者 val?

不同业务应该放在不同库里面,这样就不用一股脑的全往db0里面塞了。

既然没找到那就找其他办法呗,想了想,死方法,将key存入的时候加个前缀,不同业务加不同前缀。

由于那会也不知道可不可以通过模糊搜索来得到key。

取的时候就痛苦了。。。每次都是拿出来全部的key。。然后遍历 判断 哎有点难受

今天花时间研究了下,上面两个问题,都解决了,过程有点小曲折,也是断断续续的在弄最终把github上的源码down了,翻了翻代码

https://github.com/ServiceStack/ServiceStack.Redis   这是github的地址

先上我改了之后的代码吧(原版的代码是从哪里来的 忘了...) 有些注释有点问题 看看就好了 我也没仔细改

配置文件中的

 <add key="SessionExpireMinutes" value="180" />
    <add key="RedisServerSession" value="123456@127.0.0.1:6379" />
    <add key="RedisMaxReadPool" value="3" />
    <add key="RedisMaxWritePool" value="1" />
using ServiceStack.Redis;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 public class RedisCacheHelper
    {
        private static readonly PooledRedisClientManager pool = null;
        private static readonly string[] redisHosts = null;
        public static int RedisMaxReadPool = int.Parse(ConfigurationManager.AppSettings["RedisMaxReadPool"]);
        public static int RedisMaxWritePool = int.Parse(ConfigurationManager.AppSettings["RedisMaxWritePool"]);

        static RedisCacheHelper()
        {
            var redisHostStr = ConfigurationManager.AppSettings["RedisServerSession"];

            if (!string.IsNullOrEmpty(redisHostStr))
            {
                redisHosts = redisHostStr.Split(',');

                if (redisHosts.Length > 0)
                {
                    pool = new PooledRedisClientManager(redisHosts, redisHosts,
                        new RedisClientManagerConfig()
                        {
                            MaxWritePoolSize = RedisMaxWritePool,
                            MaxReadPoolSize = RedisMaxReadPool,
                            AutoStart = true,
                             
                        });
                    //pool = new PooledRedisClientManager(1,redisHosts);

                }
            }
        }
        public static bool Add<T>(string key, T value, DateTime expiry, int db = 0)
        {
            if (value == null)
            {
                return false;
            }

            if (expiry <= DateTime.Now)
            {
                Remove(key, db);

                return false;
            }

            try
            {
                if (pool != null)
                {
                    //using (var r = pool.GetClient())
                    using (var r = (RedisClient)pool.GetClient())
                    {
           
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值