add transient to service loader, clear cache when setting a new value for the service
This commit is contained in:
parent
a72de0a62f
commit
21ac153d5b
1 changed files with 8 additions and 1 deletions
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue