-
Notifications
You must be signed in to change notification settings - Fork 4
/
cache.go
146 lines (126 loc) · 3.64 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
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright 2020 Ye Zi Jie. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Author: FishGoddess
// Email: fishgoddess@qq.com
// Created at 2020/03/14 16:28:56
package cachego
import (
"time"
)
const (
// defaultMapSize determines the initialized map size of one segment.
defaultMapSize = 1024
// defaultSegmentSize determines the size of segments.
// This value will affect the performance of concurrency.
defaultSegmentSize = 1024
)
// Cache is a struct of cache.
type Cache struct {
// mapSize is the size of map inside.
mapSize int
// segmentSize is the size of segments.
// This value will affect the performance of concurrency.
segmentSize int
// segments is a slice stores the real data.
segments []*segment
}
// NewCache returns a new Cache holder for use.
func NewCache() *Cache {
return &Cache{
mapSize: defaultMapSize,
segmentSize: defaultSegmentSize,
segments: newSegments(defaultMapSize, defaultSegmentSize),
}
}
// newSegments returns a slice of initialized segments.
func newSegments(mapSize int, segmentSize int) []*segment {
segments := make([]*segment, segmentSize)
for i := 0; i < segmentSize; i++ {
segments[i] = newSegment(mapSize)
}
return segments
}
// index returns a position in segments of this key.
func index(key string) int {
index := 0
keyBytes := []byte(key)
for _, b := range keyBytes {
index = (index << 5) - index + int(b&0xff)
}
return index
}
// segmentOf returns the segment of this key.
func (c *Cache) segmentOf(key string) *segment {
return c.segments[index(key)&(c.segmentSize-1)]
}
// Get returns the value of key and a false if not found.
func (c *Cache) Get(key string) (interface{}, bool) {
return c.segmentOf(key).get(key)
}
// Set sets key and value to Cache.
// The key will not expire.
func (c *Cache) Set(key string, value interface{}) {
c.SetWithTTL(key, value, NeverDie)
}
// SetWithTTL sets key and value to Cache with a ttl.
func (c *Cache) SetWithTTL(key string, value interface{}, ttl int64) {
c.segmentOf(key).set(key, value, ttl)
}
// Remove removes the value of key.
// If this key is not existed, nothing will happen.
func (c *Cache) Remove(key string) {
c.segmentOf(key).remove(key)
}
// RemoveAll removes all keys in Cache.
// Notice that this method is weak-consistency.
func (c *Cache) RemoveAll() {
for _, segment := range c.segments {
segment.removeAll()
}
}
// Size returns the size of Cache.
// Notice that this method is weak-consistency.
func (c *Cache) Size() int {
size := 0
for _, segment := range c.segments {
size += segment.size()
}
return size
}
// Gc removes dead entries in Cache.
// Notice that this method is weak-consistency and
// it doesn't guarantee 100% removed.
func (c *Cache) Gc() {
for _, segment := range c.segments {
segment.gc()
}
}
// AutoGc starts a goroutine to execute Gc() at fixed duration.
// It returns a <-chan type which can be used to stop this goroutine.
func (c *Cache) AutoGc(duration time.Duration) chan<- bool {
quitChan := make(chan bool)
go func() {
ticker := time.NewTicker(duration)
for {
select {
case <-ticker.C:
c.Gc()
case <-quitChan:
return
}
}
}()
return quitChan
}