MCPcopy Create free account
hub / github.com/devnullvoid/pvetui / Get

Method Get

internal/cache/badger_cache.go:152–202  ·  view source on GitHub ↗

Get retrieves data from the cache.

(key string, dest interface{})

Source from the content-addressed store, hash-verified

150
151// Get retrieves data from the cache.
152func (c *BadgerCache) Get(key string, dest interface{}) (bool, error) {
153 var found bool
154
155 err := c.db.View(func(txn *badger.Txn) error {
156 item, err := txn.Get([]byte(key))
157 if err == badger.ErrKeyNotFound {
158 getCacheLogger().Debug("Cache miss for: %s", key)
159
160 return nil
161 }
162
163 if err != nil {
164 return fmt.Errorf("badger get operation: %w", err)
165 }
166
167 return item.Value(func(val []byte) error {
168 // Parse the cache item
169 var cacheItem CacheItem
170 if err := json.Unmarshal(val, &cacheItem); err != nil {
171 return fmt.Errorf("unmarshal cache item: %w", err)
172 }
173
174 // Check if the item is expired
175 if cacheItem.TTL > 0 && time.Now().Unix()-cacheItem.Timestamp > cacheItem.TTL {
176 getCacheLogger().Debug("Cache item expired: %s", key)
177 // Item is expired, we'll handle deletion outside this transaction
178 return nil
179 }
180
181 // Item is valid
182 found = true
183
184 getCacheLogger().Debug("Cache hit for: %s", key)
185
186 // Unmarshal the raw JSON directly into the destination (no double marshaling)
187 if err := json.Unmarshal(cacheItem.Data, dest); err != nil {
188 return fmt.Errorf("unmarshal into destination: %w", err)
189 }
190
191 return nil
192 })
193 })
194
195 // If the item was expired, delete it in a separate transaction
196 if err == nil && !found {
197 // We don't care about errors here, as it's just cleanup
198 _ = c.Delete(key)
199 }
200
201 return found, err
202}
203
204// Set stores data in the cache.
205func (c *BadgerCache) Set(key string, data interface{}, ttl time.Duration) error {

Callers 3

testCachePersistenceFunction · 0.95

Calls 4

DeleteMethod · 0.95
getCacheLoggerFunction · 0.85
GetMethod · 0.65
DebugMethod · 0.65

Tested by 3

testCachePersistenceFunction · 0.76