public void AddOrReplace(string key, object value)
        {
            Ensure.StringArgumentNotNullAndNotEmpty(key, nameof(key));
            Ensure.ArgumentNotNull(value, nameof(value));

            System.Runtime.Caching.CacheItem       item   = new System.Runtime.Caching.CacheItem(key, value);
            System.Runtime.Caching.CacheItemPolicy policy = new System.Runtime.Caching.CacheItemPolicy();
            policy.RemovedCallback = x => OnCacheItemRemoved(new CacheItemRemovedEventArgs(x.CacheItem.Key, x.CacheItem.Value));
            cache.Set(item, policy);
        }
Пример #2
0
        /// <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);
        }
Пример #3
0
        /// <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));
        }
Пример #4
0
        /// <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));
        }
Пример #5
0
        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));
        }
Пример #6
0
        // private CacheEntryRemovedCallback callback = null;

        public static void AddToCache(String CacheKeyName, Object CacheItem, CachePriorityType MyCacheItemPriority)
        {
            System.Runtime.Caching.CacheItemPolicy policy = new System.Runtime.Caching.CacheItemPolicy();
            policy.Priority = (MyCacheItemPriority == CachePriorityType.Default) ?
                              System.Runtime.Caching.CacheItemPriority.Default : System.Runtime.Caching.CacheItemPriority.NotRemovable;
            policy.AbsoluteExpiration = DateTimeOffset.Now.AddHours(0.5);

            // Add inside cache
            cache.Set(CacheKeyName, CacheItem, policy);
        }
Пример #7
0
        /// <summary>
        /// Retrieves the asset. Tries local storage first, then moves on to the remote storage systems.
        /// If neither could find the data, or if there is no remote storage set up, the failure callback is called.
        /// </summary>
        /// <param name="assetId">Asset identifier.</param>
        /// <param name="successCallback">Callback called when the asset was successfully found.</param>
        /// <param name="failureCallback">Callback called when there was a failure attempting to get the asset.</param>
        /// <param name="storeResultLocally">Specifies to locally store the asset if it was fetched from a remote.</param>
        public void GetAsset(Guid assetId, SuccessCallback successCallback, FailureCallback failureCallback, bool storeResultLocally = true)
        {
            successCallback = successCallback ?? throw new ArgumentNullException(nameof(successCallback));
            failureCallback = failureCallback ?? throw new ArgumentNullException(nameof(failureCallback));
            if (assetId == Guid.Empty)
            {
                throw new ArgumentException("Asset ID cannot be zero.", nameof(assetId));
            }

            if (_negativeCache != null)
            {
                _negativeCacheLock.EnterReadLock();
                try {
                    if (_negativeCache.Contains(assetId.ToString("N")))
                    {
                        failureCallback();
                        return;
                    }
                }
                finally {
                    _negativeCacheLock.ExitReadLock();
                }
            }

            // Solves GET in middle of PUT situation.
            if (_localStorage.Contains(assetId) && !_localStorage.AssetWasWrittenToDisk(assetId))
            {
                // Asset exists, just might not be on disk yet. Wait here until the asset makes it to disk.
                SpinWait.SpinUntil(() => _localStorage.AssetWasWrittenToDisk(assetId));
            }

            _assetReader.GetAssetAsync(assetId, asset => {
                if (asset != null)
                {
                    successCallback(asset);
                    return;
                }

                failureCallback();

                if (_negativeCache != null)
                {
                    _negativeCacheLock.EnterWriteLock();
                    try {
                        _negativeCache.Set(new System.Runtime.Caching.CacheItem(assetId.ToString("N"), 0), _negativeCachePolicy);
                    }
                    finally {
                        _negativeCacheLock.ExitWriteLock();
                    }
                }
            }, storeResultLocally ? ChattelReader.CacheRule.Normal : ChattelReader.CacheRule.SkipWrite);
        }
Пример #8
0
        /// <summary>
        /// 檢查是否存在快取位址,無則創建
        /// </summary>
        /// <typeparam name="TValue"></typeparam>
        /// <param name="cache"></param>
        /// <param name="key"></param>
        /// <param name="absoluteExpiration"></param>
        /// <param name="slidingExpiration"></param>
        /// <returns></returns>
        private static Container <TValue> GetOrStoreContainer <TValue>(this System.Runtime.Caching.ObjectCache cache, string key, DateTimeOffset absoluteExpiration, TimeSpan slidingExpiration)
        {
            var instance = cache[key];

            if (instance == null)
            {
                lock (cache)
                {
                    instance = cache[key];
                    if (instance == null)
                    {
                        instance = new Container <TValue>();
                        System.Runtime.Caching.CacheItemPolicy policy = new System.Runtime.Caching.CacheItemPolicy();
                        policy.Priority           = System.Runtime.Caching.CacheItemPriority.Default;
                        policy.AbsoluteExpiration = absoluteExpiration;
                        // policy.SlidingExpiration = slidingExpiration;
                        policy.UpdateCallback = CacheItemRemoved;
                        cache.Set(key, instance, policy);
                    }
                }
            }

            return((Container <TValue>)instance);
        }
Пример #9
0
 protected virtual void Add <TValue>(String key, TValue value)
 {
     innerCache.Set(key, new CacheItem(value), ToRuntimePolicy(policy), null);
 }
Пример #10
0
        public void RequestTextureAssetOnCap(Guid capId, Guid assetId, AssetRequest.AssetRequestHandler handler, AssetRequest.AssetErrorHandler errHandler)
        {
            handler    = handler ?? throw new ArgumentNullException(nameof(handler));
            errHandler = errHandler ?? throw new ArgumentNullException(nameof(errHandler));

            if (_negativeCache != null)
            {
                _negativeCacheLock.EnterReadLock();
                var negCacheContainsId = false;
                try {
                    negCacheContainsId = _negativeCache.Contains(assetId.ToString("N"));
                }
                finally {
                    _negativeCacheLock.ExitReadLock();
                }

                if (negCacheContainsId)
                {
                    errHandler(new AssetError {
                        Error = AssetErrorType.AssetTypeWrong,
                    });
                    return;
                }
            }

            if (_caps.TryGetValue(capId, out Capability cap))
            {
                cap.RequestAsset(
                    assetId,
                    (asset) => {
                    if (!ConfigSingleton.ValidTypes.Any(type => type == asset.Type))
                    {
                        if (_negativeCache != null)
                        {
                            _negativeCacheLock.EnterWriteLock();
                            try {
                                _negativeCache.Set(new System.Runtime.Caching.CacheItem(assetId.ToString("N"), 0), _negativeCachePolicy);
                            }
                            finally {
                                _negativeCacheLock.ExitWriteLock();
                            }
                        }

                        errHandler(new AssetError {
                            Error = AssetErrorType.AssetTypeWrong,
                        });
                    }

                    switch (asset.Type)
                    {
                    case 0:
                    case 12:
                    case 18:
                    case 19:
                        handler(asset);
                        break;

                    default:
                        errHandler(new AssetError {
                            Error = AssetErrorType.AssetTypeWrong,
                        });
                        break;
                    }
                },
                    errHandler
                    );

                return;
            }

            errHandler(new AssetError {
                Error = AssetErrorType.CapabilityIdUnknown,
            });
        }