source: binary-improvements2/WebServer/src/FileCache/SimpleCache.cs@ 402

Last change on this file since 402 was 402, checked in by alloc, 22 months ago
  • Major refactoring
  • Using Utf8Json for (de)serialization
  • Moving APIs to REST
  • Removing dependencies from WebServer and MapRenderer to ServerFixes
File size: 1.0 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) {
[391]13 if (fileCache.ContainsKey (_filename)) {
14 return fileCache [_filename];
15 }
[199]16
[391]17 if (!File.Exists (_filename)) {
18 return null;
[199]19 }
20
[391]21 fileCache.Add (_filename, File.ReadAllBytes (_filename));
22
[351]23 return fileCache [_filename];
[199]24 }
25 } catch (Exception e) {
[402]26 Log.Out ($"Error in SimpleCache.GetFileContent: {e}");
[199]27 }
[325]28
[199]29 return null;
30 }
[392]31
32 public override (int, int) Invalidate () {
33 (int, int) result = (0, 0);
34
35 lock (fileCache) {
36 result.Item1 = fileCache.Count;
37 foreach ((string _, byte[] data) in fileCache) {
38 result.Item2 += data.Length;
39 }
40
41 fileCache.Clear ();
42 }
43
44 return result;
45 }
[199]46 }
[325]47}
Note: See TracBrowser for help on using the repository browser.