public object GetAndRefreshTimeout(string key) { Ensure.StringArgumentNotNullAndNotEmpty(key, nameof(key)); // calling Get() on MemoryCache refreshes the timeout, so we don' need to do anything more return(cache.Get(key)); }
/// <summary> /// Đếm số lượt của hành động dựa trên tài khoản /// </summary> /// <param name="accountName">truyền vào tên tài khoản</param> /// <param name="action">truyền vào tên action</param> /// <returns></returns> public static int AccountActionCounter(string accountName, string action) { System.Runtime.Caching.ObjectCache cache = System.Runtime.Caching.MemoryCache.Default; object cacheCounter = cache.Get("P" + accountName + "_" + action); return(Convert.ToInt32(cacheCounter)); }
/// <summary> /// Đếm số lượt của hành động dựa trên IP /// </summary> /// <param name="action">truyền vào tên action</param> /// <returns></returns> public static int IpActionCounter(string action) { string ip = IPAddressHelper.GetClientIP(); System.Runtime.Caching.ObjectCache cache = System.Runtime.Caching.MemoryCache.Default; object cacheCounter = cache.Get("P" + ip.ToLower() + "_" + action); return(Convert.ToInt32(cacheCounter)); }
/// <summary> /// Chỉ đếm số lượt trong cache /// </summary> /// <param name="action"></param> /// <returns>số lượt</returns> private int CheckStatusFrequency(string action) { string ip = IPAddressHelper.GetClientIP(); System.Runtime.Caching.ObjectCache cache = System.Runtime.Caching.MemoryCache.Default; object cacheCounter = cache.Get("@Post" + ip.ToLower() + AccountSession.AccountName + "_" + action); if (cacheCounter == null) { return(0); } return(Convert.ToInt32(cacheCounter)); }
protected virtual bool TryGetInner <TValue>(String key, out TValue value) { if (innerCache.Get(key, null) is CacheItem item) { // ReSharper disable once CanBeReplacedWithTryCastAndCheckForNull // ReSharper disable once MergeConditionalExpression value = item.Value is TValue ? (TValue)item.Value : default(TValue); return(true); } value = default(TValue); return(false); }
private int CountPostFrequency(string action) { string ip = Utils.GetIp(); System.Runtime.Caching.ObjectCache cache = System.Runtime.Caching.MemoryCache.Default; object cacheCounter = cache.Get("Post" + ip.ToLower() + AccountSession.AccountName + "_" + action); if (cacheCounter == null) { return(0); } return(Convert.ToInt32(cacheCounter)); }
protected virtual bool TryGetInner <TValue>(String key, out TValue value) { var item = innerCache.Get(key, null) as CacheItem; if (item != null) { // ReSharper disable once CanBeReplacedWithTryCastAndCheckForNull value = item.Value is TValue ? (TValue)item.Value : default(TValue); return(true); } value = default(TValue); return(false); }
/// <summary> /// Thêm số lượt ăn theo tài khoản /// </summary> /// <param name="totalSecond"></param> /// <param name="action"></param> private void AddStatusFrequency(int totalSecond, string action) { string ip = IPAddressHelper.GetClientIP(); System.Runtime.Caching.ObjectCache cache = System.Runtime.Caching.MemoryCache.Default; System.Runtime.Caching.CacheItemPolicy policy = new System.Runtime.Caching.CacheItemPolicy() { AbsoluteExpiration = DateTime.Now.AddSeconds(totalSecond) }; object cacheCounter = cache.Get("@Post" + ip.ToLower() + AccountSession.AccountName + "_" + action); if (cacheCounter == null) { cache.Set("@Post" + ip.ToLower() + AccountSession.AccountName + "_" + action, 1, policy); } cache.Set("@Post" + ip.ToLower() + AccountSession.AccountName + "_" + action, Convert.ToInt32(cacheCounter) + 1, policy); }
/// <summary> /// Kiểm tra tài khoản thực hiện 1 hành động trong số giây (tự cộng số lượt mỗi lần gọi hàm check) /// </summary> /// <param name="accountName">Tên tài khoản</param> /// <param name="totalSecond">Số giây kiểm tra</param> /// <param name="action">tên hành động</param> /// <returns>số lượt gọi hành động</returns> public static int CheckAccountActionFrequency(string accountName, int totalSecond, string action) { System.Runtime.Caching.ObjectCache cache = System.Runtime.Caching.MemoryCache.Default; System.Runtime.Caching.CacheItemPolicy policy = new System.Runtime.Caching.CacheItemPolicy() { AbsoluteExpiration = DateTime.Now.AddSeconds(totalSecond) }; object cacheCounter = cache.Get("P" + accountName + "_" + action); if (cacheCounter == null) { cache.Set("P" + accountName + "_" + action, 1, policy); return(0); } cache.Set("P" + accountName + "_" + action, Convert.ToInt32(cacheCounter) + 1, policy); return(Convert.ToInt32(cacheCounter)); }
/// <summary> /// Kiểm tra ip thực hiện 1 hành động trong số giây (tự cộng số lượt mỗi lần gọi hàm check) /// Không ăn theo tài khoản /// </summary> /// <param name="totalSecond">Số giây kiểm tra</param> /// <param name="action">tên hành động</param> /// <returns>số lượt gọi hành động</returns> public static int CheckIpPostFrequency(int totalSecond, string action) { string ip = IPAddressHelper.GetClientIP(); System.Runtime.Caching.ObjectCache cache = System.Runtime.Caching.MemoryCache.Default; System.Runtime.Caching.CacheItemPolicy policy = new System.Runtime.Caching.CacheItemPolicy() { AbsoluteExpiration = DateTime.Now.AddSeconds(totalSecond) }; object cacheCounter = cache.Get("P" + ip.ToLower() + "_" + action); if (cacheCounter == null) { cache.Set("P" + ip.ToLower() + "_" + action, 1, policy); return(0); } cache.Set("P" + ip.ToLower() + "_" + action, Convert.ToInt32(cacheCounter) + 1, policy); return(Convert.ToInt32(cacheCounter)); }
private int CheckIpPostFrequency(int totalSecond, string action) { string ip = Utils.GetIp(); System.Runtime.Caching.ObjectCache cache = System.Runtime.Caching.MemoryCache.Default; System.Runtime.Caching.CacheItemPolicy policy = new System.Runtime.Caching.CacheItemPolicy() { AbsoluteExpiration = DateTime.Now.AddSeconds(totalSecond) }; object cacheCounter = cache.Get("Post" + ip.ToLower() + AccountSession.AccountName + "_" + action); if (cacheCounter == null) { cache.Set("Post" + ip.ToLower() + AccountSession.AccountName + "_" + action, 1, policy); return(0); } cache.Set("Post" + ip.ToLower() + AccountSession.AccountName + "_" + action, Convert.ToInt32(cacheCounter) + 1, policy); return(Convert.ToInt32(cacheCounter)); }
/// <summary> /// Permite asignar el valor del token guardado en Caché en null para que en el próximo Request vuelva a ser consultado mediante la autenticacion de Azure. /// Esto es en un caso eventual en que el Token sea revocado o expirado de forma extraordinaria, pues el cache dura 50 minutos, y la vida del token es de 60 /// por lo que no debería ocurrir, pero si ocurre, se consultará el token en el próximo request. /// </summary> /// <param name="statusCode"></param> internal void RemoveCacheIfStatusIsUnauthorized(HttpStatusCode statusCode) { try { if (statusCode == System.Net.HttpStatusCode.Unauthorized) { System.Runtime.Caching.ObjectCache cache = System.Runtime.Caching.MemoryCache.Default; if (cache != null) { if (cache.Get("AuthToken") != null) { cache.Remove("AuthToken"); } } } } catch (Exception ex) { } }