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

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

1.1.0.1 Release for V 1.0

File size: 1.1 KB
RevLine 
[199]1using System;
2using System.Collections.Generic;
3using System.IO;
4
[402]5namespace Webserver.FileCache {
[199]6 // Caching all files, useful for completely static folders only
[325]7 public class SimpleCache : AbstractCache {
8 private readonly Dictionary<string, byte[]> fileCache = new Dictionary<string, byte[]> ();
[199]9
[351]10 public override byte[] GetFileContent (string _filename) {
[199]11 try {
12 lock (fileCache) {
[487]13 if (fileCache.TryGetValue(_filename, out byte[] content)) {
14 return content;
[391]15 }
[199]16
[391]17 if (!File.Exists (_filename)) {
18 return null;
[199]19 }
20
[487]21 byte[] newContent = File.ReadAllBytes (_filename);
22 fileCache.Add (_filename, newContent);
[391]23
[487]24 return newContent;
[199]25 }
26 } catch (Exception e) {
[402]27 Log.Out ($"Error in SimpleCache.GetFileContent: {e}");
[199]28 }
[325]29
[199]30 return null;
31 }
[392]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 }
[199]47 }
[325]48}
Note: See TracBrowser for help on using the repository browser.