caching

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2023 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultLevel2CacheExpiration  = time.Hour * 6
	DefaultCachePreUpdateDuration = time.Second * 5
)

Functions

This section is empty.

Types

type FailOverCache

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

FailOverCache implements fail-over caching strategy via two-level cache

func NewFailOverCache

func NewFailOverCache(exp1, exp2 time.Duration) *FailOverCache

NewFailOverCache instantiates a fail-over cache NOTE:

  • exp1: level 1 cache expiration, e.g. 5 minutes
  • exp2: level 2 cache expiration, DefaultLevel2CacheExpiration is recommended

func (*FailOverCache) Get

func (c *FailOverCache) Get(key string, fn RefreshFunc) (v interface{}, err error)

Get tries to get cached key, calls fn when cached needs to be updated. NOTE:

  • key: cache key
  • fn: business code to get the newest value of key, e.g. issuing an API call

func (*FailOverCache) Remove

func (c *FailOverCache) Remove(key string)

Remove removes both level 1 & level 2 cache

type RefreshFunc

type RefreshFunc func(key string) (interface{}, error)

RefreshFunc cache refresh function to retrieve the newest value, accepts a key as input which is also the cache key NOTE:

  • Handle function timeout carefully (better finish in no more than 2 seconds)
  • Avoid returning a nil value while error is nil too

Jump to

Keyboard shortcuts

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