-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.go
47 lines (38 loc) · 1.58 KB
/
cache.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package mcache
import (
"context"
"errors"
)
var (
KeyNotFoundError = errors.New("mcache: key not found.")
KeyExpiredError = errors.New("mcache: key expired.")
RedisNotFoundError = errors.New("mcache: redis not found.")
SerializeError = errors.New("mcache: must set WithSafeValPtrFunc option!")
KeyValueLenError = errors.New("mcache: len of key != len of value.")
DefaultValueSetError = errors.New("mcache: set def val, 1min expiration.")
)
type Cache interface {
Set(ctx context.Context, key string, value interface{}, opts ...Option) error
MSet(ctx context.Context, keys []string, values []interface{}, opts ...Option) error
Get(ctx context.Context, key string, opts ...Option) (interface{}, error)
MGet(ctx context.Context, keys []string, opts ...Option) (map[string]interface{}, error)
Remove(ctx context.Context, key string) bool
MRemove(ctx context.Context, keys []string) bool
Exists(ctx context.Context, key string) bool
//only for debug
DebugShardIndex(key string) uint64
debugLocalGet(ctx context.Context, key string) (interface{}, error)
debugLocalRemove(ctx context.Context, key string) bool
serializer
}
type (
LoaderFunc func(context.Context, string) (interface{}, error)
MLoaderFunc func(context.Context, []string) (map[string]interface{}, error)
valuePtrFunc func() interface{}
)
const (
defaultCacheSize = 1 << 7 //默认缓存容量
defaultShardCap = 1 << 6 //默认单片容量
defaultShardCount = 1 << 5 //默认分片数量
defaultExpiredAt = 100 * 365 * 24 * 3600e9 //100years
)