/// <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)); }
/// <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)); }
/// <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] => factoryFn() /// e.g. urn:user:1 => dto /// The serialized dto is set on the cacheClient[cacheKey.mimeType] => serialized(factoryFn()) /// e.g. urn:user:1.xml => xmlDto /// Finally, if a compressionType is specified, the compressed dto is set on the /// cacheClient[cacheKey.mimeType.compressionType] => compressed(serialized(factoryFn())) /// e.g. urn:user:1.xml.gzip => 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); }
/// <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] => factoryFn() /// e.g. urn:user:1 => dto /// The serialized dto is set on the cacheClient[cacheKey.mimeType] => serialized(factoryFn()) /// e.g. urn:user:1.xml => xmlDto /// Finally, if a compressionType is specified, the compressed dto is set on the /// cacheClient[cacheKey.mimeType.compressionType] => compressed(serialized(factoryFn())) /// e.g. urn:user:1.xml.gzip => 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); }