|
4 | 4 | package cache
|
5 | 5 |
|
6 | 6 | import (
|
| 7 | + "errors" |
7 | 8 | "time"
|
8 | 9 | )
|
9 | 10 |
|
10 |
| -// Cache is a representation of any cache store that has keys and values |
| 11 | +// ErrKeyNotFound is the error when the given key is not found |
| 12 | +var ErrKeyNotFound = errors.New("key not found") |
| 13 | + |
| 14 | +// Cache is a representation of a cache store that aims to replace cache.Cache |
11 | 15 | type Cache interface {
|
12 | 16 | // Purge is used to completely clear the cache.
|
13 |
| - Purge() |
14 |
| - |
15 |
| - // Add adds the given key and value to the store without an expiry. |
16 |
| - Add(key string, value interface{}) |
| 17 | + Purge() error |
17 | 18 |
|
18 |
| - // AddWithDefaultExpires adds the given key and value to the store with the default expiry. |
19 |
| - AddWithDefaultExpires(key string, value interface{}) |
| 19 | + // Set adds the given key and value to the store without an expiry. If the key already exists, |
| 20 | + // it will overwrite the previous value. |
| 21 | + Set(key string, value interface{}) error |
20 | 22 |
|
21 |
| - // AddWithExpiresInSecs adds the given key and value to the cache with the given expiry. |
22 |
| - AddWithExpiresInSecs(key string, value interface{}, expireAtSecs int64) |
| 23 | + // SetWithDefaultExpiry adds the given key and value to the store with the default expiry. If |
| 24 | + // the key already exists, it will overwrite the previoous value |
| 25 | + SetWithDefaultExpiry(key string, value interface{}) error |
23 | 26 |
|
24 |
| - // Get returns the value stored in the cache for a key, or nil if no value is present. The ok result indicates whether value was found in the cache. |
25 |
| - Get(key string) (value interface{}, ok bool) |
| 27 | + // SetWithExpiry adds the given key and value to the cache with the given expiry. If the key |
| 28 | + // already exists, it will overwrite the previoous value |
| 29 | + SetWithExpiry(key string, value interface{}, ttl time.Duration) error |
26 | 30 |
|
27 |
| - // GetOrAdd returns the existing value for the key if present. Otherwise, it stores and returns the given value. The loaded result is true if the value was loaded, false if stored. |
28 |
| - // This API intentionally deviates from the Add-only variants above for simplicity. We should simplify the entire API in the future. |
29 |
| - GetOrAdd(key string, value interface{}, ttl time.Duration) (actual interface{}, loaded bool) |
| 31 | + // Get the content stored in the cache for the given key, and decode it into the value interface. |
| 32 | + // Return ErrKeyNotFound if the key is missing from the cache |
| 33 | + Get(key string, value interface{}) error |
30 | 34 |
|
31 |
| - // Remove deletes the value for a key. |
32 |
| - Remove(key string) |
| 35 | + // Remove deletes the value for a given key. |
| 36 | + Remove(key string) error |
33 | 37 |
|
34 | 38 | // Keys returns a slice of the keys in the cache.
|
35 |
| - Keys() []string |
| 39 | + Keys() ([]string, error) |
36 | 40 |
|
37 | 41 | // Len returns the number of items in the cache.
|
38 |
| - Len() int |
39 |
| - |
40 |
| - // Name identifies this cache instance among others in the system. |
41 |
| - Name() string |
| 42 | + Len() (int, error) |
42 | 43 |
|
43 | 44 | // GetInvalidateClusterEvent returns the cluster event configured when this cache was created.
|
44 | 45 | GetInvalidateClusterEvent() string
|
45 |
| -} |
46 | 46 |
|
47 |
| -// Provider defines how to create new caches |
48 |
| -type Provider interface { |
49 |
| - // Connect opens a new connection to the cache using specific provider parameters. |
50 |
| - Connect() |
51 |
| - |
52 |
| - // NewCache creates a new cache with given size. |
53 |
| - NewCache(size int) Cache |
54 |
| - |
55 |
| - // NewCacheWithParams creates a new cache with the given parameters. |
56 |
| - NewCacheWithParams(size int, name string, defaultExpiry int64, invalidateClusterEvent string) Cache |
57 |
| - |
58 |
| - // Close releases any resources used by the cache provider. |
59 |
| - Close() |
| 47 | + // Name returns the name of the cache |
| 48 | + Name() string |
60 | 49 | }
|
0 commit comments