C# Redis RedisHelper 帮助类源代码

Anonymity | | 访问(371)

  Redis RedisHelper 帮助类源代码

using Newtonsoft.Json;
using ServiceStack.Redis;

namespace Clump.Redis.Business
{
    public static class RedisHelper
    {
        public static bool isRedisLog = true;//开关
        public static IRedisClientsManager redisClientManager = new PooledRedisClientManager(new string[] 
        {
            //如果是Redis集群则配置多个{IP地址:端口号}即可
            //例如: "10.0.0.1:6379","10.0.0.2:6379","10.0.0.3:6379"
            //开启密码的格式  你的password@你的ip:你的port
            "localhost" //未开始密码 请去掉此注释
           //"foobared@localhost:6379" //开始密码 请去掉此注释
        });

        //从池中获取Redis客户端实例
        public static IRedisClient redisClient = redisClientManager.GetClient();

        // 获取值
        //public static T Get(string key = "")
        {
            T model = redisClient.Get(key);
            return model;
        }

        // 设置值
        public static bool Set(string key, T value)
        {
            bool flag = redisClient.Set(key, value);
            return flag;
        }

        // 删除
        public static bool Delete(string key)
        {
            bool flag = redisClient.Remove(key);
            return flag;
        }

        // 队列,存储
        public static bool EnqueueItemOnList(string listId, object value)
        {
            redisClient.EnqueueItemOnList(listId, JsonConvert.SerializeObject(value));
            return true;
        }

        // 队列,读取
        public static string DequeueItemFromList(string listId)
        {
            string str = redisClient.DequeueItemFromList(listId);
            return str;
        }

    }
}