Exemple #1
0
        public CacheObj Get(string key)
        {
            if (string.IsNullOrEmpty(key))
            {
                return(null);
            }
            CacheObj obj = new CacheObj();

            if (_cacheList.TryGetValue(key, out obj))
            {
                if (IsOverdue(obj))
                {
                    this.Remove(key);
                    DateTime dt = new DateTime();
                    _cacheTimeList.TryRemove(key, out dt);
                    return(null);
                }
                else
                {
                    return(obj);
                }
            }
            else
            {
                return(null);
            }
        }
Exemple #2
0
        public IDictionary <string, CacheObj> GetValues(List <string> keys)
        {
            Dictionary <string, CacheObj> dic_t = new Dictionary <string, CacheObj>();

            foreach (var key in keys)
            {
                if (!string.IsNullOrEmpty(key))
                {
                    CacheObj obj = new CacheObj();
                    if (_cacheList.TryGetValue(key, out obj))
                    {
                        if (!this.IsOverdue(obj))
                        {
                            dic_t.Add(key, obj);
                        }
                        else
                        {
                            DateTime dt = new DateTime();
                            _cacheTimeList.TryRemove(key, out dt);
                        }
                    }
                }
            }
            return(dic_t);
        }
Exemple #3
0
        public bool Add(string key, CacheObj obj)
        {
            if (string.IsNullOrEmpty(key))
            {
                return(false);
            }
            if (GetCount() > MaxCount)
            {
                return(false);
            }
            string path = Path.Combine(CachePath, key.Trim() + Extension);

            if (!File.Exists(path) && obj != null)
            {
                try
                {
                    File.WriteAllText(path, SerializeToJson(obj));
                    return(true);
                }
                catch (Exception ex) {
                    Debug.WriteLine("Add Error 未命中:" + ex.Message);
                    return(false);
                }
            }
            else
            {
                return(false);
            }
        }
Exemple #4
0
        public CacheObj Get(string key)
        {
            if (string.IsNullOrEmpty(key))
            {
                return(null);
            }
            string path = Path.Combine(CachePath, key.Trim() + Extension);

            if (File.Exists(path))
            {
                CacheObj obj = DeserializationByJson(File.ReadAllText(path));
                if (IsOverdue(obj))
                {
                    this.Remove(key);
                    return(null);
                }
                else
                {
                    return(obj);
                }
            }
            else
            {
                return(null);
            }
        }
Exemple #5
0
        public bool Remove(string key)
        {
            if (string.IsNullOrEmpty(key))
            {
                return(false);
            }
            CacheObj obj = new CacheObj();
            DateTime dt  = new DateTime();

            _cacheTimeList.TryRemove(key, out dt);
            return(_cacheList.TryRemove(key, out obj));
        }
Exemple #6
0
 /// <summary>
 /// 监控主体函数
 /// </summary>
 /// <param name="obj"></param>
 private static void SurveillantTask(object obj)
 {
     while (true)
     {
         Debug.WriteLine("监视序列化的文件:" + DateTime.Now.ToString());
         try
         {
             if (Directory.GetFiles(CachePath).Length > 0)
             {
                 string[] key_arr = Directory.GetFiles(CachePath);//临时的key
                 for (int j = 0; j < key_arr.Length; j++)
                 {
                     string key = key_arr[j];
                     if (string.IsNullOrEmpty(key))
                     {
                         continue;
                     }
                     CacheObj tempobj  = new CacheObj();
                     string   filepath = key;
                     if (File.Exists(filepath))
                     {
                         tempobj = DeserializationByJson(File.ReadAllText(filepath));
                         if (DateTime.Now > tempobj.FailureTime)
                         {
                             try
                             {
                                 File.Delete(filepath);
                                 Debug.WriteLine("监控线程移除key:" + key);
                             }
                             catch (Exception ex) {
                                 Debug.WriteLine("监控线程 Error:" + ex.Message);
                             }
                         }
                     }
                 }
             }
         }
         catch (Exception ex) {
             Debug.WriteLine("SurveillantTask Message:" + ex.Message);
         }
         if (PatrolInterval < 10)
         {
             PatrolInterval = 10;//不要间隔太短
         }
         Thread.Sleep(PatrolInterval * 1000);
     }
 }
