cache.go 912 B

12345678910111213141516171819202122232425262728293031323334
  1. package jet
  2. import "sync"
  3. // Cache is the interface Jet uses to store and retrieve parsed templates.
  4. type Cache interface {
  5. // Get fetches a template from the cache. If Get returns nil, the same path with a different extension will be tried.
  6. // If Get() returns nil for all configured extensions, the same path and extensions will be tried on the Set's Loader.
  7. Get(templatePath string) *Template
  8. // Put places the result of parsing a template "file"/string in the cache.
  9. Put(templatePath string, t *Template)
  10. }
  11. // cache is the cache used by default in a new Set.
  12. type cache struct {
  13. m sync.Map
  14. }
  15. // compile-time check that cache implements Cache
  16. var _ Cache = (*cache)(nil)
  17. func (c *cache) Get(templatePath string) *Template {
  18. _t, ok := c.m.Load(templatePath)
  19. if !ok {
  20. return nil
  21. }
  22. return _t.(*Template)
  23. }
  24. func (c *cache) Put(templatePath string, t *Template) {
  25. c.m.Store(templatePath, t)
  26. }