/// <summary> /// Saves the file with fullFilePath, uses FileMode.Create, so file create time will be rewrited if needed /// If exception has occurred while writing the file, it will delete it /// </summary> /// <param name="fullFilePath">example: "\\image_cache\\213898adj0jd0asd</param> /// <param name="cacheStream">stream to write to the file</param> /// <returns>true if file was successfully written, false otherwise</returns> protected async virtual Task <bool> InternalSaveAsync(string fullFilePath, Stream cacheStream) { using (var fileStream = new IsolatedStorageFileStream(fullFilePath, FileMode.Create, FileAccess.ReadWrite, ISF)) { try { await cacheStream.CopyToAsync(fileStream); return(true); } catch { try { // If file was not saved normally, we should delete it ISF.DeleteFile(fullFilePath); } catch { JetImageLoader.Log("[error] can not delete unsaved file: " + fullFilePath); } } } JetImageLoader.Log("[error] can not save cache to the: " + fullFilePath); return(false); }
/// <summary> /// Async gets file stream by the cacheKey (cacheKey will be converted using CacheFileNameGenerator) /// </summary> /// <param name="cacheKey">key will be used by CacheFileNameGenerator to get cache's file name</param> /// <returns>Stream of that file or null, if it does not exists</returns> public async virtual Task <Stream> LoadCacheStreamAsync(string cacheKey) { var fullFilePath = GetFullFilePath(CacheFileNameGenerator.GenerateCacheFileName(cacheKey)); if (!ISF.FileExists(fullFilePath)) { return(null); } try { var cacheFileMemoryStream = new MemoryStream(); using (var cacheFileStream = ISF.OpenFile(fullFilePath, FileMode.Open, FileAccess.Read)) { await cacheFileStream.CopyToAsync(cacheFileMemoryStream); return(cacheFileMemoryStream); } } catch { JetImageLoader.Log("[error] can not load file stream from: " + fullFilePath); return(null); } }
/// <summary> /// Checks file existence /// </summary> /// <param name="cacheKey">Will be used by CacheFileNameGenerator</param> /// <returns>true if file with cache exists, false otherwise</returns> public virtual bool IsCacheExists(string cacheKey) { var fullFilePath = GetFullFilePath(CacheFileNameGenerator.GenerateCacheFileName(cacheKey)); try { return(ISF.FileExists(fullFilePath)); } catch { JetImageLoader.Log("[error] can not check cache existence, file: " + fullFilePath); return(false); } }
/// <summary> /// Checks is cache existst and its last write time <= CacheMaxLifetimeInMillis /// </summary> /// <param name="cacheKey">Will be used by CacheFileNameGenerator</param> /// <returns>true if cache exists and alive, false otherwise</returns> public virtual bool IsCacheExistsAndAlive(string cacheKey) { var fullFilePath = GetFullFilePath(CacheFileNameGenerator.GenerateCacheFileName(cacheKey)); try { if (ISF.FileExists(fullFilePath)) { return(CacheMaxLifetimeInMillis <= 0 ? true : ((DateTime.Now - ISF.GetLastWriteTime(fullFilePath)).TotalMilliseconds < CacheMaxLifetimeInMillis)); } } catch { JetImageLoader.Log("[error] can not check is cache exists and alive, file: " + fullFilePath); } return(false); }
/// <summary> /// Removing oldest cache file (file, which last access time is smaller) /// </summary> private bool RemoveOldestCacheFile() { if (_lastAccessTimeDictionary.Count == 0) { return(false); } var oldestCacheFilePath = _lastAccessTimeDictionary.Aggregate((pair1, pair2) => (pair1.Value < pair2.Value)? pair1 : pair2).Key; if (String.IsNullOrEmpty(oldestCacheFilePath)) { return(false); } try { long fileSizeInBytes; using (var file = ISF.OpenFile(oldestCacheFilePath, FileMode.Open, FileAccess.Read)) { fileSizeInBytes = file.Length; } try { ISF.DeleteFile(oldestCacheFilePath); _lastAccessTimeDictionary.Remove(oldestCacheFilePath); CurrentCacheSizeInBytes -= fileSizeInBytes; // Updating current cache size JetImageLoader.Log("[delete] cache file " + oldestCacheFilePath); return(true); } catch { JetImageLoader.Log("[error] can not delete oldest cache file: " + oldestCacheFilePath); } } catch { JetImageLoader.Log("[error] can not get olders cache's file size: " + oldestCacheFilePath); } return(false); }
private void BeginCountCurrentCacheSize() { Task.Factory.StartNew(() => { // Pattern to match all innerFiles and innerDirectories inside absoluteDirPath var filesAndDirectoriesPattern = CacheDirectory + @"\*"; string[] cacheFileNames; try { cacheFileNames = ISF.GetFileNames(Path.Combine(CacheDirectory, filesAndDirectoriesPattern)); } catch { return; } long cacheSizeInBytes = 0; foreach (var cacheFileName in cacheFileNames) { var fullCacheFilePath = Path.Combine(CacheDirectory, cacheFileName); try { using (var file = ISF.OpenFile(fullCacheFilePath, FileMode.Open, FileAccess.Read)) { cacheSizeInBytes += file.Length; _lastAccessTimeDictionary.Add(fullCacheFilePath, DateTimeUtil.ConvertDateTimeToMillis(ISF.GetLastAccessTime(fullCacheFilePath).DateTime)); } } catch { JetImageLoader.Log("[error] can not get cache's file size: " + fullCacheFilePath); } } CurrentCacheSizeInBytes += cacheSizeInBytes; // Updating current cache size }); }