github.com/Jeffail/benthos/v3@v3.65.0/internal/bundle/caches.go (about) 1 package bundle 2 3 import ( 4 "sort" 5 6 "github.com/Jeffail/benthos/v3/internal/docs" 7 "github.com/Jeffail/benthos/v3/lib/cache" 8 "github.com/Jeffail/benthos/v3/lib/types" 9 ) 10 11 // AllCaches is a set containing every single cache that has been imported. 12 var AllCaches = &CacheSet{ 13 specs: map[string]cacheSpec{}, 14 } 15 16 //------------------------------------------------------------------------------ 17 18 // CacheAdd adds a new cache to this environment by providing a constructor 19 // and documentation. 20 func (e *Environment) CacheAdd(constructor CacheConstructor, spec docs.ComponentSpec) error { 21 return e.caches.Add(constructor, spec) 22 } 23 24 // CacheInit attempts to initialise a cache from a config. 25 func (e *Environment) CacheInit(conf cache.Config, mgr NewManagement) (types.Cache, error) { 26 return e.caches.Init(conf, mgr) 27 } 28 29 // CacheDocs returns a slice of cache specs, which document each method. 30 func (e *Environment) CacheDocs() []docs.ComponentSpec { 31 return e.caches.Docs() 32 } 33 34 //------------------------------------------------------------------------------ 35 36 // CacheConstructor constructs an cache component. 37 type CacheConstructor func(cache.Config, NewManagement) (types.Cache, error) 38 39 type cacheSpec struct { 40 constructor CacheConstructor 41 spec docs.ComponentSpec 42 } 43 44 // CacheSet contains an explicit set of caches available to a Benthos service. 45 type CacheSet struct { 46 specs map[string]cacheSpec 47 } 48 49 // Add a new cache to this set by providing a spec (name, documentation, and 50 // constructor). 51 func (s *CacheSet) Add(constructor CacheConstructor, spec docs.ComponentSpec) error { 52 if s.specs == nil { 53 s.specs = map[string]cacheSpec{} 54 } 55 s.specs[spec.Name] = cacheSpec{ 56 constructor: constructor, 57 spec: spec, 58 } 59 docs.RegisterDocs(spec) 60 return nil 61 } 62 63 // Init attempts to initialise an cache from a config. 64 func (s *CacheSet) Init(conf cache.Config, mgr NewManagement) (types.Cache, error) { 65 spec, exists := s.specs[conf.Type] 66 if !exists { 67 // TODO: V4 Remove this 68 if ctor, exists := cache.GetDeprecatedPlugin(conf.Type); exists { 69 return ctor(conf, mgr, mgr.Logger(), mgr.Metrics()) 70 } 71 return nil, types.ErrInvalidCacheType 72 } 73 return spec.constructor(conf, mgr) 74 } 75 76 // Docs returns a slice of cache specs, which document each method. 77 func (s *CacheSet) Docs() []docs.ComponentSpec { 78 var docs []docs.ComponentSpec 79 for _, v := range s.specs { 80 docs = append(docs, v.spec) 81 } 82 sort.Slice(docs, func(i, j int) bool { 83 return docs[i].Name < docs[j].Name 84 }) 85 return docs 86 } 87 88 // DocsFor returns the documentation for a given component name, returns a 89 // boolean indicating whether the component name exists. 90 func (s *CacheSet) DocsFor(name string) (docs.ComponentSpec, bool) { 91 c, ok := s.specs[name] 92 if !ok { 93 return docs.ComponentSpec{}, false 94 } 95 return c.spec, true 96 }