public long DecrementValueInHash(string hashId, string key, int count, ProviderLevel level) { var provider = CachingProvider.Instance(level); if (!provider.IsDistributedCache()) { object @lock = UniqueObject.GetUniqueObject <object>(key); lock (@lock) { var retVal = provider.DecrementValueInHash(hashId, key, count); UniqueObject.RemoveUniqueObject(key); return(retVal); } } return(provider.DecrementValueInHash(hashId, key, count)); }
public long DecrementValue(string key, Func <long> initialCallBack, int count = 1, int expiredMinutes = 0, ProviderLevel level = ProviderLevel.Normal) { var provider = CachingProvider.Instance(level); if (!provider.IsDistributedCache()) { object @lock = UniqueObject.GetUniqueObject <object>(key); lock (@lock) { var retVal = provider.DecrementValue(key, count, expiredMinutes, initialCallBack); UniqueObject.RemoveUniqueObject(key); return(retVal); } } return(provider.DecrementValue(key, count, expiredMinutes, initialCallBack)); }
private TObject GetCachedDataInternal <TObject>(CacheItemArgs cacheItemArgs, CacheItemExpiredCallback <TObject> cacheItemExpired) { TObject objObject = GetCache <TObject>(cacheItemArgs.CacheKey, cacheItemArgs.Scope, cacheItemArgs.Level); // if item is not cached if (EqualityComparer <TObject> .Default.Equals(objObject, default(TObject))) { //Get Unique Lock for cacheKey object @lock = UniqueObject.GetUniqueObject <object>(cacheItemArgs.CacheKey); // prevent other threads from entering this block while we regenerate the cache lock (@lock) { // try to retrieve object from the cache again (in case another thread loaded the object since we first checked) objObject = GetCache <TObject>(cacheItemArgs.CacheKey, cacheItemArgs.Scope, cacheItemArgs.Level); // if object was still not retrieved if (EqualityComparer <TObject> .Default.Equals(objObject, default(TObject))) { // get object from data source using delegate //try //{ objObject = cacheItemExpired(cacheItemArgs); _logger.LogInformation("GetCacheItemFromExpiredCallback:" + cacheItemArgs.CacheKey); //} //catch (Exception ex) //{ // objObject = default(TObject); // _logger.LogError(ex, "CacheItemExpiredCallbackError"); // } // if we retrieved a valid object and we are using caching if (objObject != null) { // save the object in the cache SetCache(cacheItemArgs.CacheKey, objObject, cacheItemArgs.Scope, cacheItemArgs.ExpireTimeOutMinutes > 0 ? DateTimeOffset.Now.AddMinutes(cacheItemArgs.ExpireTimeOutMinutes) : (DateTimeOffset?)null, cacheItemArgs.CacheTimeOutMinutes > 0 ? TimeSpan.FromMinutes(cacheItemArgs.CacheTimeOutMinutes) : (TimeSpan?)null, cacheItemArgs.Level, cacheItemArgs.CacheDependency, cacheItemArgs.CacheCallback); // check if the item was actually saved in the cache if (GetCache <TObject>(cacheItemArgs.CacheKey, cacheItemArgs.Scope, cacheItemArgs.Level) == null) { // log the event if the item was not saved in the cache ( likely because we are out of memory ) _logger.LogError("CACHE_OVERFLOW:" + cacheItemArgs.CacheKey + ":Overflow - Item Not Cached"); } } //This thread won so remove unique Lock from collection UniqueObject.RemoveUniqueObject(cacheItemArgs.CacheKey); } } } return(objObject); }