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 ¶
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
Click to show internal directories.
Click to hide internal directories.