コード例 #1
0
        /// <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);
            }
        }
コード例 #2
0
        /// <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);
            }
        }
コード例 #3
0
        /// <summary>
        /// Checks is cache existst and its last write time &lt;= 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);
        }
コード例 #4
0
        public async override Task <bool> SaveAsync(string cacheKey, Stream cacheStream)
        {
            var fullFileName     = Path.Combine(CacheDirectory, CacheFileNameGenerator.GenerateCacheFileName(cacheKey));
            var cacheSizeInBytes = cacheStream.Length;

            while (CurrentCacheSizeInBytes + cacheSizeInBytes > _cacheLimitInBytes)
            {
                if (!RemoveOldestCacheFile())
                {
                    break; // All cache deleted
                }
            }

            var wasFileSaved = await base.InternalSaveAsync(fullFileName, cacheStream);

            if (wasFileSaved)
            {
                _lastAccessTimeDictionary[Path.Combine(CacheDirectory, fullFileName)] = DateTimeUtil.CurrentTimeMillis();
                CurrentCacheSizeInBytes += cacheStream.Length; // Updating current cache size
            }

            return(wasFileSaved);
        }
コード例 #5
0
 /// <summary>
 /// Just calls BaseStorageCache.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, Stream cacheStream)
 {
     return(InternalSaveAsync(CacheFileNameGenerator.GenerateCacheFileName(cacheKey), cacheStream));
 }