/// <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; }
protected virtual string SerializeArgument( string argumentName, object argumentValue, ICacheItemSerializer <object, string> serializer, MethodInfo methodInfo, IDictionary <string, Type> parameterTypes, IDictionary <string, object> arguments) { return(serializer.Serialize(argumentValue)); }
/// <summary> /// Initializes a new instance of the <see cref="SerializingCacheProviderAsync{TResult, TSerialized}"/> class. /// </summary> /// <param name="wrappedCacheProvider">The wrapped cache provider.</param> /// <param name="serializer">The serializer.</param> /// <exception cref="System.ArgumentNullException">wrappedCacheProvider </exception> /// <exception cref="System.ArgumentNullException">serializer </exception> public SerializingCacheProviderAsync(IAsyncCacheProvider <TSerialized> wrappedCacheProvider, ICacheItemSerializer <object, TSerialized> serializer) { if (wrappedCacheProvider == null) { throw new ArgumentNullException(nameof(wrappedCacheProvider)); } if (serializer == null) { throw new ArgumentNullException(nameof(serializer)); } _wrappedCacheProvider = wrappedCacheProvider; _serializer = serializer; }
/// <summary> /// Wraps the <paramref name="serializer"/> around the asynchronous <paramref name="cacheProvider"/> so that delegate return values of type <typeparamref name="TResult"/> can be stored in the cache as type <typeparamref name="TSerialized"/>. /// </summary> /// <typeparam name="TResult">The return type of delegates which may be executed through the policy.</typeparam> /// <typeparam name="TSerialized">The type of serialized objects to be placed in the cache.</typeparam> /// <param name="cacheProvider">The cache provider.</param> /// <param name="serializer">The serializer.</param> /// <returns>SerializingCacheProvider<TResult, TSerialized>.</returns> public static AsyncSerializingCacheProvider <TResult, TSerialized> WithSerializer <TResult, TSerialized>( this IAsyncCacheProvider <TSerialized> cacheProvider, ICacheItemSerializer <TResult, TSerialized> serializer) => new AsyncSerializingCacheProvider <TResult, TSerialized>(cacheProvider, serializer);
/// <summary> /// Wraps the <paramref name="serializer"/> around the <paramref name="cacheProvider"/> so that delegate return values of any type can be stored in the cache as type <typeparamref name="TSerialized"/>. /// </summary> /// <typeparam name="TSerialized">The type of serialized objects to be placed in the cache.</typeparam> /// <param name="cacheProvider">The cache provider.</param> /// <param name="serializer">A serializer which can serialize/deserialize all types to/from <typeparamref name="TSerialized"/>.</param> /// <returns>SerializingCacheProvider<TResult, TSerialized>.</returns> public static SerializingCacheProvider <TSerialized> WithSerializer <TSerialized>( this ISyncCacheProvider <TSerialized> cacheProvider, ICacheItemSerializer <object, TSerialized> serializer) => new SerializingCacheProvider <TSerialized>(cacheProvider, serializer);
/// <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> /// Initializes a new instance of the <see cref="SerializingCacheProvider{TResult, TSerialized}" /> class. /// </summary> /// <param name="wrappedCacheProvider">The wrapped cache provider.</param> /// <param name="serializer">The serializer.</param> /// <exception cref="System.ArgumentNullException">wrappedCacheProvider </exception> /// <exception cref="System.ArgumentNullException">serializer </exception> public SerializingCacheProvider(ISyncCacheProvider <TSerialized> wrappedCacheProvider, ICacheItemSerializer <object, TSerialized> serializer) { _wrappedCacheProvider = wrappedCacheProvider ?? throw new ArgumentNullException(nameof(wrappedCacheProvider)); _serializer = serializer ?? throw new ArgumentNullException(nameof(serializer)); }
/// <summary> /// Wraps the <paramref name="serializer"/> around the asynchronous <paramref name="cacheProvider"/> so that delegate return values of type <typeparamref name="TResult"/> can be stored in the cache as type <typeparamref name="TSerialized"/>. /// </summary> /// <typeparam name="TResult">The return type of delegates which may be executed through the policy.</typeparam> /// <typeparam name="TSerialized">The type of serialized objects to be placed in the cache.</typeparam> /// <param name="cacheProvider">The cache provider.</param> /// <param name="serializer">The serializer.</param> /// <returns>SerializingCacheProvider<TResult, TSerialized>.</returns> public static SerializingCacheProviderAsync <TResult, TSerialized> WithSerializer <TResult, TSerialized>( this IAsyncCacheProvider <TSerialized> cacheProvider, ICacheItemSerializer <TResult, TSerialized> serializer) { return(new SerializingCacheProviderAsync <TResult, TSerialized>(cacheProvider, serializer)); }