windows下redis安装及应用

一、下载安装Redis(windows版本)

  1、下载地址:https://github.com/MicrosoftArchive/redis/releases

  2、安装:

      1)打开运行窗口,输出cmd,进入DOS命令窗口。然后打开至redis目录,执行redis-service redis.windows.conf命令(下图为安装成功)。

        

       2)这个窗口要保持开启关闭时redis服务会自动关闭,所以需要把redis添加到windows服务(可以保存成两个bat批处理文件)。

         安装命令:redis-server.exe --service-install redis.windows.conf --loglevel verbose

         卸载命令:redis-server --service-uninstall 

二、.net环境应用,以ServiceStack.Redis类库为例(功能:删除商品下Key值为xxxxx的缓存)。

  1、定义redis单独配置文件。

    1)web.config

      

       

    2)redis.config

      

  2、定义RedisSection类,redis相关属性等。

namespace Demo.Util
{
    public class RedisSection : ConfigurationSection
    {
        private static RedisSection _instance;

        public static RedisSection Instatce
        {
            get
            {
                _instance = ConfigurationManager.GetSection("redissection") as RedisSection;
                return _instance;
            }
        }

        /// <summary>
        /// 服务器
        /// </summary>
        [ConfigurationProperty("host", IsRequired = true)]
        public string Host
        {
            get { return this["host"].ToString(); }
        }

        /// <summary>
        /// 端口号
        /// </summary>
        [ConfigurationProperty("port", IsRequired = true)]
        public string Port
        {
            get { return this["port"].ToString(); }
        }

        /// <summary>
        /// 密码
        /// </summary>
        [ConfigurationProperty("password", IsRequired = false)]
        public string Password
        {
            get { return this["password"].ToString(); }
        }

        /// <summary>
        /// 过期时间 单位:小时
        /// </summary>
        [ConfigurationProperty("expireTime", IsRequired = false)]
        public int ExpireTime
        {
            get { return int.Parse(this["expireTime"].ToString()); }
        }

        /// <summary>
        /// 最大写链接数
        /// </summary>
        [ConfigurationProperty("MaxWritePoolSize", IsRequired = false, DefaultValue = 5)]
        public int MaxWritePoolSize
        {
            get
            {
                int maxWritePoolSize = (int)base["MaxWritePoolSize"];
                return maxWritePoolSize > 0 ? maxWritePoolSize : 5;
            }
            set
            {
                base["MaxWritePoolSize"] = value;
            }
        }

        /// <summary>
        /// 最大读链接数
        /// </summary>
        [ConfigurationProperty("MaxReadPoolSize", IsRequired = false, DefaultValue = 5)]
        public int MaxReadPoolSize
        {
            get
            {
                int maxReadPoolSize = (int)base["MaxReadPoolSize"];
                return maxReadPoolSize > 0 ? maxReadPoolSize : 5;
            }
            set
            {
                base["MaxReadPoolSize"] = value;
            }
        }

        /// <summary>
        /// 自动重启
        /// </summary>
        [ConfigurationProperty("AutoStart", IsRequired = false, DefaultValue = true)]
        public bool AutoStart
        {
            get
            {
                return (bool)base["AutoStart"];
            }
            set
            {
                base["AutoStart"] = value;
            }
        }

        [ConfigurationProperty("keys", IsDefaultCollection = true)]
        public KeyCollection Keys
        {
            get { return this["keys"] as KeyCollection; }
        }
    }

    public class KeyCollection : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new KeyElement();
        }
        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((KeyElement)element).Name;
        }

        public KeyElement this[int index]
        {
            get
            {
                return this.BaseGet(index) as KeyElement;
            }
        }
        new public KeyElement this[string Name]
        {
            get
            {
                return (KeyElement)BaseGet(Name);
            }
        }
        new public int Count
        {
            get { return base.Count; }
        }
    }

    public class KeyElement : ConfigurationElement
    {
        [ConfigurationProperty("name", IsRequired = false)]
        public string Name
        {
            get { return this["name"].ToString(); }
        }

        [ConfigurationProperty("key", IsRequired = true)]
        public string Key
        {
            get { return this["key"].ToString(); }
        }
    }
}
View Code

  3、定义RedisHelper类,redis相关操作。

public class RedisHelper
    {

        private static string password = string.IsNullOrEmpty(RedisSection.Instatce.Password) ? "" : RedisSection.Instatce.Password + "@";

        //Redis服务器和端口号(密码@127.0.0.1:6379)
        private static string RedisPath = string.Format("{0}:{1}", password + RedisSection.Instatce.Host, RedisSection.Instatce.Port);

        //过期时间(小时)
        //private static int ExpireTime = RedisSection.Instatce.ExpireTime;

        private static PooledRedisClientManager prcm = CreateManager(new string[] { RedisPath }, new string[] { RedisPath });

        /// <summary>
        /// 缓存池
        /// </summary>
        /// <param name="readWriteHosts">“写”链接池链接数</param>
        /// <param name="readOnlyHosts">“读”链接池链接数</param>
        /// <returns></returns>
        private static PooledRedisClientManager CreateManager(string[] readWriteHosts, string[] readOnlyHosts)
        {
            // 支持读写分离,均衡负载 
            return new PooledRedisClientManager(readWriteHosts, readOnlyHosts, new RedisClientManagerConfig
            {
                MaxWritePoolSize = RedisSection.Instatce.MaxWritePoolSize,
                MaxReadPoolSize = RedisSection.Instatce.MaxReadPoolSize,
                AutoStart = RedisSection.Instatce.AutoStart
            });
        }

        public static void Remove(string key)
        {
            using (var redis = prcm.GetClient())
            {
                redis.Remove(key);
            }
        }

        public static void RemoveAll(IEnumerable<string> keys)
        {
            using (var redis = prcm.GetClient())
            {
                redis.RemoveAll(keys);
            }
        }

    }
View Code

  4、使用

var key = RedisSection.Instatce.Keys["product"].Key;
Util.Redis.RedisHelper.Remove(key);
View Code

 

posted @ 2017-08-04 11:28  随学·笔记  阅读(314)  评论(2编辑  收藏  举报