public virtual Task Set(string key, ICachable value, CacheEntryOptions entryOptions) { Condition.Requires(key, nameof(key)).IsNotNullOrEmpty(); Condition.Requires(value, nameof(value)).IsNotNull(); Condition.Requires(entryOptions, nameof(entryOptions)).IsNotNull(); _syncClearLock.EnterReadLock(); try { if (CheckSize(value)) { var memoryCacheEntryOptions = CreateMemoryCacheEntryOptions(entryOptions); Interlocked.Add(ref _memoryUsed, value.SizeInBytes); Interlocked.Increment(ref _itemsCount); _memoryCache.Set(key, value, memoryCacheEntryOptions); _logger.LogDebug("{Operation} => key : {Key} | value : {Value}", "set", key, value.Value); } else { _logger.LogWarning("{CacheStore} Cache is full, cannot add item ({Key}) to cache.", Name, key); } } finally { _syncClearLock.ExitReadLock(); } return(CompletedTask); }
public static T Get <T> ([NotNull] this ICacheProvider cache, [NotNull] ICachable <T> cachableObject) { if (cachableObject == null) { throw new ArgumentNullException(nameof(cachableObject)); } return(cache.Get(cachableObject.GetCacheKey(), cachableObject.GetCachedValue)); }
protected virtual bool CheckSize(ICachable cachable) { if (cachable.SizeInBytes + _memoryUsed > _maxSize) { Compact(); return(false); } return(true); }
public virtual async Task Set(string key, ICachable value, CacheEntryOptions entryOptions) { Condition.Requires(key, nameof(key)); Condition.Requires(value, nameof(value)); Condition.Requires(entryOptions, nameof(entryOptions)); var distributedCacheEntryOptions = CreateDistributedCacheEntryOptions(entryOptions); var serializedValue = _objectSerializer.Serialize(value.Value); await _redisCache.SetAsync(key, serializedValue, distributedCacheEntryOptions); _logger.LogDebug("{Operation} => key : {Key} | value : {Value}", "set", key, value.Value); }
/// <summary> /// Complete the operation and invoke events. /// </summary> /// <param name="result">The result object for the operation.</param> /// <param name="success">True if successful.</param> /// <param name="errorMsg">The error message if the operation has failed.</param> public void Complete(TObject result, bool success, string errorMsg) { IUpdateReceiver upOp = this as IUpdateReceiver; if (m_UpdateCallbacks != null && upOp != null) { m_UpdateCallbacks.Remove(m_UpdateCallback); } m_Result = result; m_Status = success ? AsyncOperationStatus.Succeeded : AsyncOperationStatus.Failed; m_RM.PostDiagnosticEvent(new ResourceManager.DiagnosticEventContext(new AsyncOperationHandle(this), ResourceManager.DiagnosticEventType.AsyncOperationPercentComplete, 1)); m_RM.PostDiagnosticEvent(new ResourceManager.DiagnosticEventContext(new AsyncOperationHandle(this), ResourceManager.DiagnosticEventType.AsyncOperationComplete)); if (m_Status == AsyncOperationStatus.Failed) { if (string.IsNullOrEmpty(errorMsg)) { errorMsg = "Unknown error in AsyncOperation"; } m_RM.PostDiagnosticEvent(new ResourceManager.DiagnosticEventContext(new AsyncOperationHandle(this), ResourceManager.DiagnosticEventType.AsyncOperationFail, 0, errorMsg)); OperationException = new Exception(errorMsg); ICachable cachedOperation = this as ICachable; if (cachedOperation != null) { m_RM.RemoveOperationFromCache(cachedOperation.Hash); } RegisterForDeferredCallbackEvent(false); } else { InvokeCompletionEvent(); DecrementReferenceCount(); } }
public virtual Task Set(string key, ICachable value, CacheEntryOptions entryOptions = null) { Condition.Requires(key, nameof(key)).IsNotNull(); Condition.Requires(value, nameof(value)).IsNotNull(); if (entryOptions == null) { entryOptions = new CacheEntryOptions(); } var prefixedKey = CreateKey(key); entryOptions.Priority = entryOptions.Priority ?? _defaultPriority; lock (_keysLock) { if (!_keys.Contains(key)) { _keys.Add(key); } } return(_store.Set(prefixedKey, value, entryOptions)); }
/// <summary> /// Assigns this identifier to the tags property of the cache item /// </summary> /// <param name="cache"></param> /// <param name="type"></param> /// <param name="identifier"></param> public static void AddTag(this ICachable cache, CacheTags type, dynamic identifier) { cache.Tags.Add(FormatTag(type, identifier)); }
public Task Set(string key, ICachable value, CacheEntryOptions entryOptions = null) { return(_cache.Set(key, value, entryOptions)); }
protected static void SaveObjectToCache(ICachable cache, string keyPrefix, int id, object val) { string key = GetKey(keyPrefix, id); cache.Insert(key, val); }
protected static void RemoveObjectFromCache(ICachable cache, string keyPrefix, int id) { string key = GetKey(keyPrefix, id); cache.Remove(key); }
/// <summary> /// Complete the operation and invoke events. /// </summary> /// <remarks> /// An operation is considered to have failed silently if success is true and if exception isn't null. /// The exception handler will be called in cases of silent failures. /// </remarks> /// <param name="result">The result object for the operation.</param> /// <param name="success">True if successful or if the operation failed silently.</param> /// <param name="exception">The exception if the operation has failed.</param> /// <param name="releaseDependenciesOnFailure">When true, failed operations will release any dependencies that succeeded.</param> public void Complete(TObject result, bool success, Exception exception, bool releaseDependenciesOnFailure = true) { if (IsDone) { return; } IUpdateReceiver upOp = this as IUpdateReceiver; if (m_UpdateCallbacks != null && upOp != null) { m_UpdateCallbacks.Remove(m_UpdateCallback); } Result = result; m_Status = success ? AsyncOperationStatus.Succeeded : AsyncOperationStatus.Failed; if (m_RM != null && m_RM.postProfilerEvents) { m_RM.PostDiagnosticEvent(new ResourceManager.DiagnosticEventContext(new AsyncOperationHandle(this), ResourceManager.DiagnosticEventType.AsyncOperationPercentComplete, 1)); m_RM.PostDiagnosticEvent(new ResourceManager.DiagnosticEventContext(new AsyncOperationHandle(this), ResourceManager.DiagnosticEventType.AsyncOperationComplete)); } if (m_Status == AsyncOperationStatus.Failed || exception != null) { if (exception == null || string.IsNullOrEmpty(exception.Message)) { OperationException = new OperationException($"Unknown error in AsyncOperation : {DebugName}"); } else { OperationException = exception; } } if (m_Status == AsyncOperationStatus.Failed) { if (releaseDependenciesOnFailure) { ReleaseDependencies(); } if (m_RM != null && m_RM.postProfilerEvents) { m_RM.PostDiagnosticEvent(new ResourceManager.DiagnosticEventContext(new AsyncOperationHandle(this), ResourceManager.DiagnosticEventType.AsyncOperationFail, 0, exception?.ToString())); } ICachable cachedOperation = this as ICachable; if (cachedOperation?.Key != null) { m_RM?.RemoveOperationFromCache(cachedOperation.Key); } RegisterForDeferredCallbackEvent(false); } else { InvokeCompletionEvent(); DecrementReferenceCount(); } IsRunning = false; }