Ejemplo n.º 1
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);
        }