/// <summary>
        ///     Get from cache.
        /// </summary>
        /// <param name="cacheKey">The cache key.</param>
        /// <param name="region"></param>
        /// <returns>
        ///     An object instance with the Cache Value corresponding to the entry if found, else null
        /// </returns>
        public override async Task <object> Get(object cacheKey, string region)
        {
            if (!_isEnabled)
            {
                return(null);
            }

            object item;

            lock (Sync)
            {
                item = _cache.Get(MemoryUtilities.CombinedKey(cacheKey, region));
            }

            if (item == null)
            {
                return(null);
            }

            // todo remove after dec when obsolete methods are removed
            var itemType = item.GetType();

            if (itemType == typeof(BaseModel))
            {
                var obj = (BaseModel)item;
                return(await MemoryStreamHelper.DeserializeObject(obj.CacheObject));
            }

            if (itemType == typeof(CacheObject))
            {
                var obj1 = (CacheObject)item;
                return(await MemoryStreamHelper.DeserializeObject(obj1.Item));
            }
            return(null);
        }
        /// <summary>
        ///     Remove from cache.(region not supported in memorycache)
        /// </summary>
        /// <param name="cacheKey">The cache key.</param>
        /// <param name="region"></param>
        /// <returns>True if successful else false.</returns>
        public override Task <bool> Remove(object cacheKey, string region)
        {
            if (!_isEnabled)
            {
                Task.FromResult(true);
            }

            lock (Sync)
            {
                _cache.Remove(MemoryUtilities.CombinedKey(cacheKey, region));
            }
            return(Task.FromResult(true));
        }
        private static async Task <bool> CreateUpdateItem(object cacheKey, object cacheObject, string region, CacheItemPolicy cacheItemPolicy, ICacheOptions cacheOptions)
        {
            var key       = MemoryUtilities.CombinedKey(cacheKey, region);
            var cacheItem = new CacheObject
            {
                Item = await MemoryStreamHelper.SerializeObject(cacheObject)
            };

            bool results;

            lock (Sync)
            {
                results = _cache.Add(key, cacheItem, cacheItemPolicy);
            }

            return(results);
        }
        private static async Task <bool> CreateUpdateItem(object cacheKey, object cacheObject, string region, int expireCacheTime, CacheItemPolicy cacheItemPolicy, bool allowSliddingTime = false)
        {
            var cacheData = await MemoryStreamHelper.SerializeObject(cacheObject);

            var key        = MemoryUtilities.CombinedKey(cacheKey, region);
            var expireTime = DateTime.UtcNow.AddMinutes(expireCacheTime);
            var item       = new BaseModel
            {
                CacheKey          = key,
                Expires           = expireTime,
                CacheObject       = cacheData,
                AllowSliddingTime = allowSliddingTime
            };

            bool results;

            lock (Sync)
            {
                results = _cache.Add(key, item, cacheItemPolicy);
            }

            return(results);
        }
 /// <summary>
 /// Check if the item exist
 /// </summary>
 /// <param name="cacheKey"></param>
 /// <param name="region"></param>
 /// <returns>true false</returns>
 public override Task <bool> Exist(object cacheKey, string region)
 {
     return(!_isEnabled?
            Task.FromResult(false) :
                Task.FromResult(_cache[MemoryUtilities.CombinedKey(cacheKey, region)] != null));
 }