(keys []K, fetch func(keys []K) (map[K]V, error))
| 89 | } |
| 90 | |
| 91 | func (x *LazyLRU[K, V]) GetBatch(keys []K, fetch func(keys []K) (map[K]V, error)) (map[K]V, error) { |
| 92 | var ( |
| 93 | err error |
| 94 | once sync.Once |
| 95 | ) |
| 96 | |
| 97 | res := make(map[K]V) |
| 98 | queries := make([]K, 0, len(keys)) |
| 99 | |
| 100 | for _, key := range keys { |
| 101 | x.lock.Lock() |
| 102 | v, ok := x.core.Get(key) |
| 103 | x.lock.Unlock() |
| 104 | if ok { |
| 105 | v.lock.Lock() |
| 106 | expires, value, err1 := v.expires, v.value, v.err |
| 107 | v.lock.Unlock() |
| 108 | if expires != 0 && expires > time.Now().UnixMilli() { |
| 109 | x.target.IncrGetHit() |
| 110 | res[key] = value |
| 111 | if err1 != nil { |
| 112 | once.Do(func() { |
| 113 | err = err1 |
| 114 | }) |
| 115 | } |
| 116 | continue |
| 117 | } |
| 118 | } |
| 119 | queries = append(queries, key) |
| 120 | } |
| 121 | |
| 122 | if len(queries) == 0 { |
| 123 | return res, err |
| 124 | } |
| 125 | |
| 126 | values, fetchErr := fetch(queries) |
| 127 | if fetchErr != nil { |
| 128 | once.Do(func() { |
| 129 | err = fetchErr |
| 130 | }) |
| 131 | } |
| 132 | |
| 133 | for key, val := range values { |
| 134 | v := &lazyLruItem[V]{} |
| 135 | v.value = val |
| 136 | |
| 137 | if err == nil { |
| 138 | v.expires = time.Now().Add(x.successTTL).UnixMilli() |
| 139 | x.target.IncrGetSuccess() |
| 140 | } else { |
| 141 | v.expires = time.Now().Add(x.failedTTL).UnixMilli() |
| 142 | x.target.IncrGetFailed() |
| 143 | } |
| 144 | |
| 145 | x.lock.Lock() |
| 146 | x.core.Add(key, v) |
| 147 | x.lock.Unlock() |
| 148 | res[key] = val |
nothing calls this directly
no test coverage detected