| 42 | } |
| 43 | |
| 44 | func 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 | |
| 85 | func withLRUCache[T any](key string, fetch func() (T, error)) (data T, err error) { |
| 86 | // check cache store first |