Documentation ¶
Overview ¶
Package rockscache The first Redis cache library to ensure eventual consistency and strong consistency with DB.
Index ¶
- func SetVerbose(v bool)
- type Client
- func (c *Client) Fetch(key string, expire time.Duration, fn func() (string, error)) (string, error)
- func (c *Client) Fetch2(ctx context.Context, key string, expire time.Duration, ...) (string, error)
- func (c *Client) FetchBatch(keys []string, expire time.Duration, ...) (map[int]string, error)
- func (c *Client) FetchBatch2(ctx context.Context, keys []string, expire time.Duration, ...) (map[int]string, error)
- func (c *Client) LockForUpdate(ctx context.Context, key string, owner string) error
- func (c *Client) RawGet(ctx context.Context, key string) (string, error)
- func (c *Client) RawSet(ctx context.Context, key string, value string, expire time.Duration) error
- func (c *Client) TagAsDeleted(key string) error
- func (c *Client) TagAsDeleted2(ctx context.Context, key string) error
- func (c *Client) TagAsDeletedBatch(keys []string) error
- func (c *Client) TagAsDeletedBatch2(ctx context.Context, keys []string) error
- func (c *Client) UnlockForUpdate(ctx context.Context, key string, owner string) error
- type Options
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Client ¶
type Client struct { Options Options // contains filtered or unexported fields }
Client delay client
func NewClient ¶
NewClient return a new rockscache client for each key, rockscache client store a hash set, the hash set contains the following fields: value: the value of the key lockUntil: the time when the lock is released. lockOwner: the owner of the lock. if a thread query the cache for data, and no cache exists, it will lock the key before querying data in DB
func (*Client) Fetch ¶
Fetch returns the value store in cache indexed by the key. If the key doest not exists, call fn to get result, store it in cache, then return.
func (*Client) Fetch2 ¶ added in v0.0.12
func (c *Client) Fetch2(ctx context.Context, key string, expire time.Duration, fn func() (string, error)) (string, error)
Fetch2 returns the value store in cache indexed by the key. If the key doest not exists, call fn to get result, store it in cache, then return.
func (*Client) FetchBatch ¶ added in v0.1.0
func (c *Client) FetchBatch(keys []string, expire time.Duration, fn func(idxs []int) (map[int]string, error)) (map[int]string, error)
FetchBatch returns a map with values indexed by index of keys list. 1. the first parameter is the keys list of the data 2. the second parameter is the data expiration time 3. the third parameter is the batch data fetch function which is called when the cache does not exist the parameter of the batch data fetch function is the index list of those keys missing in cache, which can be used to form a batch query for missing data. the return value of the batch data fetch function is a map, with key of the index and value of the corresponding data in form of string
func (*Client) FetchBatch2 ¶ added in v0.1.0
func (c *Client) FetchBatch2(ctx context.Context, keys []string, expire time.Duration, fn func(idxs []int) (map[int]string, error)) (map[int]string, error)
FetchBatch2 is same with FetchBatch, except that a user defined context.Context can be provided.
func (*Client) LockForUpdate ¶ added in v0.0.2
LockForUpdate locks the key, used in very strict strong consistency mode
func (*Client) RawGet ¶ added in v0.0.2
RawGet returns the value store in cache indexed by the key, no matter if the key locked or not
func (*Client) RawSet ¶ added in v0.0.2
RawSet sets the value store in cache indexed by the key, no matter if the key locked or not
func (*Client) TagAsDeleted ¶ added in v0.0.7
TagAsDeleted a key, the key will expire after delay time.
func (*Client) TagAsDeleted2 ¶ added in v0.0.12
TagAsDeleted2 a key, the key will expire after delay time.
func (*Client) TagAsDeletedBatch ¶ added in v0.1.0
TagAsDeletedBatch a key list, the keys in list will expire after delay time.
func (*Client) TagAsDeletedBatch2 ¶ added in v0.1.0
TagAsDeletedBatch2 a key list, the keys in list will expire after delay time.
type Options ¶
type Options struct { // Delay is the delay delete time for keys that are tag deleted. default is 10s Delay time.Duration // EmptyExpire is the expire time for empty result. default is 60s EmptyExpire time.Duration // LockExpire is the expire time for the lock which is allocated when updating cache. default is 3s // should be set to the max of the underling data calculating time. LockExpire time.Duration // LockSleep is the sleep interval time if try lock failed. default is 100ms LockSleep time.Duration // WaitReplicas is the number of replicas to wait for. default is 0 // if WaitReplicas is > 0, it will use redis WAIT command to wait for TagAsDeleted synchronized. WaitReplicas int // WaitReplicasTimeout is the number of replicas to wait for. default is 3000ms // if WaitReplicas is > 0, WaitReplicasTimeout is the timeout for WAIT command. WaitReplicasTimeout time.Duration // RandomExpireAdjustment is the random adjustment for the expire time. default 0.1 // if the expire time is set to 600s, and this value is set to 0.1, then the actual expire time will be 540s - 600s // solve the problem of cache avalanche. RandomExpireAdjustment float64 // CacheReadDisabled is the flag to disable read cache. default is false // when redis is down, set this flat to downgrade. DisableCacheRead bool // CacheDeleteDisabled is the flag to disable delete cache. default is false // when redis is down, set this flat to downgrade. DisableCacheDelete bool // StrongConsistency is the flag to enable strong consistency. default is false // if enabled, the Fetch result will be consistent with the db result, but performance is bad. StrongConsistency bool // Context for redis command Context context.Context }
Options represents the options for rockscache client