-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileCache.cs
More file actions
236 lines (194 loc) · 7.8 KB
/
Copy pathFileCache.cs
File metadata and controls
236 lines (194 loc) · 7.8 KB
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Serialization;
using System.Text;
using System.IO;
using System.Windows.Forms;
namespace RandomFilePicker
{
public class FileCache
{
private int hits = 0;
private int missed = 0;
private int cache_Start = 0;
private int cache_stale = 0;
string status = "uninitialised";
[Serializable]
private class DirectoryCacheInfo
{
public String path;
public DateTime expiry;
public String extensionFilter = ""; //can be "" if we are not using extension filters
public Guid Guid;
public int fileCount = 0;
}
//for putting things into and out of XML as it complains about root nodes if you do a List
[Serializable]
public class FileCacheInfo
{
public Guid dirGuid;
public List<FileShortInfo> files;
}
public XmlSerializer xmlSer = new XmlSerializer(typeof(FileCacheInfo));
private List<DirectoryCacheInfo> theCache = new List<DirectoryCacheInfo>();
private List<FileCacheInfo> theFileCache = new List<FileCacheInfo>();
FileInfo cacheFile;
String cacheDirectory = "";
FileInfo lockFile;
public FileCache()
{
if (PickRandomFile.cachefolder == "")
{
cacheDirectory = new Uri(System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly().CodeBase)).LocalPath;
}
else
{
cacheDirectory = PickRandomFile.cachefolder;
}
status = "unavailable";
cacheFile = new FileInfo(cacheDirectory + "\\RandomFilePicker.cache");
lockFile = new FileInfo(cacheDirectory + "\\RandomFilePicker.lock");
//TODO try and lock
bool ignoreCache = false;
while (lockFile.Exists && status == "unavailable" && ignoreCache == false)
{
DialogResult r = MessageBox.Show("Cache Locked. Retry, Ignore (skip cache), or Abort?", "Cache Locked", MessageBoxButtons.AbortRetryIgnore);
//r == DialogResult.Retry - will loop
if (r == DialogResult.Abort) { Application.Exit(); }
if (r == DialogResult.Ignore)
{
ignoreCache = true;
PickRandomFile.useCache = false;
}
}
if (!ignoreCache)
{
//if there is no existing cache, then the cache is ready (but unpopulated). Should be written on exit.
if (cacheFile.Exists)
{
//TODO lock cache
//load cache
using (Stream stream = File.Open(cacheFile.FullName, FileMode.Open))
{
var binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
theCache = (List<DirectoryCacheInfo> )binaryFormatter.Deserialize(stream);
}
cache_Start = theCache.Count;
//expire anything that is stale.
List<DirectoryCacheInfo> stale = new List<DirectoryCacheInfo>();
foreach (DirectoryCacheInfo dic in theCache)
{
if (DateTime.Compare(DateTime.Now, dic.expiry) > 0)
{
stale.Add(dic);
}
}
cache_stale = stale.Count;
foreach (DirectoryCacheInfo dic in stale)
{
theCache.Remove(dic);
}
}
status = "ready";
}
}
/// <summary>
///
/// </summary>
/// <param name="directoryName"></param>
/// <returns>Null if not in cache</returns>
public List<FileShortInfo> getFilesFromCache(String directoryName)
{
//TODO check cache status first. should be ready
foreach (DirectoryCacheInfo dic in theCache)
{
if (dic.path == directoryName && dic.extensionFilter == PickRandomFile.fileFilter)
{
hits++;
return getCachedFiles(dic);
}
}
missed++;
return null;
}
/// <summary>
/// Loads the cache for a view of a folder when we need it.
/// </summary>
/// <param name="dir"></param>
/// <returns></returns>
private List<FileShortInfo> getCachedFiles(DirectoryCacheInfo dir)
{
if (dir.fileCount == 0) { return new List<FileShortInfo>(); }
else
{
//load cache
try
{
FileCacheInfo tempFiles = new FileCacheInfo();
string cacheFilePath = cacheDirectory + "\\" + dir.Guid.ToString();
using (StreamReader stream = new StreamReader(cacheFilePath, Encoding.UTF8))
{
tempFiles = (FileCacheInfo)xmlSer.Deserialize(stream);
}
return tempFiles.files;
}
catch (Exception e)
{
throw new Exception("Error reading a set of cache info", e);
}
}
}
public void addCacheData(String path, String extFilter, List<FileShortInfo> files)
{
//TODO check cache status first. should be ready
//TODO theoretically this shouldnt exist in the cache. we should probably remove it anyway if it does.
Random r = new Random(DateTime.Now.Millisecond + DateTime.Now.Second + DateTime.Now.Minute);
Guid g = Guid.NewGuid();
theCache.Add(new DirectoryCacheInfo()
{
fileCount = files.Count,
Guid = g,
path = path,
extensionFilter = extFilter,
expiry = DateTime.Now.AddHours(r.Next(PickRandomFile.cacheHoursMin, PickRandomFile.cacheHoursMax))
});
theFileCache.Add(new FileCacheInfo()
{
dirGuid = g,
files = files
});
}
public void saveCache()
{
//save the FULL list of cached files (including ones we didnt open).
using (Stream stream = File.Open(cacheFile.FullName, FileMode.Create))
{
var binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
binaryFormatter.Serialize(stream, theCache);
}
//save all opened file lists.
foreach (FileCacheInfo fci in theFileCache)
{
if (fci.files.Count > 0)
{
string cacheFilePath = cacheDirectory + "\\" + fci.dirGuid.ToString();
StreamWriter stream = new StreamWriter(cacheFilePath, false, Encoding.UTF8);
xmlSer.Serialize(stream, fci);
stream.Close();
}
}
//TODO - clear lock.
status = "unavailable";
}
public string getStats()
{
return
"Cache Expired: " + cache_stale.ToString() + "\n" +
"Cache Hits: " + hits.ToString() + ", Misses:" + missed.ToString() + "\n" +
"Cache Size Start: " + cache_Start.ToString() + "\n" +
"Cache Size End: " + theCache.Count.ToString();
}
}
}