-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.go
55 lines (44 loc) · 1.13 KB
/
cache.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package di
import "reflect"
// callsite result cache location
type CacheLocation byte
const (
CacheLocation_Root CacheLocation = iota
CacheLocation_Scope
CacheLocation_Dispose
CacheLocation_None
)
var NoneResultCache = newResultCache(CacheLocation_None, EmptyServiceCacheKey)
type ServiceCacheKey struct {
// Type of service being cached
ServiceType reflect.Type
// Reverse index of the service when resolved in slice where default instance gets slot 0.
Slot int
}
var EmptyServiceCacheKey = ServiceCacheKey{nil, 0}
// callsite result cache
type ResultCache struct {
Location CacheLocation
Key ServiceCacheKey
}
func newResultCache(loc CacheLocation, key ServiceCacheKey) ResultCache {
return ResultCache{
Location: loc,
Key: key,
}
}
func newResultCacheWithLifetime(lifetime Lifetime, typ reflect.Type, slot int) ResultCache {
loc := CacheLocation_None
switch lifetime {
case Lifetime_Singleton:
loc = CacheLocation_Root
case Lifetime_Scoped:
loc = CacheLocation_Scope
case Lifetime_Transient:
loc = CacheLocation_Dispose
}
return ResultCache{
Location: loc,
Key: ServiceCacheKey{typ, slot},
}
}