-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCacheItem.cs
222 lines (197 loc) · 4.98 KB
/
CacheItem.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
namespace ZCache
{
using System;
using System.Windows;
using System.Collections.Generic;
using System.Data.Services.Client;
using System.ComponentModel;
/// <summary>
/// A cached item. Contains the entity and query to pull data.
/// </summary>
/// <typeparam name="T">ADO.NET Entity object</typeparam>
public class CacheItem<T> : ZCache.Interfaces.IZCacheable
where T : class, INotifyPropertyChanged
{
/// <summary>
/// Listing of cached item.
/// </summary>
private List<T> EntityCache { get; set; }
/// <summary>
/// Cache name.
/// </summary>
private string CacheName { get; set; }
/// <summary>
/// Data service query.
/// </summary>
private DataServiceQuery<T> CacheQuery { get; set; }
/// <summary>
/// Total entities on database.
/// </summary>
private int TotalEntityCount { get; set; }
/// <summary>
/// Counts databases entities.
/// </summary>
private BackgroundWorker bwCounter { get; set; }
/// <summary>
/// Default constructor.
/// </summary>
/// <param name="resourceName">Resource name.</param>
/// <param name="cacheQuery">DataServiceQuery</param>
public CacheItem(string resourceName, DataServiceQuery<T> cacheQuery)
{
this.CacheName = resourceName;
this.CacheQuery = cacheQuery;
this.EntityCache = null;
this.bwCounter = new BackgroundWorker();
this.bwCounter.WorkerReportsProgress = true;
this.bwCounter.WorkerSupportsCancellation = true;
this.bwCounter.DoWork += new DoWorkEventHandler(bwCounter_DoWork);
}
void bwCounter_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker me = sender as BackgroundWorker;
while ( true )
{
if ( me.CancellationPending ) { e.Cancel = true; return; }
else
{
// Does cache support auto refreshing?
if ( this.AutoRefresh &&
DateTime.Now.Subtract(LastCache).Minutes >= CacheTimer )
{
DataServiceQuery<T> updateQuery =
this.CacheQuery.AddQueryOption(
"$skip",
this.EntityCache.Count);
// Update the total server count.
updateQuery.BeginExecute(result =>
{
if ( result.IsCompleted )
{
try
{
foreach ( T item in updateQuery.EndExecute(result) )
{
this.EntityCache.Add(item);
}
this.LastCache = DateTime.Now;
}
catch ( Exception ex ) { new Windows.ErrorWindow(ex).Show(); }
}
}, null);
}
// Take a nap.
System.Threading.Thread.Sleep(CacheTimer * 60000);
}
}
}
/// <summary>
/// Retreive stored cache.
/// </summary>
/// <returns></returns>
public List<T> GetCache()
{
if ( this.EntityCache != null && this.EntityCache.Count > 0 )
return this.EntityCache;
else
return null;
}
/// <summary>
/// Returns true if cache has been loaded.
/// </summary>
public bool IsLoaded
{
get
{
return this.IsCompleted ? (this.EntityCache.Count > 0) : false;
}
}
/// <summary>
/// Returns true if cache has completed all events.
/// </summary>
public bool IsCompleted { get { return (this.EntityCache != null); } }
/// <summary>
/// Returns true.
/// </summary>
public bool IsCacheable { get { return true; } }
/// <summary>
/// Returns true if cache should use isolated storage.
/// </summary>
public bool UseIsolatedStorage
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
/// <summary>
/// Datetime of last cache update.
/// </summary>
public DateTime LastCache { get; set; }
/// <summary>
/// Refresh interval of cache (in minutes).
/// </summary>
public int CacheTimer { get; set; }
/// <summary>
/// Does cache auto-refresh in background.
/// </summary>
public bool AutoRefresh { get; set; }
/// <summary>
/// Local name for cache resource.
/// </summary>
public string ResourceName { get { return this.CacheName; } }
/// <summary>
/// Initial cache load.
/// </summary>
public void LoadCache()
{
try
{
if ( this.CacheQuery != null )
{
// Bad. Ass Lamb. Da.
// BALD.
this.CacheQuery.BeginExecute(result =>
{
if ( result.IsCompleted )
{
try
{
this.EntityCache = new List<T>();
foreach ( T item in this.CacheQuery.EndExecute(result) )
{
this.EntityCache.Add(item);
}
bwCounter.RunWorkerAsync();
this.LastCache = DateTime.Now;
CheckCompletion();
}
catch ( Exception ex ) { new Windows.ErrorWindow(ex).Show(); }
}
}, null);
}
}
catch ( Exception ex ) { new Windows.ErrorWindow(ex).Show(); }
}
/// <summary>
/// Checks for completion of cache save.
/// </summary>
private void CheckCompletion()
{
if ( this.IsCompleted )
{
ResourceLoaded(
this,
new ZCache.Interfaces.ResourceLoadedEventArgs(this.IsLoaded));
}
}
/// <summary>
/// Resource loaded event.
/// </summary>
public event Interfaces.ResourceLoad ResourceLoaded;
}
}