Skip to content

Commit

Permalink
add transient to service loader, clear cache when setting a new value…
Browse files Browse the repository at this point in the history
… for the service
  • Loading branch information
maddalax committed Oct 23, 2024
1 parent a72de0a commit 21ac153
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion framework/service/locator.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type Lifecycle = string

var (
Singleton Lifecycle = "singleton"
Transient Lifecycle = "transient"
)

type Provider struct {
Expand All @@ -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]
}
Expand Down Expand Up @@ -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)
}

0 comments on commit 21ac153

Please sign in to comment.