Exemple #1
0
        //获取js_api_ticket
        public static string GetJsApiTicket()
        {
            string        ticket;
            ICacheManager cache    = new MemoryCacheManager();
            var           contains = cache.Contains("js_api_ticket");

            if (contains)
            {
                ticket = cache.GetCache <string>("js_api_ticket");
            }
            else
            {
                var httpGet          = HttpHelper.HttpGet(GetTicketUrl(), "");
                var jsApiTicketModel = JsonConvert.DeserializeObject <JsApiTicketModel>(httpGet);
                if (jsApiTicketModel.errcode == 0)
                {
                    ticket = jsApiTicketModel.ticket;
                    long expiresIn = jsApiTicketModel.expires_in;
                    cache.SetCache("js_api_ticket", ticket, TimeSpan.FromSeconds(expiresIn - 60));
                }
                else
                {
                    ticket = null;
                }
            }

//            var httpGet = HttpHelper.HttpGet(GetTicketUrl(), "");
//            var jsApiTicketModel = JsonConvert.DeserializeObject<JsApiTicketModel>(httpGet);
//            ticket = jsApiTicketModel.errcode == 0 ? jsApiTicketModel.ticket : null;

            return(ticket);
        }
        public void MemoryCacheManager_Should_Not_Throw_Exception_When_Trying_To_Remove_A_Non_Excisting_Key()
        {
            ICacheManager cacheManager = new MemoryCacheManager();

            cacheManager.Remove("Test");

            Assert.IsFalse(cacheManager.Contains("Test"));
        }
        public void MemoryCacheManager_Should_Return_Null_When_Item_From_Cache_Was_Removed_And_Afterwards_Requested()
        {
            ICacheManager cacheManager = new MemoryCacheManager();

            cacheManager.Add("Test", "Dit is de waarde...", new CacheItemPolicy());

            cacheManager.Remove("Test");

            Assert.IsFalse(cacheManager.Contains("Test"));
        }
        public static bool HasPermission(Permission permission)
        {
            if (!HttpContext.Current.User.Identity.IsAuthenticated)
            {
                return(false);
            }
            var    username = HttpContext.Current.User.Identity.Name;
            string key      = $"AuthorizeService-Permission-{username}";
            var    list     = new MemoryCacheManager().Get(key, GetPermissionByUser);

            return(list.Contains((int)permission));
        }
        public IActionResult GetAll()
        {
            var list = _userService.GetAll();

            if (list != null)
            {
                var    cm       = new MemoryCacheManager();
                string cacheKey = "users";
                if (cm.Contains(cacheKey))
                {
                    cm.Remove(cacheKey);
                }
                cm.Add(cacheKey, list);
                return(Ok(list));
            }
            return(BadRequest());
        }
Exemple #6
0
        //获取accessToken
        public static string GetAccessToken()
        {
            string        accessToken;
            ICacheManager cache    = new MemoryCacheManager();
            var           contains = cache.Contains("access_token");

            if (contains)
            {
                accessToken = cache.GetCache <string>("access_token");
            }
            else
            {
                var result           = HttpHelper.HttpGet(GetAccessTokenUrl(), "");
                var accessTokenModel = JsonConvert.DeserializeObject <AccessTokenModel>(result);
                accessToken = accessTokenModel.access_token;
                long expiresIn = accessTokenModel.expires_in;
                cache.SetCache("access_token", accessToken, TimeSpan.FromSeconds(expiresIn - 60));
            }
            return(accessToken);
        }