-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathtemplate_cache.go
More file actions
180 lines (154 loc) · 6.07 KB
/
Copy pathtemplate_cache.go
File metadata and controls
180 lines (154 loc) · 6.07 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
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
package plush
import (
"github.com/gobuffalo/plush/v5/ast"
"github.com/gobuffalo/plush/v5/helpers/hctx"
)
type TemplateCache interface {
// Get retrieves a cached template by key.
Get(key string) (*Template, bool)
// Set stores a template in the cache.
Set(key string, t *Template)
// Delete removes a template from the cache.
Delete(key ...string)
Clear()
}
type contextWriteReporter interface {
ContextWrites() bool
}
func ClearTemplateCache() {
if templateCacheBackend != nil {
templateCacheBackend.Clear()
}
}
func EnableTrustedPartialBytecodeCache(ctx hctx.Context) {
SetTrustedPartialBytecodeCache(ctx, true)
}
func DisableTrustedPartialBytecodeCache(ctx hctx.Context) {
SetTrustedPartialBytecodeCache(ctx, false)
}
func SetTrustedPartialBytecodeCache(ctx hctx.Context, enabled bool) {
if ctx == nil {
return
}
ctx.Set(trustedPartialBytecodeCacheKey, enabled)
}
func TrustedPartialBytecodeCacheEnabled(ctx hctx.Context) bool {
if ctx == nil {
return false
}
enabled, _ := ctx.Value(trustedPartialBytecodeCacheKey).(bool)
return enabled
}
// SetTrustedTopLevelBytecodeCacheFilename gives the next top-level VM render a
// stable physical cache identity. The filename is consumed by that render so
// nested renders cannot inherit it, and it is only honored while trusted
// partial bytecode caching is enabled on the same context.
func SetTrustedTopLevelBytecodeCacheFilename(ctx hctx.Context, filename string) {
if ctx == nil {
return
}
ctx.Set(trustedTopLevelBytecodeCacheFilenameKey, cleanFilePath(filename))
}
// TrustedTopLevelBytecodeCacheFilename returns the physical top-level cache
// identity selected by a trusted file-backed renderer.
func TrustedTopLevelBytecodeCacheFilename(ctx hctx.Context) string {
if ctx == nil || !TrustedPartialBytecodeCacheEnabled(ctx) {
return ""
}
filename, _ := ctx.Value(trustedTopLevelBytecodeCacheFilenameKey).(string)
return filename
}
func CachedVMBytecode(filename string, ctx hctx.Context) (interface{}, bool) {
if filename == "" || !cacheEnabled || templateCacheBackend == nil || !isVMBytecodeCacheableFile(filename) || isHole(ctx) {
return nil, false
}
return CachedVMBytecodeForFilename(filename)
}
func CachedVMBytecodeForFilename(filename string) (interface{}, bool) {
return CachedVMBytecodeForFilenameWithSource(filename, "")
}
func CachedVMBytecodeForFilenameWithSource(filename string, source string) (interface{}, bool) {
if filename == "" || !cacheEnabled || templateCacheBackend == nil || !isVMBytecodeCacheableFile(filename) {
return nil, false
}
t, ok := templateCacheBackend.Get(GenerateASTKey(filename))
if !ok || t == nil || t.VMBytecode == nil || !templateSourceMatches(t, templateSourceCacheHash(source)) {
return nil, false
}
return t.VMBytecode, true
}
func CachedVMBytecodeForCleanFilename(filename string) (interface{}, bool) {
return CachedVMBytecodeForCleanFilenameWithSource(filename, "")
}
func CachedVMBytecodeForCleanFilenameWithSource(filename string, source string) (interface{}, bool) {
if filename == "" || !cacheEnabled || templateCacheBackend == nil || !isVMBytecodeCacheableFile(filename) {
return nil, false
}
t, ok := templateCacheBackend.Get(GenerateASTKeyFromCleanFilename(filename))
if !ok || t == nil || t.VMBytecode == nil || !templateSourceMatches(t, templateSourceCacheHash(source)) {
return nil, false
}
return t.VMBytecode, true
}
func CachedASTProgram(filename string, ctx hctx.Context) (*ast.Program, bool) {
return CachedASTProgramWithSource(filename, ctx, "")
}
func CachedASTProgramWithSource(filename string, ctx hctx.Context, source string) (*ast.Program, bool) {
if filename == "" || !cacheEnabled || templateCacheBackend == nil || !isFilePlush(filename) || isHole(ctx) {
return nil, false
}
t, ok := templateCacheBackend.Get(GenerateASTKey(filename))
if !ok || t == nil || t.Program == nil || !templateSourceMatches(t, templateSourceCacheHash(source)) {
return nil, false
}
return t.Program, true
}
func CacheVMBytecode(filename string, ctx hctx.Context, program *ast.Program, bytecode interface{}) {
cacheVMBytecode(filename, ctx, program, bytecode)
}
func cacheVMBytecode(filename string, ctx hctx.Context, program *ast.Program, bytecode interface{}) {
if filename == "" || !cacheEnabled || templateCacheBackend == nil || !isVMBytecodeCacheableFile(filename) || isHole(ctx) || bytecode == nil {
return
}
CacheVMBytecodeForFilename(filename, program, bytecode)
}
func CacheVMBytecodeForFilename(filename string, program *ast.Program, bytecode interface{}) {
CacheVMBytecodeForFilenameWithSource(filename, program, bytecode, "")
}
func CacheVMBytecodeForFilenameWithSource(filename string, program *ast.Program, bytecode interface{}, source string) {
if filename == "" || !cacheEnabled || templateCacheBackend == nil || !isVMBytecodeCacheableFile(filename) || bytecode == nil {
return
}
key := GenerateASTKey(filename)
cacheVMBytecodeWithKey(key, program, bytecode, templateSourceCacheHash(source))
}
func CacheVMBytecodeForCleanFilename(filename string, program *ast.Program, bytecode interface{}) {
CacheVMBytecodeForCleanFilenameWithSource(filename, program, bytecode, "")
}
func CacheVMBytecodeForCleanFilenameWithSource(filename string, program *ast.Program, bytecode interface{}, source string) {
if filename == "" || !cacheEnabled || templateCacheBackend == nil || !isVMBytecodeCacheableFile(filename) || bytecode == nil {
return
}
cacheVMBytecodeWithKey(GenerateASTKeyFromCleanFilename(filename), program, bytecode, templateSourceCacheHash(source))
}
func cacheVMBytecodeWithKey(key string, program *ast.Program, bytecode interface{}, sourceHash string) {
t, ok := templateCacheBackend.Get(key)
if !ok || t == nil || !templateSourceMatches(t, sourceHash) {
t = &Template{}
} else {
cloned := *t
t = &cloned
}
if program != nil {
t.Program = program
t.HasContextWrites = programHasContextWrites(program)
}
if reporter, ok := bytecode.(contextWriteReporter); ok {
t.HasContextWrites = t.HasContextWrites || reporter.ContextWrites()
}
t.Input = ""
t.VMBytecode = bytecode
t.SourceHash = sourceHash
t.IsCache = false
templateCacheBackend.Set(key, t)
}