Example #1
0
        public T Get <T>(string cacheKey, TimeSpan slidingExpiryWindow, GetDataToCacheDelegate <T> getData, bool addToPerRequestCache = false) where T : class
        {
            //Get data from cache
            T data = _cache.Get <T>(cacheKey);

            if (data == null)
            {
                //get data from source
                data = getData();

                //only add non null data to the cache.
                if (data != null)
                {
                    if (addToPerRequestCache)
                    {
                        _cache.AddToPerRequestCache(cacheKey, data);
                    }
                    else
                    {
                        _cache.Add(cacheKey, slidingExpiryWindow, data);
                        if (debugLog)
                        {
                            log.Info(
                                string.Format("Adding item [{0}] to cache with sliding sliding expiry window in seconds [{1}].", cacheKey,
                                              slidingExpiryWindow.TotalSeconds));
                        }
                    }
                }
            }
            else
            {
                if (debugLog)
                {
                    log.Info(string.Format("Retrieving item [{0}] from cache.", cacheKey));
                }
            }
            return(data);
        }
Example #2
0
        public T Get <T>(string cacheKey, DateTime expiryDate, GetDataToCacheDelegate <T> getData, bool addToPerRequestCache = false) where T : class
        {
            //Get data from cache
            T data = _cache.Get <T>(cacheKey);

            if (data == null)
            {
                //get data from source
                data = getData();

                //only add non null data to the cache.
                if (data != null)
                {
                    if (addToPerRequestCache)
                    {
                        _cache.AddToPerRequestCache(cacheKey, data);
                    }
                    else
                    {
                        _cache.Add(cacheKey, expiryDate, data);
                        if (debugLog)
                        {
                            log.Info(string.Format("Adding item [{0}] to cache with expiry date/time of [{1}].", cacheKey,
                                                   expiryDate.ToString("dd/MM/yyyy hh:mm:ss")));
                        }
                    }
                }
            }
            else
            {
                if (debugLog)
                {
                    log.Info(string.Format("Retrieving item [{0}] from cache.", cacheKey));
                }
            }
            return(data);
        }