感觉redis的接口不好用,还是我用的不对?网上找的帮助类
添加数据时,我需要查询表然后转list,然后循环list,把每个值转成RedisValue保存
using StackExchange.Redis;
 
            var appconfigList= MySqlHelper.QueryList<Appconfig>(strDB, "select * from Appconfig");            
            redisListService.ListRightPush("appconfigList5", appconfigList);
        public long ListRightPush<T>(string redisKey, List<T> redisValue)
        {
            redisKey = AddKeyPrefix(redisKey);
            foreach (var item in redisValue)
            {
                _db.ListRightPush(redisKey, Serialize(item));
            }
            return redisValue.Count;
        }
 public static byte[] Serialize(object obj)
        {
            try
            {
                if (obj == null)
                    return null;
                using (MemoryStream ms = new MemoryStream())
                {
                    IFormatter formatter = new BinaryFormatter();
                    formatter.Serialize(ms, obj);
                    return ms.GetBuffer();
                }
            }
            catch (SerializationException ex)
            {
                throw ex;
            }
        }
取出时数据时。根据key取出RedisValue[],循环RedisValue[],然后把RedisValue转换成model,添加到list中
var aa = redisListService.ListRange<Appconfig>("appconfigList5");
public IEnumerable<T> ListRange<T>(string redisKey) where T : class
        {
            try
            {
                redisKey = AddKeyPrefix(redisKey);
                var List = _db.ListRange(redisKey);
                return ConvetList<T>(List);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public IEnumerable<T> ConvetList<T>(RedisValue[] values)
        {
            List<T> result = new List<T>();
            foreach (var item in values)
            {
                var model = Deserialize<T>(item);
                result.Add(model);
            }
            return result;
        }
        public static T Deserialize<T>(byte[] data)
        {
            if (data == null)
                return default(T);
             
            using (var memoryStream = new MemoryStream(data))
            {
                var binaryFormatter = new BinaryFormatter();
                var result = (T)binaryFormatter.Deserialize(memoryStream);
                return result;
            }
        }移除数据时要循环索引移除,不能一次移除key的全部数据
long ListRemove(RedisKey key, RedisValue value, long count = 0, CommandFlags flags = CommandFlags.None);
查询某个数据要根据key和索引查询,或者取出全部list然后where查

解决方案 »

  1.   

    应该是你找的有问题吧..我找的 常用的 也就3个方法..
    add<T>(key,T);
    get<T>(key);
    delete(key);
    对应调用就是
    var rs=add<userinfo>("admin",new userinfo(){id=...};var rs=get<uerinfo>("admin");var rs=delete("admin");这种基本就是最常用的了.. 对了 少打了个参数.. add后面有timespan的...
      

  2.   


    public class RedisCacheHelper
        {
            private static readonly PooledRedisClientManager pool = null;        private static readonly string[] writeHosts = null;        private static readonly string[] readHosts = null;        private static int RedisMaxReadPool = int.Parse(ConfigurationManager.AppSettings["redis_max_read_pool"]);        private static int RedisMaxWritePool = int.Parse(ConfigurationManager.AppSettings["redis_max_write_pool"]);
            static RedisCacheHelper()
            {
                var redisMasterHost = ConfigurationManager.AppSettings["redis_server_master_session"];
                var redisSlaveHost = ConfigurationManager.AppSettings["redis_server_slave_session"];            if (!string.IsNullOrEmpty(redisMasterHost))
                {
                    writeHosts = redisMasterHost.Split(',');
                    readHosts = redisSlaveHost.Split(',');                if (readHosts.Length > 0)
                    {
                        var config = new RedisClientManagerConfig();
                        config.MaxWritePoolSize = RedisMaxWritePool;
                        config.MaxReadPoolSize = RedisMaxReadPool;
                        config.AutoStart = true;
                        //pool = new PooledRedisClientManager(writeHosts, readHosts, config);//, 15, 50, 5);
                        pool = new PooledRedisClientManager(writeHosts, readHosts, config, 0, 50, 5);
                    }
                }
            }        public static void Add<T>(string key, T value, TimeSpan slidingExpiration)
            {
                if (value == null)
                {
                    return;
                }            if (slidingExpiration.TotalSeconds <= 0)
                {
                    Remove(key);                return;
                }            try
                {
                    if (pool != null)
                    {
                        using (var r = pool.GetClient())
                        {
                            if (r != null)
                            {
                                r.SendTimeout = 1000;
                                r.Set(key, value, slidingExpiration);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    string msg = string.Format("{0}:{1}发生异常!{2}", "cache", "存储", key);
                }        }        public static T Get<T>(string key)
            {
                if (string.IsNullOrEmpty(key))
                {
                    return default(T);
                }            T obj = default(T);            try
                {
                    if (pool != null)
                    {
                        using (var r = pool.GetCacheClient())
                        {
                           
                            if (r != null)
                            {
                                obj = r.Get<T>(key);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    string msg = string.Format("{0}:{1}发生异常!{2}", "cache", "获取", key);
                }
                return obj;
            }        public static bool Set<T>(string key, T value)
            {
                try
                {
                    if (pool != null)
                    {
                        using (var r = pool.GetClient())
                        {
                            if (r != null)
                                r.Set(key, value);
                            return true;
                        }
                    }
                }
                catch (Exception ex)
                {
                    string msg = string.Format("{0}:{1}发生异常!{2}", "cache", "修改", key);
                }
                return false;
            }        public static void Remove(string key)
            {
                try
                {
                    if (pool != null)
                    {
                        using (var r = pool.GetClient())
                        {
                            if (r != null)
                            {
                                r.SendTimeout = 1000;
                                r.Remove(key);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    string msg = string.Format("{0}:{1}发生异常!{2}", "cache", "删除", key);
                }        }        public static bool Exists(string key)
            {
                try
                {
                    if (pool != null)
                    {
                        using (var r = pool.GetClient())
                        {
                            if (r != null)
                            {
                                r.SendTimeout = 1000;
                                return r.ContainsKey(key);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    string msg = string.Format("{0}:{1}发生异常!{2}", "cache", "是否存在", key);
                }            return false;
            }
        }    <add key="redis_server_master_session" value="[email protected]:6379"/>
        <add key="redis_server_slave_session" value="[email protected]:6379"/>
        <add key="redis_max_read_pool" value="60"/>
        <add key="redis_max_write_pool" value="60"/>
      

  3.   

    这儿有一篇.net操作redis的文章,可以参考下。https://136cc.com/p/net_core_redis
      

  4.   

    这是把一个list当一个T传进去保存,删除吗?
      

  5.   

    redis有帮助类啊。
    两个比较有名的。
    StackExchange

    ServiceStack.Redis
      

  6.   


    看你实际需求.如果这个表的数据不多 但是频繁查询 改动不大 你可以把整个list保存到一个key中.如果是操作频繁 那么一般来说 会单独把每条记录当作一个记录 使用name+key 作为key.比如用户表 你可以
    add<user>("user_"+userid,new user(){});var user=get<user>(userid);
    这种方式来实现.所以 看你自己需求 采用对应的方法..至于你代码中出现的序列化啥的 其实没啥用
      

  7.   


    看你实际需求.如果这个表的数据不多 但是频繁查询 改动不大 你可以把整个list保存到一个key中.如果是操作频繁 那么一般来说 会单独把每条记录当作一个记录 使用name+key 作为key.比如用户表 你可以
    add<user>("user_"+userid,new user(){});var user=get<user>(userid);
    这种方式来实现.所以 看你自己需求 采用对应的方法..至于你代码中出现的序列化啥的 其实没啥用
    目前我能想到只做查询使用,不修改,到期后删除重新保存就行,修改量不大,只是当缓存使用,不记录数据。后面队列或者订阅者模式就不是用list了
      

  8.   


    看你实际需求.如果这个表的数据不多 但是频繁查询 改动不大 你可以把整个list保存到一个key中.如果是操作频繁 那么一般来说 会单独把每条记录当作一个记录 使用name+key 作为key.比如用户表 你可以
    add<user>("user_"+userid,new user(){});var user=get<user>(userid);
    这种方式来实现.所以 看你自己需求 采用对应的方法..至于你代码中出现的序列化啥的 其实没啥用
    如果只是查询数据缓存起来使用是不是用SET会更好?
      

  9.   

    直接用StackExchange.Redis.Extensions,这个都帮你封装好了,你只需要声明数据是怎么序列化的