github.com/projectdiscovery/nuclei/v2@v2.9.15/pkg/templates/cache/cache.go (about)

     1  package cache
     2  
     3  import (
     4  	"sync"
     5  )
     6  
     7  // Templates is a cache for caching and storing templates for reuse.
     8  type Templates struct {
     9  	items *sync.Map
    10  }
    11  
    12  // New returns a new templates cache
    13  func New() *Templates {
    14  	return &Templates{items: &sync.Map{}}
    15  }
    16  
    17  type parsedTemplateErrHolder struct {
    18  	template interface{}
    19  	err      error
    20  }
    21  
    22  // Has returns true if the cache has a template. The template
    23  // is returned along with any errors if found.
    24  func (t *Templates) Has(template string) (interface{}, error) {
    25  	value, ok := t.items.Load(template)
    26  	if !ok || value == nil {
    27  		return nil, nil
    28  	}
    29  	templateError, ok := value.(parsedTemplateErrHolder)
    30  	if !ok {
    31  		return nil, nil
    32  	}
    33  	return templateError.template, templateError.err
    34  }
    35  
    36  // Store stores a template with data and error
    37  func (t *Templates) Store(template string, data interface{}, err error) {
    38  	t.items.Store(template, parsedTemplateErrHolder{template: data, err: err})
    39  }