Skip to content
This repository has been archived by the owner on Feb 16, 2021. It is now read-only.

Commit

Permalink
部分的缓存管理器
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiichiamane committed Oct 23, 2019
1 parent 13d9bb5 commit d25049c
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
49 changes: 49 additions & 0 deletions PixivFSUWP/Data/CacheManager.cs
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());
}
}
1 change: 1 addition & 0 deletions PixivFSUWP/PixivFSUWP.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@
<Compile Include="Data\FollowingIllustsCollection.cs" />
<Compile Include="Data\IllustCommentItem.cs" />
<Compile Include="Data\IllustDetail.cs" />
<Compile Include="Data\CacheManager.cs" />
<Compile Include="Data\RankingIllustsCollection.cs" />
<Compile Include="Data\RecommendIllustsCollection.cs" />
<Compile Include="Data\OverAll.cs" />
Expand Down

0 comments on commit d25049c

Please sign in to comment.