MCPcopy
hub / github.com/esm-dev/esm.sh / withCache

Function withCache

server/cache.go:44–83  ·  view source on GitHub ↗
(key string, cacheTtl time.Duration, fetch func() (data T, aliasKey string, err error))

Source from the content-addressed store, hash-verified

42}
43
44func withCache[T any](key string, cacheTtl time.Duration, fetch func() (data T, aliasKey string, err error)) (data T, err error) {
45 if cacheTtl == 0 {
46 data, _, err = fetch()
47 return
48 }
49
50 // check cache store first
51 if v, ok := cacheStore.Load(key); ok {
52 item := v.(*cacheItem)
53 if item.exp >= time.Now().UnixMilli() {
54 return item.data.(T), nil
55 }
56 }
57
58 unlock := cacheMutex.Lock(key)
59 defer unlock()
60
61 // check cache store again after get lock
62 if cacheTtl > time.Millisecond {
63 if v, ok := cacheStore.Load(key); ok {
64 item := v.(*cacheItem)
65 if item.exp >= time.Now().UnixMilli() {
66 return item.data.(T), nil
67 }
68 }
69 }
70
71 var aliasKey string
72 data, aliasKey, err = fetch()
73 if err != nil {
74 return
75 }
76
77 exp := time.Now().Add(cacheTtl)
78 cacheStore.Store(key, &cacheItem{exp.UnixMilli(), data})
79 if aliasKey != "" && aliasKey != key {
80 cacheStore.Store(aliasKey, &cacheItem{exp.UnixMilli(), data})
81 }
82 return
83}
84
85func withLRUCache[T any](key string, fetch func() (T, error)) (data T, err error) {
86 // check cache store first

Callers 7

esmRouterFunction · 0.85
listGhRepoRefsContextFunction · 0.85
resolveGhPackageVersionFunction · 0.85
resolvePrPackageVersionFunction · 0.85
getPackageInfoContextMethod · 0.85
TestCacheFunction · 0.85

Calls 1

AddMethod · 0.80

Tested by 1

TestCacheFunction · 0.68