public void Set <TObj>(string key, TObj obj, TimeSpan timeSpan, CacheDurationType cacheDuration)
 {
     if (obj != null && !string.IsNullOrEmpty(key))
     {
         var cacheEvictionPolicy = new CacheEvictionPolicy(timeSpan, _typeConveter[cacheDuration]);
         _syncronizedObjectInstanceCache.Insert(key, obj, cacheEvictionPolicy);
         _keyCache.Set(key, DateTime.Now.Add(timeSpan), timeSpan, cacheDuration);
     }
 }
Exemple #2
0
        public void Set <TObj>(string key, TObj obj, TimeSpan timeSpan, CacheDurationType cacheDuration)
        {
            if (string.IsNullOrEmpty(key) && obj == null)
            {
                return;
            }
            var cacheItemPolicy = CreateCacheItemPolicy(cacheDuration, timeSpan);

            _cache.Set(key, obj, cacheItemPolicy);
        }
Exemple #3
0
        private CacheItemPolicy CreateCacheItemPolicy(CacheDurationType cacheDuration, TimeSpan timeSpan)
        {
            CacheItemPolicy policy = new CacheItemPolicy();

            switch (cacheDuration)
            {
            case CacheDurationType.Absolute:
                policy.AbsoluteExpiration = DateTimeOffset.Now.Add(timeSpan);
                break;

            case CacheDurationType.Sliding:
                policy.SlidingExpiration = timeSpan;
                break;
            }
            return(policy);
        }
Exemple #4
0
 protected static async Task<T> GetAsync<T>(string endpoint, string identifier, HybridCache.GetLiveEndpointResultDelegate<T> cacheSetCall,
     DateTimeOffset cacheDuration, CacheDurationType cacheDurationType = CacheDurationType.Absolute,
     bool persistInMemory = true, bool persistOnDisk = false) where T : class {
     return await apiCache.GetEntryFromCache<T>(endpoint, identifier, cacheSetCall, cacheDuration,
         cacheDurationType, persistInMemory, persistOnDisk);
 }
        public static async Task <T> GetOrCreateAsync <T>(this ICache cache, string key, Func <Task <T> > func, TimeSpan expiresIn, CacheDurationType cacheDuration, bool forceUpdate = false)
        {
            var rtnObj = cache.Get <T>(key);

            if (rtnObj != null && forceUpdate == false)
            {
                return(rtnObj);
            }

            var lockOn = LockCache.GetOrAdd(key, () => new AsyncLock());

            using (await lockOn.LockAsync(TimeOut.LockTimeOut))
            {
                rtnObj = cache.Get <T>(key);
                if (rtnObj != null && forceUpdate == false)
                {
                    return(rtnObj);
                }
                rtnObj = await func();

                cache.Set(key, rtnObj, expiresIn, cacheDuration);
                return(rtnObj);
            }
        }
Exemple #6
0
        public async Task <T> GetEntryFromCache <T>(string endpoint, string identifier, GetLiveEndpointResultDelegate <T> cacheSetCall, DateTimeOffset cacheExpiration, CacheDurationType cacheDurationType = CacheDurationType.Absolute, bool persistInMemory = true, bool persistOnDisk = false) where T : class
        {
            // Check memory cache first
            var responseItem = persistInMemory ? Get(identifier, endpoint) as T : null;

            // If not in memory cache, check file system cache
            if (responseItem == null)
            {
                responseItem = persistOnDisk ? await GetFromFsCache(endpoint, identifier, cacheSetCall, cacheExpiration, cacheDurationType) : null;

                if (responseItem == null)
                {
                    return(await cacheSetCall.Invoke(identifier, endpoint));
                }

                if (persistInMemory)
                {
                    var policy = new CacheItemPolicy();

                    if (cacheDurationType == CacheDurationType.Absolute)
                    {
                        policy.AbsoluteExpiration = cacheExpiration;
                    }
                    else if (cacheDurationType == CacheDurationType.Sliding)
                    {
                        policy.SlidingExpiration = cacheExpiration.Offset;
                    }

                    base.Set(GetCacheItemFqn(identifier, endpoint), responseItem, policy);
                }
            }

            return(responseItem);
        }
Exemple #7
0
        public async Task <T> GetFromFsCache <T>(string endpoint, string identifier, GetLiveEndpointResultDelegate <T> cacheSetCall, DateTimeOffset cacheExpiration, CacheDurationType cacheDurationType = CacheDurationType.Absolute) where T : class
        {
            string niceEndpointName = GetEndpointNiceName(endpoint);

            // Cache it to the file system, if that's enabled or available
            if (cacheExpiration.Offset.TotalSeconds > 0 && !string.IsNullOrEmpty(niceEndpointName) && !string.IsNullOrEmpty(this.CacheDirectory))
            {
                string endpointCacheRoot = $"{niceEndpointName}{CACHE_ENDPOINT_EXTENSION}";

                // TODO: For now while we aren't updating fs cache in this function
                if (!File.Exists(Path.Combine(this.CacheDirectory, endpointCacheRoot)))
                {
                    return(null);
                }

                using (var endpointFileStream = new FileStream(Path.Combine(this.CacheDirectory, endpointCacheRoot), FileMode.Open)) {
                    using (var endpointArchive = new ZipArchive(endpointFileStream, ZipArchiveMode.Update)) {
                        string entryName = identifier + CACHE_ENTRY_EXTENSION;

                        var entryFind = endpointArchive.GetEntry(entryName);

                        // Where we will store the result
                        T result;

                        if (entryFind == null || entryFind.LastWriteTime.Subtract(DateTime.Now) > cacheExpiration.Offset)
                        {
                            entryFind?.Delete();

                            result = await cacheSetCall.Invoke(identifier, endpoint);

                            // TODO: Instead of doing this now, queue it and do them all at once to avoid IO errors
                            // Add new cache entry into zip
                            //entryFind = endpointArchive.CreateEntry(entryName, CompressionLevel.Fastest);

                            //using (var entryStream = entryFind.Open()) {
                            //    ProtoBuf.Serializer.Serialize(entryStream, result);
                            //}
                        }
                        else
                        {
                            result = ProtoBuf.Serializer.Deserialize <T>(entryFind.Open());

                            // If cache duration mode is sliding, then touch the timestamp to update it from this access
                            if (cacheDurationType == CacheDurationType.Sliding)
                            {
                                entryFind.LastWriteTime = DateTimeOffset.Now;
                            }
                        }

                        return(result);
                    }
                }
            }

            return(null);
        }
Exemple #8
0
        public static T GetOrCreate <T>(this ICache cache, string key, Func <T> func, TimeSpan expiresIn, CacheDurationType cacheDuration, bool forceUpdate = false) where T : class
        {
            var rtnObj = cache.Get <T>(key);

            if (!rtnObj.IsNullOrEmpty() && forceUpdate == false)
            {
                return(rtnObj);
            }

            object lockOn = LockCache.GetOrAdd(key, () => new object());

            using (new Lock(lockOn, TimeOut.LockTimeOut))
            {
                rtnObj = cache.Get <T>(key);
                if (rtnObj != null && forceUpdate == false)
                {
                    return(rtnObj);
                }
                rtnObj = func();
                if (rtnObj != null)
                {
                    cache.Set(key, rtnObj, expiresIn, cacheDuration);
                }
                return(rtnObj);
            }
        }