/// <summary> /// Caches the invocation. /// </summary> /// <param name="invocation">The invocation.</param> /// <param name="cacheKeyProvider">The cache key provider.</param> /// <param name="cacheItemSerializer">The cache item serializer.</param> /// <param name="cacheStorageMode">The cache storage mode.</param> private void CacheInvocation(IInvocation invocation, ICacheKeyProvider cacheKeyProvider, ICacheItemSerializer cacheItemSerializer) { string hash = cacheKeyProvider.GetCacheKeyForMethod(invocation.InvocationTarget, invocation.MethodInvocationTarget, invocation.Arguments); string hashedObjectDataType = string.Format(HASHED_DATA_TYPE_FORMAT, hash); var cacheProvider = CacheProviderFactory.Default.GetCacheProvider(); Type type = cacheProvider[hashedObjectDataType] as Type; object data = null; if (type != null && cacheProvider[hash] != null) data = cacheItemSerializer.Deserialize(cacheProvider[hash].ToString(), type, invocation.InvocationTarget, invocation.Method, invocation.Arguments); if (data == null) { invocation.Proceed(); data = invocation.ReturnValue; if (data != null) { cacheProvider.Add(hashedObjectDataType, invocation.Method.ReturnType); cacheProvider.Add(hash, cacheItemSerializer.Serialize(data, invocation.InvocationTarget, invocation.Method, invocation.Arguments)); } } else invocation.ReturnValue = data; }
/// <summary> /// Caches the invocation. /// </summary> /// <param name="invocation">The invocation.</param> /// <param name="cacheKeyProvider">The cache key provider.</param> /// <param name="cacheItemSerializer">The cache item serializer.</param> /// <param name="cacheStorageMode">The cache storage mode.</param> private void CacheInvocation(IInvocation invocation, ICacheKeyProvider cacheKeyProvider, ICacheItemSerializer cacheItemSerializer) { string hash = cacheKeyProvider.GetCacheKeyForMethod(invocation.InvocationTarget, invocation.MethodInvocationTarget, invocation.Arguments); string hashedObjectDataType = string.Format(HASHED_DATA_TYPE_FORMAT, hash); var cacheProvider = CacheProviderFactory.Default.GetCacheProvider(); Type type = cacheProvider[hashedObjectDataType] as Type; object data = null; if (type != null && cacheProvider[hash] != null) { data = cacheItemSerializer.Deserialize(cacheProvider[hash].ToString(), type, invocation.InvocationTarget, invocation.Method, invocation.Arguments); } if (data == null) { invocation.Proceed(); data = invocation.ReturnValue; if (data != null) { cacheProvider.Add(hashedObjectDataType, invocation.Method.ReturnType); cacheProvider.Add(hash, cacheItemSerializer.Serialize(data, invocation.InvocationTarget, invocation.Method, invocation.Arguments)); } } else { invocation.ReturnValue = data; } }
/// <summary> /// Gets a value from the cache. /// </summary> /// <param name="key">The cache key.</param> /// <returns>The value from cache; or null, if none was found.</returns> public object Get(string key) { TSerialized objectToDeserialize = _wrappedCacheProvider.Get(key); return(objectToDeserialize == null || objectToDeserialize.Equals(default(TSerialized)) ? null : _serializer.Deserialize(objectToDeserialize)); }
/// <summary> /// Gets a value from the cache asynchronously. /// </summary> /// <param name="key">The cache key.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <param name="continueOnCapturedContext">Whether async calls should continue on a captured synchronization context.</param> /// <returns> /// A <see cref="Task{TResult}" /> promising as Result a tuple whose first element is a value indicating whether /// the key was found in the cache, and whose second element is the value from the cache (null if not found). /// </returns> public async Task <(bool, object)> TryGetAsync(string key, CancellationToken cancellationToken, bool continueOnCapturedContext) { (bool cacheHit, TSerialized objectToDeserialize) = await _wrappedCacheProvider.TryGetAsync(key, cancellationToken, continueOnCapturedContext).ConfigureAwait(continueOnCapturedContext); return(cacheHit, cacheHit ? _serializer.Deserialize(objectToDeserialize) : null); }
/// <summary> /// Gets a value from the cache asynchronously. /// </summary> /// <param name="key">The cache key.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <param name="continueOnCapturedContext">Whether async calls should continue on a captured synchronization context.</param> /// <returns>A <see cref="Task{TResult}" /> promising as Result the value from cache; or null, if none was found.</returns> public async Task <object> GetAsync(string key, CancellationToken cancellationToken, bool continueOnCapturedContext) { TSerialized objectToDeserialize = await _wrappedCacheProvider.GetAsync(key, cancellationToken, continueOnCapturedContext).ConfigureAwait(continueOnCapturedContext); return(objectToDeserialize == null || objectToDeserialize.Equals(default(TSerialized)) ? null :_serializer.Deserialize(objectToDeserialize)); }
/// <summary> /// Gets a value from the cache asynchronously. /// </summary> /// <param name="key">The cache key.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <param name="continueOnCapturedContext">Whether async calls should continue on a captured synchronization context.</param> /// <returns>A <see cref="Task{TResult}" /> promising as Result the value from cache; or null, if none was found.</returns> public async Task <object> GetAsync(string key, CancellationToken cancellationToken, bool continueOnCapturedContext) { return(_serializer.Deserialize( await _wrappedCacheProvider.GetAsync(key, cancellationToken, continueOnCapturedContext).ConfigureAwait(continueOnCapturedContext))); }
/// <summary> /// Gets a value from the cache. /// </summary> /// <param name="key">The cache key.</param> /// <returns> /// A tuple whose first element is a value indicating whether the key was found in the cache, /// and whose second element is the value from the cache (null if not found). /// </returns> public (bool, object) TryGet(string key) { (bool cacheHit, TSerialized objectToDeserialize) = _wrappedCacheProvider.TryGet(key); return(cacheHit, cacheHit ? _serializer.Deserialize(objectToDeserialize) : null); }
/// <summary> /// Gets a value from the cache. /// </summary> /// <param name="key">The cache key.</param> /// <returns>The value from cache; or null, if none was found.</returns> public object Get(string key) { return(_serializer.Deserialize(_wrappedCacheProvider.Get(key))); }