From 21ac153d5b291bdb0431e63ee2960c2b5722c3f4 Mon Sep 17 00:00:00 2001 From: maddalax Date: Wed, 23 Oct 2024 11:10:48 -0500 Subject: [PATCH] add transient to service loader, clear cache when setting a new value for the service --- framework/service/locator.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/framework/service/locator.go b/framework/service/locator.go index 855cae02..3cc79d44 100644 --- a/framework/service/locator.go +++ b/framework/service/locator.go @@ -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) }