Пример #1
0
 private void LoadCacheItems()
 {
     var fileNames = _fs.GetFileNames(FileSystemCache.CacheDirectory, "*.dat");
     foreach (var file in fileNames)
     {
         var fqfn = FileSystemCache.GetFullyQualifiedFileName(file);
         var info = new FileCacheItemInfo(file) { LastUsed = _fs.GetLastAccessTime(fqfn), FullyQualifiedFileName = fqfn, Size = _fs.GetFileSize(fqfn)  };
         _cacheItems.Add(info);
     }
 }
Пример #2
0
        public T Get <T>(string key) where T : class
        {
            //If the file doesn't exist then the item has expired and been deleted so get out
            var fileName = GetFilePathFromKey(key);

            if (fileName == null || !FileSystem.FileExists(fileName))
            {
                return(default(T));
            }

            T obj;

            //If the item has expired, get out
            var cachInfo = new FileCacheItemInfo(fileName);

            if (cachInfo.IsExpired)
            {
                return(default(T));
            }

            //If the item exists in the memory cache then return it
            obj = MemoryCache.Get <T>(key);
            if (obj != default(T))
            {
                return(obj);
            }

            //The file is not yet in the memory cache so lets deserialize,
            //store it the memory cache then return it
            var cacheItemInfo = new FileCacheItemInfo(Path.GetFileName(fileName));

            //TODO: Add error handling in case we can't open the file for some reason
            try
            {
                using (var fs = FileSystem.CreateFileStream(fileName, FileMode.Open))
                {
                    var serializer = new DataContractJsonSerializer(typeof(T));
                    obj = (T)serializer.ReadObject(fs);
                }
            }
            catch (Exception)
            {
                //For now, if reading from disk fails, then we'll just ignore the exception
                return(obj);
            }


            MemoryCache.Add(obj, key, cacheItemInfo.Expiration);

            return(obj);
        }
Пример #3
0
        private void LoadCacheItems()
        {
            var fileNames = _fs.GetFileNames(FileSystemCache.CacheDirectory, "*.dat");

            foreach (var file in fileNames)
            {
                var fqfn = FileSystemCache.GetFullyQualifiedFileName(file);
                var info = new FileCacheItemInfo(file)
                {
                    LastUsed = _fs.GetLastAccessTime(fqfn), FullyQualifiedFileName = fqfn, Size = _fs.GetFileSize(fqfn)
                };
                _cacheItems.Add(info);
            }
        }