Exemple #7
0
 public bool Add(string key, CacheObj obj)
 {
     if (_cacheList.Count > MaxCount)
     {
         return(false);
     }
     else
     {
         try {
             _cacheTimeList.TryAdd(key, obj.FailureTime);
             return(_cacheList.TryAdd(key, obj));
         }
         catch {
             return(false);
         }
     }
 }
Exemple #8
0
 /// <summary>
 /// 监控主体函数
 /// </summary>
 /// <param name="obj"></param>
 private static void SurveillantTask(object obj)
 {
     while (true)
     {
         try
         {
             if (_cacheTimeList.Keys.Count > 0)
             {
                 string[] key_arr = new string[_cacheTimeList.Keys.Count];//临时的key
                 _cacheTimeList.Keys.CopyTo(key_arr, 0);
                 for (int j = 0; j < key_arr.Length; j++)
                 {
                     DateTime dt  = new DateTime();
                     string   key = key_arr[j];
                     if (string.IsNullOrEmpty(key))
                     {
                         continue;
                     }
                     if (_cacheTimeList.TryGetValue(key, out dt))
                     {
                         if (dt != null)
                         {
                             if (DateTime.Now > dt)
                             {
                                 CacheObj c_obj = new CacheObj();
                                 _cacheTimeList.TryRemove(key, out dt);
                                 _cacheList.TryRemove(key, out c_obj);
                                 Debug.WriteLine("监控线程移除key:" + key);
                             }
                         }
                     }
                 }
             }
         }
         catch (Exception ex) {
             Debug.WriteLine("Message:" + ex.Message);
         }
         if (PatrolInterval < 5)
         {
             PatrolInterval = 5;//不要间隔太短
         }
         Thread.Sleep(PatrolInterval * 1000);
     }
 }
Exemple #9
0
 /// <summary>
 /// 过期判断
 /// </summary>
 /// <param name="obj"></param>
 /// <returns></returns>
 private bool IsOverdue(CacheObj obj)
 {
     if (obj == null)
     {
         return(false);
     }
     if (obj.FailureTime == null)
     {
         return(false);
     }
     if (obj.FailureTime == DateTime.MaxValue)
     {
         return(false);
     }
     else
     {
         return(DateTime.Now > obj.FailureTime);
     }
 }
Exemple #10
0
        public IDictionary <string, CacheObj> GetValues(List <string> keys)
        {
            Dictionary <string, CacheObj> dic_t = new Dictionary <string, CacheObj>();

            foreach (var key in keys)
            {
                if (!string.IsNullOrEmpty(key))
                {
                    CacheObj obj = Get(key);
                    if (obj != null)
                    {
                        if (!this.IsOverdue(obj))
                        {
                            dic_t.Add(key, obj);
                        }
                    }
                }
            }
            return(dic_t);
        }
Exemple #11
0
 public bool Update(string key, HTools.CacheObj obj)
 {
     if (string.IsNullOrEmpty(key))
     {
         return(false);
     }
     if (obj == null)
     {
         return(false);
     }
     if (m_mcache.Update(key, obj))
     {
         m_dcache.Update(key, obj);
         return(true);
     }
     else
     {
         return(false);
     }
 }
Exemple #12
0
 public bool Update(string key, CacheObj obj)
 {
     try
     {
         CacheObj temp = new CacheObj();
         if (_cacheList.TryGetValue(key, out temp))
         {
             if (this.IsOverdue(obj))
             {
                 return(false);
             }
             else
             {
                 _cacheTimeList.TryUpdate(key, temp.FailureTime, obj.FailureTime);
                 return(_cacheList.TryUpdate(key, temp, obj));
             }
         }
         else
         {
             return(false);
         }
     }
     catch { return(false); }
 }
Exemple #13
0
 public bool Update(string key, CacheObj obj)
 {
     try
     {
         if (string.IsNullOrEmpty(key))
         {
             return(false);
         }
         string path = Path.Combine(CachePath, key.Trim() + Extension);
         if (File.Exists(path))
         {
             File.WriteAllText(path, SerializeToJson(obj));
             return(true);
         }
         else
         {
             return(false);
         }
     }
     catch (Exception ex) {
         Debug.WriteLine("Update Error 未命中:" + ex.Message);
         return(false);
     }
 }
Exemple #14
0
        public void OnActionExecuting(ActionExecutingContext filterContext)
        {
            #region 权限判断
            var             token      = CookieHelper.GetCookieValue("Admin");
            string          _GroupName = filterContext.Controller.ControllerContext.RouteData.Values["controller"].ToString();
            HTools.CacheObj obj        = cacheService.Get(Constant.CacheKey.LoginAdminInfoCacheKey + "_" + token);
            if (obj != null && obj.value != null)
            {
                if (obj.value is Newtonsoft.Json.Linq.JObject)
                {
                    User user = obj.value.ParseJSON <User>();
                    this.LoginUser = user;
                }
                else
                {
                    this.LoginUser = (User)obj.value;
                }
            }
            else
            {
                this.LoginUser = null;
            }
            bool b = false;
            if (!this.LoginUser.IsSuperUser)
            {
                string[] acts = LoginUser.Role.ActionIds.Split(',');
                obj = cacheService.Get(Constant.CacheKey.PowerConfigCacheKey);
                if (obj != null && obj.value != null)
                {
                    if (obj.value is Newtonsoft.Json.Linq.JObject)
                    {
                        PowerAdmin powerAdmin = obj.value.ParseJSON <PowerAdmin>();
                        this.Power = powerAdmin;
                    }
                    else
                    {
                        this.Power = (PowerAdmin)obj.value;
                    }
                }
                else
                {
                    this.Power = null;
                }
                if (this.Power == null)
                {
                    this.Power = powerConfigService.LoadConfig(Constant.PowerConfigPath);
                    cacheService.Add(Constant.CacheKey.PowerConfigCacheKey, new CacheObj()
                    {
                        value = this.Power, AbsoluteExpiration = new TimeSpan(1, 0, 0, 0)
                    });
                }
                try
                {
                    if (this.Power != null)
                    {
                        var list = this.Power.list;
                        foreach (var li in list)
                        {
                            var p = li.module.FirstOrDefault(t => t.Name == ModuleName);
                            if (p != null)
                            {
                                string action_type = _GroupName + "|" + p.Name + "|" + actionEnum.ToString();//格式 模块名|操作
                                if (acts.Contains(action_type))
                                {
                                    b = true;//存在权限
                                    break;
                                }
                            }
                        }
                    }
                }
                catch
                {
                    b = false;
                }
            }
            else
            {
                b = true;
            }
            #endregion

            #region 无权限执行
            if (b == false)
            { //无权限执行
                if (filterContext.HttpContext.Request.IsAjaxRequest())
                {
                    filterContext.Result = new JsonResult()
                    {
                        ContentEncoding = Encoding.UTF8,
                        Data            = new ResultModel
                        {
                            pass = false,
                            msg  = "无权访问"
                        },
                        JsonRequestBehavior = JsonRequestBehavior.AllowGet
                    };
                }
                else
                {
                    filterContext.Controller.ViewData["ErrorMessage"] = "无权访问"; //filterContext.Exception.Message + " 亲!您犯错了哦!";//得到报错的内容
                    filterContext.Result = new ViewResult()                     //new一个url为Error视图
                    {
                        ViewName = "Error",                                     /*在Shard文件夹下*/
                        ViewData = filterContext.Controller.ViewData            //view视图的属性中的viewdata被赋值
                    };
                }
            }
            #endregion
        }