public ObjectCacheWrapper(CacheIdentity id = null,
                           Func <string, object, CacheItemPolicy> cacheItemPolicySelector = null)
     : this(
         new MemoryCache(id != null ? id.Name : CreateUniqueCacheName()),
         instanceName : (id != null ? id.InstanceName : null),
         cacheItemPolicySelector : cacheItemPolicySelector)
 {
 }
        protected CacheBase(CacheIdentity id)
        {
            if (id == null)
            {
                throw new ArgumentNullException("id");
            }

            Id = id;
            PartionKeyPrefix = String.Format("{0}:", Id);
        }
Exemple #3
0
        /// <summary>
        /// Creates an instance from a pre-existing <see cref="ConcurrentDictionary{TKey,TValue}"/>
        /// </summary>
        public SimpleInmemoryCache(ConcurrentDictionary <string, object> cache, CacheIdentity id = null)
            : base(id ?? CreateUniqueCacheName())
        {
            if (cache == null)
            {
                throw new ArgumentNullException("cache");
            }

            _inmemoryCache = cache;
        }
Exemple #4
0
        private bool SetIsPaused(CacheIdentity cacheId, bool isPaused)
        {
            IPausableCache cache = PausableCaches.SingleOrDefault(c => c.Id == cacheId);

            if (cache == null)
            {
                return(false);
            }

            cache.IsPaused = isPaused;
            return(true);
        }
Exemple #5
0
        /// <summary>
        /// Deletes the item from the cache identified by it's <see cref="ICache.Id" />
        /// </summary>
        /// <param name="cacheId">The unique id of the cache to remove item from </param>
        /// <param name="key">The key associated with the cached item to be removed</param>
        /// <returns>True if a cache was found matching the id supplied</returns>
        /// <remarks>
        /// <c>true</c> will be returned even if the item was not in the cache
        /// </remarks>
        /// <exception cref="ArgumentException">Cache is paused</exception>
        public bool RemoveItem(CacheIdentity cacheId, string key)
        {
            ICache cache = AllCaches.SingleOrDefault(c => c.Id == cacheId);

            if (cache == null)
            {
                return(false);
            }

            if (cache.As <IPausableCache>() != null && cache.As <IPausableCache>().IsPaused)
            {
                throw new ArgumentException(string.Format("Cannot remove item from paused cache ('{0}')", cacheId));
            }

            cache.Remove(key);
            return(true);
        }
        public virtual ICache Get <T>(CacheIdentity cacheId) where T : class
        {
            if (!_cacheConstructors.ContainsKey(typeof(T)))
            {
                throw new ArgumentException(string.Format(ExMsg, typeof(T).FullName));
            }
            if (cacheId == null)
            {
                throw new ArgumentNullException("cacheId");
            }

            ICache cache = _caches.GetOrAdd(cacheId.ToString(),
                                            id => {
                Func <CacheIdentity, ICache> ctor = _cacheConstructors[typeof(T)];
                ICache c = ctor(id);
                if (CacheAdministator != null)
                {
                    return(CacheAdministator.Register(c));
                }
                return(c);
            });

            return(cache);
        }
Exemple #7
0
 public SimpleInmemoryCache(CacheIdentity id = null) : this(new ConcurrentDictionary <string, object>(), id)
 {
 }
Exemple #8
0
 /// <summary>
 /// Pauses the cache
 /// </summary>
 /// <param name="cacheId">The unique id of the instance to pause</param>
 /// <returns>
 /// True if a <em>pausable</em> cache was found matching the name supplied
 /// </returns>
 /// <remarks>
 /// <c>true</c> will be returned even if the cache is already paused
 /// </remarks>
 public virtual bool PauseCache(CacheIdentity cacheId)
 {
     return(SetIsPaused(cacheId, true));
 }
Exemple #9
0
 /// <summary>
 /// Starts the cache
 /// </summary>
 /// <param name="cacheId">The unique id of the instance to start</param>
 /// <returns>
 /// True if a <em>pausable</em> cache was found matching the name supplied
 /// </returns>
 /// <remarks>
 /// <c>true</c> will be returned even if the cache is already started
 /// </remarks>
 public virtual bool StartCache(CacheIdentity cacheId)
 {
     return(SetIsPaused(cacheId, false));
 }