forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachedb.go
99 lines (88 loc) · 2.65 KB
/
cachedb.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
// Copyright 2020 PingCAP, Inc.
//
// 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.
package kv
import (
"context"
"sync"
"github.com/coocood/freecache"
)
type (
cacheDB struct {
mu sync.RWMutex
memTables map[int64]*freecache.Cache
}
// MemManager adds a cache between transaction buffer and the storage to reduce requests to the storage.
// Beware, it uses table ID for partition tables, because the keys are unique for partition tables.
// no matter the physical IDs are the same or not.
MemManager interface {
// UnionGet gets the value from cacheDB first, if it not exists,
// it gets the value from the snapshot, then caches the value in cacheDB.
UnionGet(ctx context.Context, tid int64, snapshot Snapshot, key Key) ([]byte, error)
// Delete releases the cache by tableID.
Delete(tableID int64)
}
)
// Set set the key/value in cacheDB.
func (c *cacheDB) set(tableID int64, key Key, value []byte) error {
c.mu.Lock()
defer c.mu.Unlock()
table, ok := c.memTables[tableID]
if !ok {
table = freecache.NewCache(100 * 1024 * 1024)
c.memTables[tableID] = table
}
return table.Set(key, value, 0)
}
// Get gets the value from cacheDB.
func (c *cacheDB) get(tableID int64, key Key) []byte {
c.mu.RLock()
defer c.mu.RUnlock()
if table, ok := c.memTables[tableID]; ok {
if val, err := table.Get(key); err == nil {
return val
}
}
return nil
}
// UnionGet implements MemManager UnionGet interface.
func (c *cacheDB) UnionGet(ctx context.Context, tid int64, snapshot Snapshot, key Key) (val []byte, err error) {
val = c.get(tid, key)
// key does not exist then get from snapshot and set to cache
if val == nil {
val, err = snapshot.Get(ctx, key)
if err != nil {
return nil, err
}
err = c.set(tid, key, val)
if err != nil {
return nil, err
}
}
return val, nil
}
// Delete delete and reset table from tables in cacheDB by tableID
func (c *cacheDB) Delete(tableID int64) {
c.mu.Lock()
if k, ok := c.memTables[tableID]; ok {
k.Clear()
delete(c.memTables, tableID)
}
c.mu.Unlock()
}
// NewCacheDB news the cacheDB.
func NewCacheDB() MemManager {
mm := new(cacheDB)
mm.memTables = make(map[int64]*freecache.Cache)
return mm
}