This repository has been archived by the owner on Feb 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
tobiichiamane
committed
Oct 23, 2019
1 parent
13d9bb5
commit d25049c
Showing
2 changed files
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Windows.Storage; | ||
|
||
namespace PixivFSUWP.Data | ||
{ | ||
//用来管理缓存 | ||
public static class CacheManager | ||
{ | ||
//临时目录 | ||
private static StorageFolder tmpFolder = ApplicationData.Current.TemporaryFolder; | ||
|
||
//获取临时目录,确保目录存在 | ||
private static async Task<StorageFolder> getCacheFolder() | ||
{ | ||
var cacheFolder = await tmpFolder.TryGetItemAsync("Cache"); | ||
if (cacheFolder == null) return await tmpFolder.CreateFolderAsync("Cache"); | ||
else return cacheFolder as StorageFolder; | ||
} | ||
|
||
//清除缓存 | ||
public static async Task ClearCache() | ||
{ | ||
var cacheFolder = await tmpFolder.TryGetItemAsync("imgCache"); | ||
if (cacheFolder != null) await cacheFolder.DeleteAsync(); | ||
} | ||
|
||
//得到目录大小 | ||
private static async Task<long> getFolderSize(StorageFolder target) | ||
{ | ||
var getFileSizeTasks = from file | ||
in await target.CreateFileQuery().GetFilesAsync() | ||
select file.GetBasicPropertiesAsync().AsTask(); | ||
var fileSizes = await Task.WhenAll(getFileSizeTasks); | ||
var getFolderSizetTasks = from folder | ||
in await target.CreateFolderQuery().GetFoldersAsync() | ||
select getFolderSize(folder); | ||
var folderSizes = await Task.WhenAll(getFolderSizetTasks); | ||
return fileSizes.Sum(i => (long)i.Size) + folderSizes.Sum(); | ||
} | ||
|
||
//得到缓存目录大小 | ||
public static async Task<long> GetCacheSize() | ||
=> await getFolderSize(await getCacheFolder()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters