/// <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 async Task <bool> IsCacheExists(string cacheKey) { var fullFilePath = GetFullFilePath(CacheFileNameGenerator.GenerateCacheName(cacheKey)); try { await SF.GetFileAsync(fullFilePath); return(true); } catch { ImageLog.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 async Task <bool> IsCacheExistsAndAlive(string cacheKey) { var fullFilePath = GetFullFilePath(CacheFileNameGenerator.GenerateCacheName(cacheKey)); try { var storageFile = await SF.GetFileAsync(fullFilePath); return(CacheMaxLifetimeInMillis <= 0 ? true : ((DateTime.Now - storageFile.DateCreated.DateTime).TotalMilliseconds < CacheMaxLifetimeInMillis)); } catch { ImageLog.Log("[error] can not check is cache exists and alive, file: " + fullFilePath); } return(false); }
public async override Task <bool> SaveAsync(string cacheKey, IRandomAccessStream cacheStream) { var fullFileName = Path.Combine(CacheDirectory, CacheFileNameGenerator.GenerateCacheName(cacheKey)); var cacheSizeInBytes = cacheStream.AsStreamForRead().Length; while (CurrentCacheSizeInBytes + cacheSizeInBytes > _cacheLimitInBytes) { if (!await RemoveOldestCacheFile()) { break; // All cache deleted } } var wasFileSaved = await base.InternalSaveAsync(fullFileName, cacheStream); if (wasFileSaved) { _lastAccessTimeDictionary[Path.Combine(CacheDirectory, fullFileName)] = DateTimeHelper.CurrentTimeMillis(); CurrentCacheSizeInBytes += cacheSizeInBytes; // Updating current cache size } return(wasFileSaved); }
/// <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 <IRandomAccessStream> LoadCacheStreamAsync(string cacheKey) { var fullFilePath = GetFullFilePath(CacheFileNameGenerator.GenerateCacheName(cacheKey)); try { var cacheFileMemoryStream = new InMemoryRandomAccessStream(); var storageFile = await SF.GetFileAsync(fullFilePath); using (var cacheFileStream = await storageFile.OpenAsync(FileAccessMode.Read)) { await RandomAccessStream.CopyAsync( cacheFileStream.GetInputStreamAt(0L), cacheFileMemoryStream.GetOutputStreamAt(0L)); return(cacheFileMemoryStream); } } catch (Exception ex) { ImageLog.Log("[error] can not load file stream from: " + fullFilePath); return(null); } }
/// <summary> /// Just calls StorageCacheBase.InternalSaveAsync() without any other operation /// </summary> /// <param name="cacheKey">will be used by CacheFileNameGenerator</param> /// <param name="cacheStream">will be written to the cache file</param> /// <returns>true if cache was saved, false otherwise</returns> public override Task <bool> SaveAsync(string cacheKey, IRandomAccessStream cacheStream) { return(InternalSaveAsync(CacheFileNameGenerator.GenerateCacheName(cacheKey), cacheStream)); }