Redis之Hash___redis中哈希(Hash)在.net中的运用-成都快上网建站

Redis之Hash___redis中哈希(Hash)在.net中的运用

一、下载redis安装文件redis-2.4.6-setup-32-bit.exe。这里一个32位的,本人现在用的XP系统,貌似影响不是很大。详情附件。

创新互联是一家专注于网站设计、成都网站设计与策划设计,东阿网站建设哪家好?创新互联做网站,专注于网站建设10余年,网设计领域的专业建站公司;建站业务涵盖:东阿等地区。东阿做网站价格咨询:18982081108

Redis之Hash___redis中哈希(Hash)在.net中的运用

安装好之后在服务里面启动

Redis之Hash___redis中哈希(Hash)在.net中的运用

二、用vs新建一个小程序,引用4个redis需要的dll文件,详情附件。

Redis之Hash___redis中哈希(Hash)在.net中的运用

三、建一个RedisHelper类,引用4个dll。

    public class RedisHelper : IDisposable
    {
        private static string strRedis = System.Configuration.ConfigurationManager.AppSettings["RedisPath"].ToString();//web.config中配置我本机的一个IP
        public RedisClient Redis = new RedisClient(strRedis, 6379);
        //缓存池
        PooledRedisClientManager prcm = new PooledRedisClientManager();

        //默认缓存过期时间单位秒
        public int secondsTimeOut = 30 * 60;

        /// 
        /// 缓冲池
        /// 
        /// 
        /// 
        /// 
        public static PooledRedisClientManager CreateManager(
         string[] readWriteHosts, string[] readOnlyHosts)
        {
            return new PooledRedisClientManager(readWriteHosts, readOnlyHosts,
                new RedisClientManagerConfig
                {
                    MaxWritePoolSize = readWriteHosts.Length * 5,
                    MaxReadPoolSize = readOnlyHosts.Length * 5,
                    AutoStart = true,
                });
        }
        /// 
        /// 构造函数
        /// 
        /// 是否开启缓冲池
        public RedisHelper(bool OpenPooledRedis = false)
        {

            if (OpenPooledRedis)
            {
                prcm = CreateManager(new string[] { strRedis + ":6379" }, new string[] { strRedis + ":6379" });
                Redis = prcm.GetClient() as RedisClient;
            }
        }

        #region Key/Value存储
        /// 
        /// 设置缓存
        /// 
        /// 
        /// 缓存建
        /// 缓存值
        /// 过期时间,单位秒,-1:不过期,0:默认过期时间
        /// 
        public bool Set(string key, T t, int timeout = 0)
        {
            if (timeout >= 0)
            {
                if (timeout > 0)
                {
                    secondsTimeOut = timeout;
                }
                Redis.Expire(key, secondsTimeOut);
            }

            return Redis.Add(key, t);
        }
        /// 
        /// 获取(根据key来获取value)
        /// 
        /// 
        /// 
        /// 
        public T Get(string key)
        {
            return Redis.Get(key);
        }
        /// 
        /// 移除整个类型的key
        /// 
        /// 
        /// 
        public bool Remove(string key)
        {
            return Redis.Remove(key);

        }

        /// 
        /// key是否存在(0:不存在;1;存在)
        /// 
        /// 
        /// 
        public int IsExists(string key)
        {
            return Convert.ToInt32(Redis.Exists(key));
        }

        public bool Add(string key, T t, int timeout)
        {
            if (timeout >= 0)
            {
                if (timeout > 0)
                {
                    secondsTimeOut = timeout;
                }
                Redis.Expire(key, secondsTimeOut);
            }
            return Redis.Add(key, t);
        }
        #endregion
        /// 
        /// 释放资源
        /// 
        public void Dispose()
        {
            if (Redis != null)
            {
                Redis.Dispose();
                Redis = null;
            }
            GC.Collect();

        }

        #region Hash的方法
        /// 
        /// 判断某个数据是否已经被缓存
        /// 
        public bool Exist(string hashId, string key)
        {
            return Redis.HashContainsEntry(hashId, key);
        }

        /// 
        /// 存储数据到hash表
        /// 
        public bool Set1(string hashId, string key, string value)
        {
            return Redis.SetEntryInHash(hashId, key, value);
        }
        /// 
        /// 移除hash中的某值
        /// 
        public bool Remove(string hashId, string key)
        {
            return Redis.RemoveEntryFromHash(hashId, key);
        }

        /// 
        /// 从hash表获取数据
        /// 
        public string Get1(string hashId, string key)
        {
            return Redis.GetValueFromHash(hashId, key);
        }
        
        /// 
        /// 获取整个hash的数据
        /// 
        public string GetAll1(string hashId)
        {
            string result = "";
            var list = Redis.GetHashValues(hashId);
            if (list != null && list.Count > 0)
            {
                for (int i = 0; i < list.Count; i++)
                {
                    var aa = list[i];
                    result += aa + ",";
                }
                result = result.Trim(',');
            }
            return result;
        }
        /// 
        /// 设置缓存过期
        /// 
        public void SetExpire(string key, DateTime datetime)
        {
            Redis.ExpireEntryAt(key, datetime);
        }
        #endregion



    }

四、再建一个ashx一般处理程序。

    public class UserTest
    {
        public int userId { get; set; }
        public string userName { get; set; }
        public string userPwd { get; set; }
        public int userAge { get; set; }
    }
    
    public class RedisSelectTset1 : IHttpHandler
    {

        public void Proce***equest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            RedisHelper redis = new RedisHelper(true);

            UserTest user = null;
            for (int i = 0; i < 10; i++)
            {
                user = new UserTest() { userId = i, userName = "admin" + i, userPwd = "123456", userAge = 20 + i };
                var value = JsonSerializer.SerializeToString(user);//序列化json格式
                redis.Set1("userHash", "user_Id" + i, value);//第一插入返回Ture,覆盖重复的返回Flash
            }
            
            string getAll = redis.GetAll1("userHash");//获得所有的数据
            DateTime dateTime = Convert.ToDateTime("2099-12-31 00:00:00");//设置缓存过期时间
            redis.SetExpire("userHash", dateTime);
            context.Response.Write(getAll);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }

运行页面之后如何显示:

Redis之Hash___redis中哈希(Hash)在.net中的运用

之后可以在cmd中打开客户端链接:hgetall userHash 根据key取出所有的value

Redis之Hash___redis中哈希(Hash)在.net中的运用就能在你的内存中看到你刚刚运行的页面保存的数据

Redis之Hash___redis中哈希(Hash)在.net中的运用根据redis的命令,可以去除单条数据,如何下图:

Redis之Hash___redis中哈希(Hash)在.net中的运用

关于redis跟多有趣的东西,楼主也在尝试。

附件:http://down.51cto.com/data/2367264

新闻标题:Redis之Hash___redis中哈希(Hash)在.net中的运用
文章位置:http://kswjz.com/article/jiscpj.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流