Esempio n. 1
0
        /// <summary>
        /// Removing oldest cache file (file, which last access time is smaller)
        /// </summary>
        private async Task <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);
            }

            oldestCacheFilePath = Path.Combine(CacheDirectory, oldestCacheFilePath);

            try
            {
                long fileSizeInBytes;
                var  storageFile = await SF.GetFileAsync(oldestCacheFilePath);

                var properties = await storageFile.GetBasicPropertiesAsync();

                fileSizeInBytes = (long)properties.Size;

                try
                {
                    await storageFile.DeleteAsync();

                    _lastAccessTimeDictionary.Remove(oldestCacheFilePath);
                    CurrentCacheSizeInBytes -= fileSizeInBytes; // Updating current cache size
                    ImageLog.Log("[delete] cache file " + oldestCacheFilePath);
                    return(true);
                }
                catch
                {
                    ImageLog.Log("[error] can not delete oldest cache file: " + oldestCacheFilePath);
                }
            }
            catch (Exception)
            {
                ImageLog.Log("[error] can not get olders cache's file size: " + oldestCacheFilePath);
            }

            return(false);
        }