add transient to service loader, clear cache when setting a new value for the service

This commit is contained in:
maddalax 2024-10-23 11:10:48 -05:00
parent a72de0a62f
commit 21ac153d5b

View file

@ -10,6 +10,7 @@ type Lifecycle = string
var (
Singleton Lifecycle = "singleton"
Transient Lifecycle = "transient"
)
type Provider struct {
@ -35,6 +36,10 @@ func (l *Locator) setCache(key string, value any) {
l.cache[key] = value
}
func (l *Locator) clearCache(key string) {
delete(l.cache, key)
}
func (l *Locator) getCache(key string) any {
return l.cache[key]
}
@ -68,10 +73,12 @@ func Get[T any](locator *Locator) *T {
func Set[T any](locator *Locator, lifecycle Lifecycle, value func() *T) {
t := reflect.TypeOf(value)
rt := t.Out(0)
locator.services[rt.String()] = Provider{
key := rt.String()
locator.services[key] = Provider{
cb: func() any {
return value()
},
lifecycle: lifecycle,
}
locator.clearCache(key)
}