示例#1
0
        /// <summary>
        /// 设置key的超时时间
        /// </summary>
        /// <param name="cacheType"></param>
        /// <param name="key"></param>
        /// <param name="isCreate"></param>
        public static void SetKeyTimeout(CacheType cacheType, string key, bool isCreate)
        {
            string       typeName = Enum.GetName(typeof(CacheType), cacheType);
            ECacheConfig model    = CacheConfigReader.GetConfig(typeName);

            if (model.TimeOut.HasValue && model.TimeOut.Value > 0 && (isCreate == true || model.AutoDely == true))
            {
                List <string> lstOutput = new List <string>();
                command.Exec(new List <string> {
                    "EXPIRE", key, model.TimeOut.Value.ToString()
                }, lstOutput);
            }
        }
        private static Dictionary <string, ECacheConfig> GetConfig()
        {
            Dictionary <string, ECacheConfig> list = new Dictionary <string, ECacheConfig>();
            XmlDocument xmlDoc = new XmlDocument();
            string      path   = System.Web.HttpContext.Current.Server.MapPath(_configPath);

            xmlDoc.Load(path);
            XmlNodeList  nodes = xmlDoc.SelectNodes("/Items/Item");
            ECacheConfig model = null;
            string       timeOut;
            string       autoDelay;

            foreach (XmlNode node in nodes)
            {
                model     = new ECacheConfig();
                timeOut   = node.Attributes["TimeOut"].Value;
                autoDelay = node.Attributes["AutoDely"].Value;
                if (string.IsNullOrEmpty(timeOut) || timeOut.ToLower() == "null")
                {
                    model.TimeOut = null;
                }
                else
                {
                    model.TimeOut = int.Parse(timeOut);
                }
                model.CacheName = node.Attributes["CacheName"].Value;
                if (autoDelay.ToLower() == "true")
                {
                    model.AutoDely = true;
                }
                else
                {
                    model.AutoDely = false;
                }

                if (string.IsNullOrEmpty(model.CacheName))
                {
                    throw new Exception("缓存配置信息出错,缓存配置名(CacheName)不允许为空");
                }
                if (list.Keys.Contains(model.CacheName))
                {
                    throw new Exception(string.Format("缓存配置信息出现重复项,path:{0},Item:{1}", _configPath, model.CacheName));
                }
                list.Add(model.CacheName, model);
            }
            return(list);
        }