FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
NetCoreServer/source/NetCoreServer/FileCache.cs at master · bubdm/NetCoreServer · GitHub
bubdm
NetCoreServer
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
NetCoreServer
/
source
/
NetCoreServer
/
FileCache.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
490 lines (409 loc) · 16.7 KB
Breadcrumbs
NetCoreServer
/
source
/
NetCoreServer
/
FileCache.cs
Copy path
File metadata and controls
490 lines (409 loc) · 16.7 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
using
System
;
using
System
.
Collections
.
Generic
;
using
System
.
IO
;
using
System
.
Text
;
using
System
.
Threading
;
using
System
.
Web
;
namespace
NetCoreServer
{
/// <summary>
/// File cache is used to cache files in memory with optional timeouts.
/// FileSystemWatcher is used to monitor file system changes in cached
/// directories.
/// </summary>
/// <remarks>Thread-safe.</remarks>
public
class
FileCache
:
IDisposable
{
public
delegate
bool
InsertHandler
(
FileCache
cache
,
string
key
,
byte
[
]
value
,
TimeSpan
timeout
)
;
#region Cache items access
/// <summary>
/// Is the file cache empty?
/// </summary>
public
bool
Empty
=>
_entriesByKey
.
Count
==
0
;
/// <summary>
/// Get the file cache size
/// </summary>
public
int
Size
=>
_entriesByKey
.
Count
;
/// <summary>
/// Add a new cache value with the given timeout into the file cache
/// </summary>
/// <param name="key">Key to add</param>
/// <param name="value">Value to add</param>
/// <param name="timeout">Cache timeout (default is 0 - no timeout)</param>
/// <returns>'true' if the cache value was added, 'false' if the given key was not added</returns>
public
bool
Add
(
string
key
,
byte
[
]
value
,
TimeSpan
timeout
=
new
TimeSpan
(
)
)
{
using
(
new
WriteLock
(
_lockEx
)
)
{
// Try to find and remove the previous key
_entriesByKey
.
Remove
(
key
)
;
// Update the cache entry
_entriesByKey
.
Add
(
key
,
new
MemCacheEntry
(
value
,
timeout
)
)
;
return
true
;
}
}
/// <summary>
/// Try to find the cache value by the given key
/// </summary>
/// <param name="key">Key to find</param>
/// <returns>'true' and cache value if the cache value was found, 'false' if the given key was not found</returns>
public
(
bool
,
byte
[
]
)
Find
(
string
key
)
{
using
(
new
ReadLock
(
_lockEx
)
)
{
// Try to find the given key
if
(
!
_entriesByKey
.
TryGetValue
(
key
,
out
var
cacheValue
)
)
return
(
false
,
new
byte
[
0
]
)
;
return
(
true
,
cacheValue
.
Value
)
;
}
}
/// <summary>
/// Remove the cache value with the given key from the file cache
/// </summary>
/// <param name="key">Key to remove</param>
/// <returns>'true' if the cache value was removed, 'false' if the given key was not found</returns>
public
bool
Remove
(
string
key
)
{
using
(
new
WriteLock
(
_lockEx
)
)
{
return
_entriesByKey
.
Remove
(
key
)
;
}
}
#endregion
#region Cache management methods
/// <summary>
/// Insert a new cache path with the given timeout into the file cache
/// </summary>
/// <param name="path">Path to insert</param>
/// <param name="prefix">Cache prefix (default is "/")</param>
/// <param name="filter">Cache filter (default is "*.*")</param>
/// <param name="timeout">Cache timeout (default is 0 - no timeout)</param>
/// <param name="handler">Cache insert handler (default is 'return cache.Add(key, value, timeout)')</param>
/// <returns>'true' if the cache path was setup, 'false' if failed to setup the cache path</returns>
public
bool
InsertPath
(
string
path
,
string
prefix
=
"/"
,
string
filter
=
"*.*"
,
TimeSpan
timeout
=
new
TimeSpan
(
)
,
InsertHandler
handler
=
null
)
{
handler
??=
(
FileCache
cache
,
string
key
,
byte
[
]
value
,
TimeSpan
timespan
)
=>
cache
.
Add
(
key
,
value
,
timespan
)
;
// Try to find and remove the previous path
RemovePathInternal
(
path
)
;
using
(
new
WriteLock
(
_lockEx
)
)
{
// Add the given path to the cache
_pathsByKey
.
Add
(
path
,
new
FileCacheEntry
(
this
,
prefix
,
path
,
filter
,
handler
,
timeout
)
)
;
// Create entries by path map
_entriesByPath
[
path
]
=
new
HashSet
<
string
>
(
)
;
}
// Insert the cache path
if
(
!
InsertPathInternal
(
path
,
path
,
prefix
,
timeout
,
handler
)
)
return
false
;
return
true
;
}
/// <summary>
/// Try to find the cache path
/// </summary>
/// <param name="path">Path to find</param>
/// <returns>'true' if the cache path was found, 'false' if the given path was not found</returns>
public
bool
FindPath
(
string
path
)
{
using
(
new
ReadLock
(
_lockEx
)
)
{
// Try to find the given key
return
_pathsByKey
.
ContainsKey
(
path
)
;
}
}
/// <summary>
/// Remove the cache path from the file cache
/// </summary>
/// <param name="path">Path to remove</param>
/// <returns>'true' if the cache path was removed, 'false' if the given path was not found</returns>
public
bool
RemovePath
(
string
path
)
{
return
RemovePathInternal
(
path
)
;
}
/// <summary>
/// Clear the memory cache
/// </summary>
public
void
Clear
(
)
{
using
(
new
WriteLock
(
_lockEx
)
)
{
// Stop all file system watchers
foreach
(
var
fileCacheEntry
in
_pathsByKey
)
fileCacheEntry
.
Value
.
StopWatcher
(
)
;
// Clear all cache entries
_entriesByKey
.
Clear
(
)
;
_entriesByPath
.
Clear
(
)
;
_pathsByKey
.
Clear
(
)
;
}
}
#endregion
#region Cache implementation
private
readonly
ReaderWriterLockSlim
_lockEx
=
new
ReaderWriterLockSlim
(
)
;
private
Dictionary
<
string
,
MemCacheEntry
>
_entriesByKey
=
new
Dictionary
<
string
,
MemCacheEntry
>
(
)
;
private
Dictionary
<
string
,
HashSet
<
string
>
>
_entriesByPath
=
new
Dictionary
<
string
,
HashSet
<
string
>
>
(
)
;
private
Dictionary
<
string
,
FileCacheEntry
>
_pathsByKey
=
new
Dictionary
<
string
,
FileCacheEntry
>
(
)
;
private
class
MemCacheEntry
{
private
readonly
byte
[
]
_value
;
private
readonly
TimeSpan
_timespan
;
public
byte
[
]
Value
=>
_value
;
public
TimeSpan
Timespan
=>
_timespan
;
public
MemCacheEntry
(
byte
[
]
value
,
TimeSpan
timespan
=
new
TimeSpan
(
)
)
{
_value
=
value
;
_timespan
=
timespan
;
}
public
MemCacheEntry
(
string
value
,
TimeSpan
timespan
=
new
TimeSpan
(
)
)
{
_value
=
Encoding
.
UTF8
.
GetBytes
(
value
)
;
_timespan
=
timespan
;
}
}
;
private
class
FileCacheEntry
{
private
readonly
string
_prefix
;
private
readonly
string
_path
;
private
readonly
InsertHandler
_handler
;
private
readonly
TimeSpan
_timespan
;
private
readonly
FileSystemWatcher
_watcher
;
public
FileCacheEntry
(
FileCache
cache
,
string
prefix
,
string
path
,
string
filter
,
InsertHandler
handler
,
TimeSpan
timespan
)
{
_prefix
=
prefix
;
_path
=
path
;
_handler
=
handler
;
_timespan
=
timespan
;
_watcher
=
new
FileSystemWatcher
(
)
;
// Start the filesystem watcher
StartWatcher
(
cache
,
path
,
filter
)
;
}
private
void
StartWatcher
(
FileCache
cache
,
string
path
,
string
filter
)
{
FileCacheEntry
entry
=
this
;
// Initialize a new filesystem watcher
_watcher
.
Created
+=
(
sender
,
e
)
=>
OnCreated
(
sender
,
e
,
cache
,
entry
)
;
_watcher
.
Changed
+=
(
sender
,
e
)
=>
OnChanged
(
sender
,
e
,
cache
,
entry
)
;
_watcher
.
Deleted
+=
(
sender
,
e
)
=>
OnDeleted
(
sender
,
e
,
cache
,
entry
)
;
_watcher
.
Renamed
+=
(
sender
,
e
)
=>
OnRenamed
(
sender
,
e
,
cache
,
entry
)
;
_watcher
.
Path
=
path
;
_watcher
.
IncludeSubdirectories
=
true
;
_watcher
.
Filter
=
filter
;
_watcher
.
NotifyFilter
=
NotifyFilters
.
FileName
|
NotifyFilters
.
LastWrite
;
_watcher
.
EnableRaisingEvents
=
true
;
}
public
void
StopWatcher
(
)
{
_watcher
.
Dispose
(
)
;
}
private
static
bool
IsDirectory
(
string
path
)
{
try
{
// Skip directory updates
if
(
File
.
GetAttributes
(
path
)
.
HasFlag
(
FileAttributes
.
Directory
)
)
return
true
;
}
catch
(
Exception
)
{
}
return
false
;
}
private
static
void
OnCreated
(
object
sender
,
FileSystemEventArgs
e
,
FileCache
cache
,
FileCacheEntry
entry
)
{
var
key
=
e
.
FullPath
.
Replace
(
entry
.
_path
,
entry
.
_prefix
)
;
var
file
=
e
.
FullPath
;
// Skip missing files
if
(
!
File
.
Exists
(
file
)
)
return
;
// Skip directory updates
if
(
IsDirectory
(
file
)
)
return
;
cache
.
InsertFileInternal
(
entry
.
_path
,
file
,
key
,
entry
.
_timespan
,
entry
.
_handler
)
;
}
private
static
void
OnChanged
(
object
sender
,
FileSystemEventArgs
e
,
FileCache
cache
,
FileCacheEntry
entry
)
{
if
(
e
.
ChangeType
!=
WatcherChangeTypes
.
Changed
)
return
;
var
key
=
e
.
FullPath
.
Replace
(
entry
.
_path
,
entry
.
_prefix
)
;
var
file
=
e
.
FullPath
;
// Skip missing files
if
(
!
File
.
Exists
(
file
)
)
return
;
// Skip directory updates
if
(
IsDirectory
(
file
)
)
return
;
cache
.
InsertFileInternal
(
entry
.
_path
,
file
,
key
,
entry
.
_timespan
,
entry
.
_handler
)
;
}
private
static
void
OnDeleted
(
object
sender
,
FileSystemEventArgs
e
,
FileCache
cache
,
FileCacheEntry
entry
)
{
var
key
=
e
.
FullPath
.
Replace
(
entry
.
_path
,
entry
.
_prefix
)
;
var
file
=
e
.
FullPath
;
cache
.
RemoveFileInternal
(
entry
.
_path
,
key
)
;
}
private
static
void
OnRenamed
(
object
sender
,
RenamedEventArgs
e
,
FileCache
cache
,
FileCacheEntry
entry
)
{
var
oldKey
=
e
.
OldFullPath
.
Replace
(
entry
.
_path
,
entry
.
_prefix
)
;
var
oldFile
=
e
.
OldFullPath
;
var
newKey
=
e
.
FullPath
.
Replace
(
entry
.
_path
,
entry
.
_prefix
)
;
var
newFile
=
e
.
FullPath
;
// Skip missing files
if
(
!
File
.
Exists
(
newFile
)
)
return
;
// Skip directory updates
if
(
IsDirectory
(
newFile
)
)
return
;
cache
.
RemoveFileInternal
(
entry
.
_path
,
oldKey
)
;
cache
.
InsertFileInternal
(
entry
.
_path
,
newFile
,
newKey
,
entry
.
_timespan
,
entry
.
_handler
)
;
}
}
;
private
bool
InsertFileInternal
(
string
path
,
string
file
,
string
key
,
TimeSpan
timeout
,
InsertHandler
handler
)
{
try
{
key
=
key
.
Replace
(
'
\\
'
,
'/'
)
;
file
=
file
.
Replace
(
'
\\
'
,
'/'
)
;
// Load the cache file content
var
content
=
File
.
ReadAllBytes
(
file
)
;
if
(
!
handler
(
this
,
key
,
content
,
timeout
)
)
return
false
;
using
(
new
WriteLock
(
_lockEx
)
)
{
// Update entries by path map
_entriesByPath
[
path
]
.
Add
(
key
)
;
}
return
true
;
}
catch
(
Exception
)
{
return
false
;
}
}
private
bool
RemoveFileInternal
(
string
path
,
string
key
)
{
try
{
key
=
key
.
Replace
(
'
\\
'
,
'/'
)
;
using
(
new
WriteLock
(
_lockEx
)
)
{
// Update entries by path map
_entriesByPath
[
path
]
.
Remove
(
key
)
;
}
return
Remove
(
key
)
;
}
catch
(
Exception
)
{
return
false
;
}
}
private
bool
InsertPathInternal
(
string
root
,
string
path
,
string
prefix
,
TimeSpan
timeout
,
InsertHandler
handler
)
{
try
{
string
keyPrefix
=
(
string
.
IsNullOrEmpty
(
prefix
)
||
(
prefix
==
"/"
)
)
?
"/"
:
(
prefix
+
"/"
)
;
// Iterate through all directory entries
foreach
(
var
item
in
Directory
.
GetDirectories
(
path
)
)
{
string
key
=
keyPrefix
+
HttpUtility
.
UrlDecode
(
Path
.
GetFileName
(
item
)
)
;
// Recursively insert sub-directory
if
(
!
InsertPathInternal
(
root
,
item
,
key
,
timeout
,
handler
)
)
return
false
;
}
foreach
(
var
item
in
Directory
.
GetFiles
(
path
)
)
{
string
key
=
keyPrefix
+
HttpUtility
.
UrlDecode
(
Path
.
GetFileName
(
item
)
)
;
// Insert file into the cache
if
(
!
InsertFileInternal
(
root
,
item
,
key
,
timeout
,
handler
)
)
return
false
;
}
return
true
;
}
catch
(
Exception
)
{
return
false
;
}
}
private
bool
RemovePathInternal
(
string
path
)
{
using
(
new
WriteLock
(
_lockEx
)
)
{
// Try to find the given path
if
(
!
_pathsByKey
.
TryGetValue
(
path
,
out
var
cacheValue
)
)
return
false
;
// Stop the file system watcher
cacheValue
.
StopWatcher
(
)
;
// Remove path entries
foreach
(
var
entryKey
in
_entriesByPath
[
path
]
)
_entriesByKey
.
Remove
(
entryKey
)
;
_entriesByPath
.
Remove
(
path
)
;
// Remove cache path
_pathsByKey
.
Remove
(
path
)
;
return
true
;
}
}
#endregion
#region IDisposable implementation
// Disposed flag.
private
bool
_disposed
;
// Implement IDisposable.
public
void
Dispose
(
)
{
Dispose
(
true
)
;
GC
.
SuppressFinalize
(
this
)
;
}
protected
virtual
void
Dispose
(
bool
disposingManagedResources
)
{
// The idea here is that Dispose(Boolean) knows whether it is
// being called to do explicit cleanup (the Boolean is true)
// versus being called due to a garbage collection (the Boolean
// is false). This distinction is useful because, when being
// disposed explicitly, the Dispose(Boolean) method can safely
// execute code using reference type fields that refer to other
// objects knowing for sure that these other objects have not been
// finalized or disposed of yet. When the Boolean is false,
// the Dispose(Boolean) method should not execute code that
// refer to reference type fields because those objects may
// have already been finalized."
if
(
!
_disposed
)
{
if
(
disposingManagedResources
)
{
// Dispose managed resources here...
Clear
(
)
;
}
// Dispose unmanaged resources here...
// Set large fields to null here...
// Mark as disposed.
_disposed
=
true
;
}
}
// Use C# destructor syntax for finalization code.
~
FileCache
(
)
{
// Simply call Dispose(false).
Dispose
(
false
)
;
}
#endregion
}
/// <summary>
/// Disposable lock class performs exit action on dispose operation.
/// </summary>
public
class
DisposableLock
:
IDisposable
{
private
readonly
Action
_exitLock
;
public
DisposableLock
(
Action
exitLock
)
{
_exitLock
=
exitLock
;
}
public
void
Dispose
(
)
{
_exitLock
(
)
;
}
}
/// <summary>
/// Read lock class enters read lock on construction and performs exit read lock on dispose.
/// </summary>
public
class
ReadLock
:
DisposableLock
{
public
ReadLock
(
ReaderWriterLockSlim
locker
)
:
base
(
locker
.
ExitReadLock
)
{
locker
.
EnterReadLock
(
)
;
}
}
/// <summary>
/// Write lock class enters write lock on construction and performs exit write lock on dispose.
/// </summary>
public
class
WriteLock
:
DisposableLock
{
public
WriteLock
(
ReaderWriterLockSlim
locker
)
:
base
(
locker
.
ExitWriteLock
)
{
locker
.
EnterWriteLock
(
)
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL