コード例 #1
0
        /// <summary>Resolves.</summary>
        ///
        /// <typeparam name="T">Generic type parameter.</typeparam>
        /// <param name="compressionType">Type of the compression.</param>
        /// <param name="cacheKey">       The cache key.</param>
        /// <param name="createCacheFn">  The create cache function.</param>
        ///
        /// <returns>An object.</returns>
        public object Resolve <T>(string compressionType, string cacheKey, Func <T> createCacheFn)
            where T : class
        {
            var contentTypeCacheKey = cacheKey + MimeTypes.GetExtension(this.ContentType);

            var acceptsCompress = !string.IsNullOrEmpty(compressionType);
            var cacheKeyZip     = acceptsCompress
                ? contentTypeCacheKey + CompressionTypes.GetExtension(compressionType)
                : null;

            if (acceptsCompress)
            {
                var fromCache = GetCompressedBytesFromCache(compressionType, cacheKeyZip);
                if (fromCache != null)
                {
                    return(fromCache);
                }

                var fromFile = GetCompressedBytesFromFile(compressionType, cacheKeyZip);
                if (fromFile != null)
                {
                    return(fromFile);
                }
            }
            else
            {
                var result = this.CacheClient.Get <string>(cacheKey);
                if (result != null)
                {
                    return(result);
                }

                var fromFile = GetFromFile(contentTypeCacheKey);
                if (fromFile != null)
                {
                    return(fromFile);
                }
            }

            var cacheValueString = this.cacheManager.ResolveText(cacheKey, createCacheFn);

            WriteToFile(contentTypeCacheKey, Encoding.UTF8.GetBytes(cacheValueString));

            if (acceptsCompress)
            {
                var cacheValueZip = cacheValueString.Compress(compressionType);

                this.CacheClient.Set(cacheKeyZip, cacheValueZip);
                WriteToFile(cacheKeyZip, cacheValueZip);

                return(new CompressedResult(cacheValueZip, compressionType, this.ContentType));
            }

            return(cacheValueString);
        }
コード例 #2
0
        public CacheKeyTuple GetCacheKeys <T>(string id)
        {
            var cacheKey = IdUtils.CreateCacheKeyPath <T>(id);

            var contentType     = cacheKey + MimeTypes.GetExtension(MimeTypes.Xml);
            var compressionType = contentType + CompressionTypes.GetExtension(CompressionTypes.Deflate);

            return(new CacheKeyTuple {
                CacheKey = cacheKey,
                ContentTypeCacheKey = contentType,
                CompressionTypeCacheKey = compressionType,
            });
        }
コード例 #3
0
        /// <summary>Clears this object to its blank/initial state.</summary>
        ///
        /// <param name="cacheKeys">A variable-length parameters list containing cache keys.</param>
        public void Clear(params string[] cacheKeys)
        {
            this.cacheManager.Clear(cacheKeys);

            var contentTypeCacheKeys = cacheKeys.ToList().ConvertAll(x => x + MimeTypes.GetExtension(this.ContentType));

            foreach (var cacheKey in contentTypeCacheKeys)
            {
                var filePath        = Path.Combine(this.baseCachePath, cacheKey);
                var gzipFilePath    = Path.Combine(this.baseCachePath, cacheKey + CompressionTypes.GetExtension(CompressionTypes.GZip));
                var deflateFilePath = Path.Combine(this.baseCachePath, cacheKey + CompressionTypes.GetExtension(CompressionTypes.Deflate));

                DeleteFiles(filePath, gzipFilePath, deflateFilePath);
            }
        }