This repository was archived by the owner on Jul 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHandlebarsCache.cs
More file actions
62 lines (55 loc) · 1.84 KB
/
HandlebarsCache.cs
File metadata and controls
62 lines (55 loc) · 1.84 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
using System;
using System.Collections.Concurrent;
using System.IO;
using HandlebarsDotNet;
namespace Red.HandlebarsRenderer
{
internal class HandlebarsCache
{
class HandlebarsCacheFile
{
public HandlebarsCacheFile(string filePath)
{
_filePath = filePath;
Update();
}
private void Update()
{
using (var reader = File.OpenText(_filePath))
{
_renderer = Handlebars.Compile(reader);
_updated = DateTime.UtcNow;
}
}
public Action<TextWriter, object> Renderer
{
get
{
if (File.GetLastWriteTimeUtc(_filePath) > _updated)
Update();
return _renderer;
}
}
private DateTime _updated;
private readonly string _filePath;
private Action<TextWriter, object> _renderer;
}
public static HandlebarsCache Instance => _instance ?? (_instance = new HandlebarsCache());
private static HandlebarsCache _instance;
public Action<TextWriter, object> GetRenderer(string filePath, bool cacheRenderers)
{
if (cacheRenderers && _cachedFiles.TryGetValue(filePath, out var renderer))
{
return renderer.Renderer;
}
renderer = new HandlebarsCacheFile(filePath);
if (cacheRenderers)
{
_cachedFiles.TryAdd(filePath, renderer);
}
return renderer.Renderer;
}
private readonly ConcurrentDictionary<string, HandlebarsCacheFile> _cachedFiles =
new ConcurrentDictionary<string, HandlebarsCacheFile>();
}
}