source: TFP-WebServer/WebServer/src/FileCache/SimpleCache.cs@ 498

Last change on this file since 498 was 487, checked in by alloc, 5 months ago

1.1.0.1 Release for V 1.0

File size: 1.1 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.IO;
4
5namespace Webserver.FileCache {
6 // Caching all files, useful for completely static folders only
7 public class SimpleCache : AbstractCache {
8 private readonly Dictionary<string, byte[]> fileCache = new Dictionary<string, byte[]> ();
9
10 public override byte[] GetFileContent (string _filename) {
11 try {
12 lock (fileCache) {
13 if (fileCache.TryGetValue(_filename, out byte[] content)) {
14 return content;
15 }
16
17 if (!File.Exists (_filename)) {
18 return null;
19 }
20
21 byte[] newContent = File.ReadAllBytes (_filename);
22 fileCache.Add (_filename, newContent);
23
24 return newContent;
25 }
26 } catch (Exception e) {
27 Log.Out ($"Error in SimpleCache.GetFileContent: {e}");
28 }
29
30 return null;
31 }
32
33 public override (int, int) Invalidate () {
34 (int, int) result = (0, 0);
35
36 lock (fileCache) {
37 result.Item1 = fileCache.Count;
38 foreach ((string _, byte[] data) in fileCache) {
39 result.Item2 += data.Length;
40 }
41
42 fileCache.Clear ();
43 }
44
45 return result;
46 }
47 }
48}
Note: See TracBrowser for help on using the repository browser.