示例#1
0
        public static TReturn TryCache <TReturn>(string key, Func <TReturn> func, int timeOut = 0)
        {
            TReturn t = default(TReturn);

            bool         isOpen  = ConfigHelper.GetBool("CacheEnabled");
            CacheContext context = isOpen ? new CacheContext(CacheTypeEnum.LocalCache) : null;

            if (isOpen && !string.IsNullOrEmpty(key))
            {
                try
                {
                    t = context.GetCache <TReturn>(key);
                }
                catch { t = default(TReturn); }
            }

            if (func != null && t.IsDefault <TReturn>())
            {
                t = func();
                if (isOpen && !string.IsNullOrEmpty(key) && !t.IsDefault <TReturn>())
                {
                    Type type = t.GetType();
                    if (type == typeof(string) || type.IsValueType)
                    {
                        if (!StringHelper.IsObjectNullOrEmpty(t))
                        {
                            if (timeOut == 0)
                            {
                                context.SetCache <TReturn>(key, t);
                            }
                            else
                            {
                                context.SetCache <TReturn>(key, t, DateTime.Now.AddSeconds(timeOut));
                                //context.SetCache<TReturn>(key, t, TimeSpan.FromSeconds(timeOut));
                            }
                        }
                    }
                    else
                    {
                        if (timeOut == 0)
                        {
                            context.SetCache <TReturn>(key, t);
                        }
                        else
                        {
                            context.SetCache <TReturn>(key, t, DateTime.Now.AddSeconds(timeOut));
                            //context.SetCache<TReturn>(key, t, TimeSpan.FromSeconds(timeOut));
                        }
                    }
                }
            }

            return(t);
        }
示例#2
0
        private IList <LoginRoleDto> GetCurrentUser(Guid uid)
        {
            var cacheProvider       = new CacheContext();
            IList <LoginRoleDto> mt = null;

            if (cacheProvider.GetCache <IList <LoginRoleDto> >("nav" + uid.ToString()) == null)
            {
                var tuple = _roleRepository.GetCurrentUser(uid).ToList();
                CommonManager.CacheObj.SetCache("nav" + uid.ToString(), tuple);
                return(tuple);
            }
            else
            {
                mt = CommonManager.CacheObj.GetCache <IList <LoginRoleDto> >("nav" + uid.ToString());
            }
            return(mt);
        }
示例#3
0
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            var cacheProvider       = new CacheContext();
            IList <LoginRoleDto> mt = cacheProvider.GetCache <IList <LoginRoleDto> >("nav" + GetLoginUserId.ToString());
            string url = Request.Path.Value.ToLower();

            if (url.Split('/').Length == 2)
            {
                url = url + "/index";
            }
            if (string.IsNullOrEmpty(url.Split('/')[2]))
            {
                url = url + "index";
            }
            if (url.Split('/').Length > 3)
            {
                url = url.Split('/')[0] + "/" + url.Split('/')[1] + "/" + url.Split('/')[2];
            }
            var first = mt.YDBSFirst(s => s.modules.URL.ToLower().Equals(url));

            ViewBag.CurrentNav = first == null ? null : first.modules.Id.ToString();
            base.OnActionExecuting(context);
        }