Пример #1
0
        /// <summary>
        /// Caches an object
        /// </summary>
        /// <param name="key">A unique key to cache the object under</param>
        /// <param name="obj">The object to cache</param>
        /// <param name="cachingOptions">Options on how to cache</param>
        public static void Add(string key, object obj, ICachingOptions cachingOptions)
        {
            if (string.IsNullOrEmpty(key))
            {
                throw new ArgumentNullException("key");
            }

            if (obj == null)
            {
                throw new ArgumentNullException("obj");
            }

            if (cachingOptions == null)
            {
                throw new ArgumentNullException("cachingOptions");
            }

            CacheItemPolicy cacheItemPolicy = new CacheItemPolicy
            {
                Priority           = CacheItemPriority.Default,
                AbsoluteExpiration = DateTime.Now.AddMilliseconds(cachingOptions.Expiration.TotalMilliseconds)
            };

            if (cachingOptions.OnRemoved != null)
            {
                cacheItemPolicy.RemovedCallback += x => cachingOptions.OnRemoved(x.RemovedReason);
            }

            CacheLock.EnterWriteLock();

            Cache.Set(key, obj, cacheItemPolicy);

            CacheLock.ExitWriteLock();
        }
Пример #2
0
        /// <summary>
        /// Retrieves an item from the cache and adds it if is not there
        /// </summary>
        /// <param name="key"></param>
        /// <param name="selector"></param>
        /// <param name="cachingOptions"></param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        public static T GetOrAdd <T>(string key, Func <T> selector, ICachingOptions cachingOptions) where T : class
        {
            if (string.IsNullOrEmpty(key))
            {
                throw new ArgumentNullException("key");
            }

            if (selector == null)
            {
                throw new ArgumentNullException("selector");
            }

            if (cachingOptions == null)
            {
                throw new ArgumentNullException("cachingOptions");
            }

            T value = Get <T>(key);

            if (value != null)
            {
                return(value);
            }

            CacheItemPolicy cacheItemPolicy = new CacheItemPolicy
            {
                Priority           = CacheItemPriority.Default,
                AbsoluteExpiration = DateTime.Now.AddMilliseconds(cachingOptions.Expiration.TotalMilliseconds)
            };

            if (cachingOptions.OnRemoved != null)
            {
                cacheItemPolicy.RemovedCallback += x => cachingOptions.OnRemoved(x.RemovedReason);
            }

            try
            {
                CacheLock.EnterWriteLock();

                value = Cache[key] as T;

                if (value != null)
                {
                    return(value);
                }

                value = selector();

                Cache.Set(key, value, cacheItemPolicy);

                return(value);
            }
            finally
            {
                CacheLock.ExitWriteLock();
            }
        }