/// <summary>
 /// Helper method on ContentSerializer to 'Resolve' the serialized/compressed
 /// result using the cacheClient and cacheKey provided
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="contentSerializer">The content serializer.</param>
 /// <param name="cacheClient">The cache client.</param>
 /// <param name="cacheKey">The cache key.</param>
 /// <returns></returns>
 public static object ResolveFromCache <T>(this ContentSerializer <T> contentSerializer,
                                           ICacheClient cacheClient, string cacheKey)
     where T : class
 {
     return(Resolve(
                contentSerializer.FactoryFn,
                contentSerializer.MimeType,
                contentSerializer.CompressionType,
                cacheClient,
                cacheKey,
                null));
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Helper method on ContentSerializer to 'Resolve' the serialized/compressed
        /// result using the cacheClient and cacheKey provided
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="contentSerializer">The content serializer.</param>
        /// <param name="cacheClient">The cache client.</param>
        /// <param name="cacheKey">The cache key.</param>
        /// <returns></returns>
        public static object ResolveFromCache <T>(this ContentSerializer <T> contentSerializer,
                                                  ICacheClient cacheClient, string cacheKey)
            where T : class
        {
            var serializationContext = new SerializationContext(contentSerializer.ContentType)
            {
                CompressionType = contentSerializer.CompressionType
            };

            return(Resolve(
                       contentSerializer.FactoryFn,
                       serializationContext,
                       cacheClient,
                       cacheKey,
                       null));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Returns a serialized dto based on the mime-type, created using the factoryFn().
        /// If the correct mimeType is found, it will return the cached result.
        /// If a compressionType is set it will return a compressed version of the result.
        /// If no result is found, the dto is created by the factoryFn()
        /// The dto is set on the cacheClient[cacheKey] =&gt; factoryFn()
        /// e.g. urn:user:1 =&gt; dto
        /// The serialized dto is set on the cacheClient[cacheKey.mimeType] =&gt; serialized(factoryFn())
        /// e.g. urn:user:1.xml =&gt; xmlDto
        /// Finally, if a compressionType is specified, the compressed dto is set on the
        /// cacheClient[cacheKey.mimeType.compressionType] =&gt; compressed(serialized(factoryFn()))
        /// e.g. urn:user:1.xml.gzip =&gt; compressedXmlDto
        /// </summary>
        /// <typeparam name="TResult">The type of the result.</typeparam>
        /// <param name="factoryFn">The factory fn.</param>
        /// <param name="serializationContext">The serialization context.</param>
        /// <param name="compressionType">Type of the compression. -optional null means no compression</param>
        /// <param name="cacheClient">The cache client</param>
        /// <param name="cacheKey">The base cache key</param>
        /// <param name="expireCacheIn">How long to cache for, default is no expiration</param>
        /// <returns></returns>
        public static object Resolve <TResult>(
            Func <TResult> factoryFn,
            IRequestContext serializationContext,
            ICacheClient cacheClient,
            string cacheKey,
            TimeSpan?expireCacheIn)
            where TResult : class
        {
            factoryFn.ThrowIfNull("factoryFn");
            serializationContext.ThrowIfNull("mimeType");
            cacheClient.ThrowIfNull("cacheClient");
            cacheKey.ThrowIfNull("cacheKey");

            var    contentType = serializationContext.ResponseContentType;
            var    compressionType = serializationContext.CompressionType;
            string modifiers = null, jsonp = null;

            if (contentType == ContentType.Json)
            {
                jsonp = serializationContext.Get <IHttpRequest>().GetJsonpCallback();
                if (jsonp != null)
                {
                    modifiers = ".jsonp," + jsonp.SafeVarName();
                }
            }

            var cacheKeySerialized = GetSerializedCacheKey(cacheKey, contentType, modifiers);

            var doCompression = compressionType != null;

            var cacheKeySerializedZip = GetCompressedCacheKey(
                cacheKeySerialized, compressionType);

            if (doCompression)
            {
                var compressedResult = cacheClient.Get <byte[]>(cacheKeySerializedZip);
                if (compressedResult != null)
                {
                    return(new CompressedResult(
                               compressedResult,
                               compressionType,
                               contentType));
                }
            }
            else
            {
                var serializedResult = cacheClient.Get <string>(cacheKeySerialized);
                if (serializedResult != null)
                {
                    return(serializedResult);
                }
            }

            var dto = factoryFn();

            CacheSet(cacheClient, cacheKey, dto, expireCacheIn);

            var serializedDto = ContentSerializer <TResult> .ToSerializedString(dto, serializationContext);

            if (jsonp != null)
            {
                serializedDto = jsonp + "(" + serializedDto + ")";
            }

            CacheSet(cacheClient, cacheKeySerialized, serializedDto, expireCacheIn);

            if (doCompression)
            {
                var compressedSerializedDto = ContentSerializer <TResult> .ToCompressedBytes(
                    serializedDto, compressionType);

                CacheSet(cacheClient, cacheKeySerializedZip, compressedSerializedDto, expireCacheIn);

                return((compressedSerializedDto != null)
                                        ? new CompressedResult(compressedSerializedDto, compressionType, contentType)
                                        : null);
            }

            return(serializedDto);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Returns a serialized dto based on the mime-type, created using the factoryFn().
        /// If the correct mimeType is found, it will return the cached result.
        /// If a compressionType is set it will return a compressed version of the result.
        /// If no result is found, the dto is created by the factoryFn()
        /// The dto is set on the cacheClient[cacheKey] =&gt; factoryFn()
        /// e.g. urn:user:1 =&gt; dto
        /// The serialized dto is set on the cacheClient[cacheKey.mimeType] =&gt; serialized(factoryFn())
        /// e.g. urn:user:1.xml =&gt; xmlDto
        /// Finally, if a compressionType is specified, the compressed dto is set on the
        /// cacheClient[cacheKey.mimeType.compressionType] =&gt; compressed(serialized(factoryFn()))
        /// e.g. urn:user:1.xml.gzip =&gt; compressedXmlDto
        /// </summary>
        /// <typeparam name="TResult">The type of the result.</typeparam>
        /// <param name="factoryFn">The factory fn.</param>
        /// <param name="serializationContext">The serialization context.</param>
        /// <param name="compressionType">Type of the compression. -optional null means no compression</param>
        /// <param name="cacheClient">The cache client</param>
        /// <param name="cacheKey">The base cache key</param>
        /// <param name="expireCacheIn">How long to cache for, default is no expiration</param>
        /// <returns></returns>
        public static object Resolve <TResult>(
            Func <TResult> factoryFn,
            IRequestContext serializationContext,
            ICacheClient cacheClient,
            string cacheKey,
            TimeSpan?expireCacheIn)
            where TResult : class
        {
            factoryFn.ThrowIfNull("factoryFn");
            serializationContext.ThrowIfNull("mimeType");
            cacheClient.ThrowIfNull("cacheClient");
            cacheKey.ThrowIfNull("cacheKey");

            var contentType        = serializationContext.ResponseContentType;
            var compressionType    = serializationContext.CompressionType;
            var cacheKeySerialized = GetSerializedCacheKey(cacheKey, contentType);

            var doCompression = compressionType != null;

            var cacheKeySerializedZip = GetCompressedCacheKey(
                cacheKeySerialized, compressionType);

            if (doCompression)
            {
                var compressedResult = cacheClient.Get <byte[]>(cacheKeySerializedZip);
                if (compressedResult != null)
                {
                    return(new CompressedResult(
                               compressedResult,
                               compressionType,
                               contentType));
                }
            }
            else
            {
                var serializedResult = cacheClient.Get <string>(cacheKeySerialized);
                if (serializedResult != null)
                {
                    return(serializedResult);
                }
            }

            var dto = factoryFn();

            CacheSet(cacheClient, cacheKey, dto, expireCacheIn);

            var serializedDto = ContentSerializer <TResult> .ToSerializedString(dto, serializationContext);

            CacheSet(cacheClient, cacheKeySerialized, serializedDto, expireCacheIn);

            if (doCompression)
            {
                var compressedSerializedDto = ContentSerializer <TResult> .ToCompressedResult(
                    serializedDto, compressionType);

                CacheSet(cacheClient, cacheKeySerializedZip, compressedSerializedDto, expireCacheIn);

                return((compressedSerializedDto != null)
                                        ? new CompressedResult(compressedSerializedDto, compressionType, contentType)
                                        : null);
            }

            return(serializedDto);
        }