Skip to content
This repository was archived by the owner on Jul 2, 2022. It is now read-only.

Commit ec4c9af

Browse files
committed
Re-enabled caching
1 parent 8f4bd87 commit ec4c9af

3 files changed

Lines changed: 90 additions & 14 deletions

File tree

CodeHub.Core/Services/ApplicationService.cs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using CodeHub.Core.ViewModels;
88
using CodeHub.Core.ViewModels.App;
99
using GitHubSharp;
10+
using System.Linq;
1011

1112
namespace CodeHub.Core.Services
1213
{
@@ -32,8 +33,63 @@ public void ActivateUser(GitHubAccount account, Client client)
3233
//Set the default account
3334
Accounts.SetDefault(account);
3435

36+
//Check the cache size
37+
CheckCacheSize(account.Cache);
38+
39+
Client.Cache = new GitHubCache(account);
40+
3541
// Show the menu & show a page on the slideout
3642
_viewDispatcher.ShowViewModel(new MvxViewModelRequest {ViewModelType = typeof (MenuViewModel)});
3743
}
44+
45+
private static void CheckCacheSize(CodeFramework.Core.Data.AccountCache cache)
46+
{
47+
var totalCacheSize = cache.Sum(x => System.IO.File.Exists(x.Path) ? new System.IO.FileInfo(x.Path).Length : 0);
48+
var totalCacheSizeMB = ((float)totalCacheSize / 1024f / 1024f);
49+
50+
if (totalCacheSizeMB > 1)
51+
{
52+
System.Console.WriteLine("Flushing cache due to size...");
53+
cache.DeleteAll();
54+
}
55+
}
56+
57+
private class GitHubCache : ICache
58+
{
59+
private readonly CodeFramework.Core.Data.AccountCache _account;
60+
public GitHubCache(GitHubAccount account)
61+
{
62+
_account = account.Cache;
63+
}
64+
65+
public string GetETag(string url)
66+
{
67+
var data = _account.GetEntry(url);
68+
if (data == null)
69+
return null;
70+
return data.CacheTag;
71+
}
72+
73+
public T Get<T>(string url) where T : new()
74+
{
75+
var data = _account.Get<T>(url);
76+
if (data == null)
77+
return default(T);
78+
79+
System.Console.WriteLine("[GET] cache: {0}", url);
80+
return data;
81+
}
82+
83+
public void Set(string url, object data, string etag)
84+
{
85+
System.Console.WriteLine("[SET] cache: {0}", url);
86+
_account.Set(url, data, etag);
87+
}
88+
89+
public bool Exists(string url)
90+
{
91+
return _account.GetEntry(url) != null;
92+
}
93+
}
3894
}
3995
}

CodeHub.Core/ViewModels/App/SettingsViewModel.cs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using CodeFramework.Core.ViewModels;
22
using System.Windows.Input;
3-
using CodeFramework.Core.ViewModels.App;
43
using Cirrious.MvvmCross.ViewModels;
4+
using System.Linq;
55

66
namespace CodeHub.Core.ViewModels.App
77
{
@@ -16,5 +16,29 @@ public ICommand GoToDefaultStartupViewCommand
1616
{
1717
get { return new MvxCommand(() => ShowViewModel<DefaultStartupViewModel>()); }
1818
}
19+
20+
public ICommand DeleteAllCacheCommand
21+
{
22+
get { return new MvxCommand(DeleteCache); }
23+
}
24+
25+
private void DeleteCache()
26+
{
27+
if (this.GetApplication().Account.Cache != null)
28+
this.GetApplication().Account.Cache.DeleteAll();
29+
}
30+
31+
public float CacheSize
32+
{
33+
get
34+
{
35+
if (this.GetApplication().Account.Cache == null)
36+
return 0f;
37+
38+
var totalCacheSize = this.GetApplication().Account.Cache.Sum(x => System.IO.File.Exists(x.Path) ? new System.IO.FileInfo(x.Path).Length : 0);
39+
var totalCacheSizeMB = ((float)totalCacheSize / 1024f / 1024f);
40+
return totalCacheSizeMB;
41+
}
42+
}
1943
}
2044
}

CodeHub.iOS/Views/App/SettingsView.cs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,15 @@ public override void ViewWillAppear(bool animated)
6161
// })
6262
// });
6363

64-
// if (Application.ClientCache != null)
65-
// {
66-
// var totalCacheSize = Application.ClientCache.Sum(x => System.IO.File.Exists(x.Path) ? new System.IO.FileInfo(x.Path).Length : 0);
67-
// var totalCacheSizeMB = ((float)totalCacheSize / 1024f / 1024f).ToString("0.##");
68-
// var cacheSection = new Section(string.Empty, string.Format("{0} MB of cache".t(), totalCacheSizeMB));
69-
// cacheSection.Add(new StyledStringElement("Delete Cache".t(), () =>
70-
// {
71-
// Application.ClientCache.DeleteAll();
72-
// cacheSection.Footer = string.Format("{0} MB of cache".t(), 0);
73-
// ReloadData();
74-
// }));
75-
// root.Add(cacheSection);
76-
// }
64+
var totalCacheSizeMB = vm.CacheSize.ToString("0.##");
65+
var cacheSection = new Section(string.Empty, string.Format("{0} MB of cache used".t(), totalCacheSizeMB));
66+
cacheSection.Add(new StyledStringElement("Delete Cache".t(), () =>
67+
{
68+
vm.DeleteAllCacheCommand.Execute(null);
69+
cacheSection.Footer = string.Format("{0} MB of cache used".t(), 0);
70+
ReloadData();
71+
}));
72+
root.Add(cacheSection);
7773

7874
//Assign the root
7975
Root = root;

0 commit comments

Comments
 (0)