dcache

package
v0.0.0-...-4713308 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 27, 2022 License: MIT Imports: 15 Imported by: 0

README

* DCache
a redis cache:
#+begin_src go
// Cache defines interface to cache
type Cache interface {
	// Get returns value of f while caching in redis and inmemcache
	// Inputs:
	// queryKey	 - key used in cache
	// target	 - receive the cached value, must be pointer
	// expire 	 - expiration of cache key
	// f		 - actual call that hits underlying data source
	// noCache 	 - whether force read from data source
	Get(ctx context.Context, queryKey QueryKey, target interface{}, expire time.Duration, f PassThroughFunc, noCache bool) error

	// GetWithExpire returns value of f while caching in redis and inmemcache
	// Inputs:
	// queryKey	 - key used in cache
	// target	 - receive the cached value, must be pointer
	// f		 - actual call that hits underlying data source, sets expire duration
	// noCache 	 - whether force read from data source
	GetWithExpire(ctx context.Context, queryKey string, target interface{}, f PassThroughExpireFunc, noCache bool) error

	// Set explicitly set a cache key to a val
	// Inputs:
	// key	  - key to set
	// val	  - val to set
	// ttl    - ttl of key
	Set(ctx context.Context, key QueryKey, val interface{}, ttl time.Duration) error

	// Invalidate explicitly invalidates a cache key
	// Inputs:
	// key    - key to invalidate
	Invalidate(ctx context.Context, key QueryKey) error

	// Close closes resources used by cache
	Close()
}
#+end_src

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrTimeout is timeout error
	ErrTimeout = errors.New("timeout")
	// ErrInternal should never happen
	ErrInternal = errors.New("internal")

	ErrNil = errors.New("nil")
)

Functions

func SetNowFunc

func SetNowFunc(f func() time.Time)

SetNowFunc is a helper function to replace time.Now()

Types

type Cache

type Cache interface {
	// Get returns value of f while caching in redis and inmemcache
	// Inputs:
	// queryKey	 - key used in cache
	// target	 - receive the cached value, must be pointer
	// expire 	 - expiration of cache key
	// f		 - actual call that hits underlying data source
	// noCache 	 - whether force read from data source
	Get(ctx context.Context, queryKey QueryKey, target interface{}, expire time.Duration, f PassThroughFunc, noCache bool) error

	// GetWithExpire returns value of f while caching in redis
	// Inputs:
	// queryKey	 - key used in cache
	// target	 - receive the cached value, must be pointer
	// f		 - actual call that hits underlying data source, sets expire duration
	// noCache 	 - whether force read from data source
	GetWithExpire(ctx context.Context, queryKey string, target interface{}, f PassThroughExpireFunc, noCache bool) error

	// Set explicitly set a cache key to a val
	// Inputs:
	// key	  - key to set
	// val	  - val to set
	// ttl    - ttl of key
	Set(ctx context.Context, key QueryKey, val interface{}, ttl time.Duration) error

	// Invalidate explicitly invalidates a cache key
	// Inputs:
	// key    - key to invalidate
	Invalidate(ctx context.Context, key QueryKey) error

	// Close closes resources used by cache
	Close()
}

Cache defines interface to cache

func NewCache

func NewCache(
	appName string,
	primaryClient redis.UniversalClient,
	inMemCache *freecache.Cache,
	readThroughPerKeyLimit time.Duration,
) (Cache, error)

NewCache creates a new redis cache with inmem cache

type CacheDuration

type CacheDuration time.Duration

func (CacheDuration) ToDuration

func (c CacheDuration) ToDuration() time.Duration

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client captures redis connection

func (*Client) Close

func (c *Client) Close()

Close terminates redis pubsub gracefully

func (*Client) Get

func (c *Client) Get(ctx context.Context, queryKey QueryKey, target interface{}, expire time.Duration, f PassThroughFunc, noCache bool) error

Get implements Cache interface

func (*Client) GetWithExpire

func (c *Client) GetWithExpire(ctx context.Context, queryKey QueryKey, target interface{}, f PassThroughExpireFunc, noCache bool) error

GetWithExpire implements Cache interface

func (*Client) Invalidate

func (c *Client) Invalidate(ctx context.Context, key QueryKey) error

Invalidate implements Cache interface

func (*Client) Set

func (c *Client) Set(ctx context.Context, key QueryKey, val interface{}, ttl time.Duration) error

Set implements Cache interface

type PassThroughExpireFunc

type PassThroughExpireFunc = func() (interface{}, time.Duration, error)

PassThroughExpireFunc is the actual call to underlying data source while returning a duration as expire timer

type PassThroughFunc

type PassThroughFunc = func() (interface{}, error)

PassThroughFunc is the actual call to underlying data source

type QueryKey

type QueryKey = string

QueryKey is an alias to string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL