github.com/msales/pkg/v3@v3.24.0/cache/cache.go (about)

     1  package cache
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"time"
     7  )
     8  
     9  type key int
    10  
    11  const (
    12  	ctxKey key = iota
    13  )
    14  
    15  var (
    16  	// ErrCacheMiss means that a Get failed because the item wasn't present.
    17  	ErrCacheMiss = errors.New("cache: miss")
    18  
    19  	// ErrNotStored means the conditional write (Add or Replace) failed because
    20  	// the condition was not met.
    21  	ErrNotStored = errors.New("cache: not stored")
    22  
    23  	// Null is the null Cache instance.
    24  	Null = &nullCache{}
    25  )
    26  
    27  // Cache represents a cache instance.
    28  type Cache interface {
    29  	// Get gets the item for the given key.
    30  	Get(key string) *Item
    31  
    32  	// GetMulti gets the items for the given keys.
    33  	GetMulti(keys ...string) ([]*Item, error)
    34  
    35  	// Set sets the item in the cache.
    36  	Set(key string, value interface{}, expire time.Duration) error
    37  
    38  	// Add sets the item in the cache, but only if the key does not already exist.
    39  	Add(key string, value interface{}, expire time.Duration) error
    40  
    41  	// Replace sets the item in the cache, but only if the key already exists.
    42  	Replace(key string, value interface{}, expire time.Duration) error
    43  
    44  	// Delete deletes the item with the given key.
    45  	Delete(key string) error
    46  
    47  	// Inc increments a key by the value.
    48  	Inc(key string, value uint64) (int64, error)
    49  
    50  	// Dec decrements a key by the value.
    51  	Dec(key string, value uint64) (int64, error)
    52  }
    53  
    54  // WithCache sets Cache in the context.
    55  func WithCache(ctx context.Context, cache Cache) context.Context {
    56  	return context.WithValue(ctx, ctxKey, cache)
    57  }
    58  
    59  // FromContext returns the instance of Cache in the context.
    60  func FromContext(ctx context.Context) (Cache, bool) {
    61  	cache, ok := ctx.Value(ctxKey).(Cache)
    62  	return cache, ok
    63  }
    64  
    65  // Get gets the item for the given key.
    66  func Get(ctx context.Context, key string) *Item {
    67  	c := getCache(ctx)
    68  	return c.Get(key)
    69  }
    70  
    71  // GetMulti gets the items for the given keys.
    72  func GetMulti(ctx context.Context, keys ...string) ([]*Item, error) {
    73  	c := getCache(ctx)
    74  	return c.GetMulti(keys...)
    75  }
    76  
    77  // Set sets the item in the cache.
    78  func Set(ctx context.Context, key string, value interface{}, expire time.Duration) error {
    79  	c := getCache(ctx)
    80  	return c.Set(key, value, expire)
    81  }
    82  
    83  // Add sets the item in the cache, but only if the key does not already exist.
    84  func Add(ctx context.Context, key string, value interface{}, expire time.Duration) error {
    85  	c := getCache(ctx)
    86  	return c.Add(key, value, expire)
    87  }
    88  
    89  // Replace sets the item in the cache, but only if the key already exists.
    90  func Replace(ctx context.Context, key string, value interface{}, expire time.Duration) error {
    91  	c := getCache(ctx)
    92  	return c.Replace(key, value, expire)
    93  }
    94  
    95  // Delete deletes the item with the given key.
    96  func Delete(ctx context.Context, key string) error {
    97  	c := getCache(ctx)
    98  	return c.Delete(key)
    99  }
   100  
   101  // Inc increments a key by the value.
   102  func Inc(ctx context.Context, key string, value uint64) (int64, error) {
   103  	c := getCache(ctx)
   104  	return c.Inc(key, value)
   105  }
   106  
   107  // Dec decrements a key by the value.
   108  func Dec(ctx context.Context, key string, value uint64) (int64, error) {
   109  	c := getCache(ctx)
   110  	return c.Dec(key, value)
   111  }
   112  
   113  func getCache(ctx context.Context) Cache {
   114  	if c, ok := FromContext(ctx); ok {
   115  		return c
   116  	}
   117  	return Null
   118  }
   119  
   120  type nullDecoder struct{}
   121  
   122  func (d nullDecoder) Bool(v []byte) (bool, error) {
   123  	return false, nil
   124  }
   125  
   126  func (d nullDecoder) Int64(v []byte) (int64, error) {
   127  	return 0, nil
   128  }
   129  
   130  func (d nullDecoder) Uint64(v []byte) (uint64, error) {
   131  	return 0, nil
   132  }
   133  
   134  func (d nullDecoder) Float64(v []byte) (float64, error) {
   135  	return 0, nil
   136  }
   137  
   138  type nullCache struct{}
   139  
   140  // Get gets the item for the given key.
   141  func (c nullCache) Get(key string) *Item {
   142  	return &Item{decoder: nullDecoder{}, value: []byte{}}
   143  }
   144  
   145  // GetMulti gets the items for the given keys.
   146  func (c nullCache) GetMulti(keys ...string) ([]*Item, error) {
   147  	return []*Item{}, nil
   148  }
   149  
   150  // Set sets the item in the cache.
   151  func (c nullCache) Set(key string, value interface{}, expire time.Duration) error {
   152  	return nil
   153  }
   154  
   155  // Add sets the item in the cache, but only if the key does not already exist.
   156  func (c nullCache) Add(key string, value interface{}, expire time.Duration) error {
   157  	return nil
   158  }
   159  
   160  // Replace sets the item in the cache, but only if the key already exists.
   161  func (c nullCache) Replace(key string, value interface{}, expire time.Duration) error {
   162  	return nil
   163  }
   164  
   165  // Delete deletes the item with the given key.
   166  func (c nullCache) Delete(key string) error {
   167  	return nil
   168  }
   169  
   170  // Inc increments a key by the value.
   171  func (c nullCache) Inc(key string, value uint64) (int64, error) {
   172  	return 0, nil
   173  }
   174  
   175  // Dec decrements a key by the value.
   176  func (c nullCache) Dec(key string, value uint64) (int64, error) {
   177  	return 0, nil
   178  }