2024-03-13 02:27:15 +01:00
|
|
|
package templates
|
2021-08-28 00:15:28 +05:30
|
|
|
|
|
|
|
|
import (
|
2024-03-10 22:29:55 +01:00
|
|
|
mapsutil "github.com/projectdiscovery/utils/maps"
|
2021-08-28 00:15:28 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Templates is a cache for caching and storing templates for reuse.
|
2024-03-13 02:27:15 +01:00
|
|
|
type Cache struct {
|
2024-03-10 22:29:55 +01:00
|
|
|
items *mapsutil.SyncLockMap[string, parsedTemplateErrHolder]
|
2021-08-28 00:15:28 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// New returns a new templates cache
|
2024-03-13 02:27:15 +01:00
|
|
|
func NewCache() *Cache {
|
|
|
|
|
return &Cache{items: mapsutil.NewSyncLockMap[string, parsedTemplateErrHolder]()}
|
2021-08-28 00:15:28 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type parsedTemplateErrHolder struct {
|
2024-03-13 02:27:15 +01:00
|
|
|
template *Template
|
2021-08-28 00:15:28 +05:30
|
|
|
err error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Has returns true if the cache has a template. The template
|
|
|
|
|
// is returned along with any errors if found.
|
2024-03-13 02:27:15 +01:00
|
|
|
func (t *Cache) Has(template string) (*Template, error) {
|
2024-03-10 22:29:55 +01:00
|
|
|
value, ok := t.items.Get(template)
|
2021-08-28 00:15:28 +05:30
|
|
|
if !ok {
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
2024-03-10 22:29:55 +01:00
|
|
|
return value.template, value.err
|
2021-08-28 00:15:28 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Store stores a template with data and error
|
2024-03-13 02:27:15 +01:00
|
|
|
func (t *Cache) Store(template string, data *Template, err error) {
|
2024-03-10 22:29:55 +01:00
|
|
|
_ = t.items.Set(template, parsedTemplateErrHolder{template: data, err: err})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Purge the cache
|
2024-03-13 02:27:15 +01:00
|
|
|
func (t *Cache) Purge() {
|
2024-03-10 22:29:55 +01:00
|
|
|
t.items.Clear()
|
2021-08-28 00:15:28 +05:30
|
|
|
}
|