Exemplo n.º 1
0
        /// <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;
        }
Exemplo n.º 2
0
 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;
        }
Exemplo n.º 4
0
 /// <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&lt;TResult, TSerialized&gt;.</returns>
 public static AsyncSerializingCacheProvider <TResult, TSerialized> WithSerializer <TResult, TSerialized>(
     this IAsyncCacheProvider <TSerialized> cacheProvider, ICacheItemSerializer <TResult, TSerialized> serializer)
 => new AsyncSerializingCacheProvider <TResult, TSerialized>(cacheProvider, serializer);
Exemplo n.º 5
0
 /// <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&lt;TResult, TSerialized&gt;.</returns>
 public static SerializingCacheProvider <TSerialized> WithSerializer <TSerialized>(
     this ISyncCacheProvider <TSerialized> cacheProvider, ICacheItemSerializer <object, TSerialized> serializer)
 => new SerializingCacheProvider <TSerialized>(cacheProvider, serializer);
Exemplo n.º 6
0
        /// <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;
            }
        }
Exemplo n.º 7
0
 /// <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));
 }
Exemplo n.º 8
0
 /// <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&lt;TResult, TSerialized&gt;.</returns>
 public static SerializingCacheProviderAsync <TResult, TSerialized> WithSerializer <TResult, TSerialized>(
     this IAsyncCacheProvider <TSerialized> cacheProvider, ICacheItemSerializer <TResult, TSerialized> serializer)
 {
     return(new SerializingCacheProviderAsync <TResult, TSerialized>(cacheProvider, serializer));
 }