之前系统需要引入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())
{