forked from leslie-fei/fastcache
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
87 lines (80 loc) · 1.99 KB
/
config.go
File metadata and controls
87 lines (80 loc) · 1.99 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package fastcache
import (
"encoding/binary"
"encoding/json"
"runtime"
)
type MemoryType int
const (
GO MemoryType = 1
SHM = 2
MMAP = 3
)
type Config struct {
// memory type in GO SHM MMAP
MemoryType MemoryType
// shard memory key
MemoryKey string
// 支持存储的最大数量, 超过将会触发LRU
MaxElementLen uint64
// 大数据块的最大数量, 超过这个数量将会触发淘汰, 并且这个数值将会用来初始化大数据块的定长Hashmap
MaxBigDataLen uint64
// 定义多少字节为大数据块
BigDataSize uint32
// 当每个分片中的空闲内存不足时会去总内存申请, 分片每次申请内存大小
ShardPerAllocSize uint64
// 分片数量
Shards uint32
// hash算法
Hasher HashFunc `json:"-"`
}
func DefaultConfig() *Config {
var defaultConfig = &Config{
MemoryType: GO,
Shards: uint32(runtime.NumCPU() * 4),
BigDataSize: 16 * KB,
ShardPerAllocSize: 1 * MB,
}
return defaultConfig
}
func mergeConfig(size int, c *Config) *Config {
config := DefaultConfig()
if c != nil {
config.MemoryKey = c.MemoryKey
if c.MemoryType > 0 {
config.MemoryType = c.MemoryType
}
if c.Shards > 0 {
config.Shards = c.Shards
}
if c.MaxElementLen > 0 {
config.MaxElementLen = c.MaxElementLen
}
if c.BigDataSize > 0 {
config.BigDataSize = c.BigDataSize
}
if c.MaxBigDataLen > 0 {
config.MaxBigDataLen = c.MaxBigDataLen
}
if c.Hasher != nil {
xxHashBytes = c.Hasher
}
}
// Calculate defaults after merging user config (use config.Shards which has default)
if config.MaxElementLen == 0 {
config.MaxElementLen = uint64(size / int(config.Shards))
}
if config.MaxBigDataLen == 0 {
config.MaxBigDataLen = config.MaxElementLen / 20
}
return config
}
func getConfigHash(size int, config *Config) (uint64, error) {
js, err := json.Marshal(config)
if err != nil {
return 0, err
}
js = append(js)
js = binary.LittleEndian.AppendUint32(js, uint32(size))
return xxHashBytes(js), nil
}