github.com/cozy/cozy-stack@v0.0.0-20240603063001-31110fa4cae1/pkg/cache/cache.go (about)

     1  package cache
     2  
     3  import (
     4  	"context"
     5  	"io"
     6  	"time"
     7  
     8  	"github.com/redis/go-redis/v9"
     9  )
    10  
    11  // Cache is a rudimentary key/value caching store backed by redis. It offers a
    12  // Get/Set interface as well a its gzip compressed alternative
    13  // GetCompressed/SetCompressed
    14  type Cache interface {
    15  	CheckStatus(ctx context.Context) (time.Duration, error)
    16  	Get(key string) ([]byte, bool)
    17  	MultiGet(keys []string) [][]byte
    18  	Keys(prefix string) []string
    19  	Clear(key string)
    20  	Set(key string, data []byte, expiration time.Duration)
    21  	SetNX(key string, data []byte, expiration time.Duration)
    22  	GetCompressed(key string) (io.Reader, bool)
    23  	SetCompressed(key string, data []byte, expiration time.Duration)
    24  	RefreshTTL(key string, expiration time.Duration)
    25  }
    26  
    27  type cacheEntry struct {
    28  	payload   []byte
    29  	expiredAt time.Time
    30  }
    31  
    32  // New instantiate a Cache Client.
    33  //
    34  // The backend selection is done based on the `client` argument. If a client is
    35  // given, the redis backend is chosen, if nil is provided the inmemory backend would
    36  // be chosen.
    37  func New(client redis.UniversalClient) Cache {
    38  	if client == nil {
    39  		return NewInMemory()
    40  	}
    41  
    42  	return NewRedis(client)
    43  }