Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/pkg/templates/cache.go
2070 views
1
package templates
2
3
import (
4
"github.com/projectdiscovery/utils/conversion"
5
mapsutil "github.com/projectdiscovery/utils/maps"
6
)
7
8
// Templates is a cache for caching and storing templates for reuse.
9
type Cache struct {
10
items *mapsutil.SyncLockMap[string, parsedTemplate]
11
}
12
13
// New returns a new templates cache
14
func NewCache() *Cache {
15
return &Cache{items: mapsutil.NewSyncLockMap[string, parsedTemplate]()}
16
}
17
18
type parsedTemplate struct {
19
template *Template
20
raw string
21
err error
22
}
23
24
// Has returns true if the cache has a template. The template
25
// is returned along with any errors if found.
26
func (t *Cache) Has(template string) (*Template, []byte, error) {
27
value, ok := t.items.Get(template)
28
if !ok {
29
return nil, nil, nil
30
}
31
return value.template, conversion.Bytes(value.raw), value.err
32
}
33
34
// Store stores a template with data and error
35
func (t *Cache) Store(id string, tpl *Template, raw []byte, err error) {
36
_ = t.items.Set(id, parsedTemplate{template: tpl, raw: conversion.String(raw), err: err})
37
}
38
39
// Purge the cache
40
func (t *Cache) Purge() {
41
t.items.Clear()
42
}
43
44