GuardedSet takes a key and a value and sets the value in the cache if it cannot be found. Returns true if value was set, false otherwise. Returns number of bytes written. Returns an error if the underlying cache returns an error during setting the value or if the value couldn't be marshalled.
(key string, value any)
| 98 | // written. Returns an error if the underlying cache returns an error during setting |
| 99 | // the value or if the value couldn't be marshalled. |
| 100 | func (s Cache) GuardedSet(key string, value any) (bool, int, error) { |
| 101 | if ok, err := get(s.lru, key, value); err != nil { |
| 102 | return false, 0, fmt.Errorf("error checking cache entry exists: %w", err) |
| 103 | } else if ok { |
| 104 | return false, 0, nil |
| 105 | } else { |
| 106 | // Currently we don't need to know about evictions with GuardedSet so ignoring |
| 107 | // to keep interface sane |
| 108 | bytesWritten, _, err := set(s.lru, key, value) |
| 109 | return true, bytesWritten, err |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | // Len returns the length of the current cache |
| 114 | func (s Cache) Len() int { |