Example #1
0
        public override CacheModificationResult AddOrChange <T>(string key, CacheValueOf <T> cacheObject)
        {
            //using (new WriteLockDisposable(ThreadLocker))
            //{
            var exists          = _memoryCache.GetCacheItem(key) != null;
            var cacheItemPolicy = cacheObject.Policy.ToCacheItemPolicy();

            var entryDate    = DateTime.Now;
            var policyExpiry = cacheItemPolicy.AbsoluteExpiration.Subtract(entryDate);

            cacheItemPolicy.RemovedCallback +=
                arguments =>
                LogHelper.TraceIfEnabled(
                    GetType(),
                    "Item was removed from cache ({0}). Policy had {1}s to run when entered at {2}. Key: {3}",
                    () => arguments.RemovedReason.ToString(),
                    () => policyExpiry.TotalSeconds.ToString(),
                    () => entryDate.ToString(),
                    () => key);

            _keyTracker.TryAdd(key, key);

            if (exists)
            {
                LogHelper.TraceIfEnabled(GetType(), "Updating item with {0} left to run", () => policyExpiry.TotalSeconds.ToString());
                _memoryCache.Set(key, cacheObject, cacheItemPolicy);
                return(new CacheModificationResult(true, false));
            }
            var diff = cacheObject.Policy.ExpiryDate.Subtract(DateTimeOffset.Now);

            LogHelper.TraceIfEnabled(GetType(), "Adding item with {0} left to run", () => policyExpiry.TotalSeconds.ToString());
            _memoryCache.Add(key, cacheObject, cacheItemPolicy);
            //}
            return(new CacheModificationResult(false, true));
        }
Example #2
0
        public override CacheModificationResult AddOrChange <T>(string key, CacheValueOf <T> cacheObject)
        {
            var exists = Get <T>(key) != null;
            var items  = GetCurrent().Items;

            if (exists)
            {
                items[key] = cacheObject;
                return(new CacheModificationResult(true, false));
            }
            items.Add(key, cacheObject);
            return(new CacheModificationResult(false, true));
        }
        public override CacheModificationResult AddOrChange <T>(string key, CacheValueOf <T> cacheObject)
        {
            CacheModificationResult toReturn = null;

            _cacheStore.AddOrUpdate(
                key,
                keyForAdding =>
            {
                toReturn = new CacheModificationResult(false, true);
                return(cacheObject);
            },
                (keyForUpdating, existing) =>
            {
                toReturn = new CacheModificationResult(true, false);
                return(cacheObject);
            });

            return(toReturn);
        }
        public virtual CacheModificationResult AddOrChangeValue <T>(string key, T cacheObject)
        {
            var value = new CacheValueOf <T>(cacheObject);

            return(AddOrChange(key, value));
        }
        //public virtual CacheModificationResult AddOrChange(string key, CacheValueOf<object> cacheObject)
        //{
        //    return AddOrChange<object>(key, cacheObject);
        //}

        public abstract CacheModificationResult AddOrChange <T>(string key, CacheValueOf <T> cacheObject);
Example #6
0
 public override CacheModificationResult AddOrChange <T>(string key, CacheValueOf <T> cacheObject)
 {
     return(new CacheModificationResult(false, false));
 }