Пример #1
0
        private void GenerateCacheIfNeed(string domain)
        {
            if (!hmacFuncCache.ContainsKey(domain))
            {
                var hmac = new HMACSHA256(Encoding.UTF8.GetBytes(domain));
                Func <string, string> onHash = (string input) =>
                {
                    var rawBytes = hmac.ComputeHash(Encoding.UTF8.GetBytes(input));
                    return(Convert.ToBase64String(rawBytes).Replace("/", "_").Replace("+", "_").Replace("=", "_"));
                };
                hmacFuncCache[domain] = onHash;
            }

            if (!onMemoryHeadCountCache.ContainsKey(domain))
            {
                onMemoryHeadCountCache[domain] = new Dictionary <Char, int>();
            }

            if (onMemoryHeadCountCache[domain].Count == 0)
            {
                // オンメモリキャッシュが空なので、ファイルがあればそこから生成する。
                var folderPaths = filePersist.DirectoryNamesInDomain(domain);
                foreach (var folderPath in folderPaths)
                {
                    var initial   = folderPath[folderPath.Length - 1];
                    var fileCount = filePersist.FileNamesInDomain(folderPath).Length;
                    onMemoryHeadCountCache[domain][initial] = fileCount;
                }
                return;
            }
        }
Пример #2
0
        public void PurgeCache(string storePath, string urlWithoutHash)
        {
            var deleteTargetPath = Path.Combine(storePath, GenerateFolderAndFilePath(urlWithoutHash, string.Empty, storePath).url);

            var filePaths = filePersist.FileNamesInDomain(deleteTargetPath);

            if (filePaths.Any())
            {
                // remove from hard cache.
                filePersist.DeleteByDomain(deleteTargetPath);

                // remove from on memory cache.
                var filePath = Path.Combine(deleteTargetPath, Path.GetFileName(filePaths[0]));
                if (pathObjectCache.ContainsKey(filePath))
                {
                    pathObjectCache.Remove(filePath);
                }
            }
        }