-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.go
109 lines (95 loc) · 2.31 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
package db
import "sync"
/*
代理模式
*/
// CacheProxy Db缓存代理
type CacheProxy struct {
db Db
cache sync.Map // key为tableName,value为sync.Map[key: primaryId, value: interface{}]
hit int
miss int
}
func NewCacheProxy(db Db) *CacheProxy {
return &CacheProxy{
db: db,
cache: sync.Map{},
hit: 0,
miss: 0,
}
}
func (c *CacheProxy) Hit() int {
return c.hit
}
func (c *CacheProxy) Miss() int {
return c.miss
}
func (c *CacheProxy) CreateTable(t *Table) error {
if err := c.db.CreateTable(t); err != nil {
return err
}
c.cache.Store(t.Name(), &sync.Map{})
return nil
}
func (c *CacheProxy) CreateTableIfNotExist(t *Table) error {
if _, ok := c.cache.Load(t.Name()); !ok {
c.cache.Store(t.Name(), t)
}
return c.db.CreateTableIfNotExist(t)
}
func (c *CacheProxy) DeleteTable(tableName string) error {
c.cache.Delete(tableName)
return c.db.DeleteTable(tableName)
}
func (c *CacheProxy) Query(tableName string, primaryKey interface{}, result interface{}) error {
cache, ok := c.cache.Load(tableName)
if ok {
if record, ok := cache.(*sync.Map).Load(primaryKey); ok {
c.hit++
result = record
return nil
}
}
c.miss++
if err := c.db.Query(tableName, primaryKey, result); err != nil {
return err
}
cache.(*sync.Map).Store(primaryKey, result)
return nil
}
func (c *CacheProxy) QueryByVisitor(tableName string, visitor TableVisitor) ([]interface{}, error) {
return c.db.QueryByVisitor(tableName, visitor)
}
func (c *CacheProxy) Insert(tableName string, primaryKey interface{}, record interface{}) error {
if err := c.db.Insert(tableName, primaryKey, record); err != nil {
return err
}
cache, ok := c.cache.Load(tableName)
if !ok {
return nil
}
cache.(*sync.Map).Store(primaryKey, record)
return nil
}
func (c *CacheProxy) Update(tableName string, primaryKey interface{}, record interface{}) error {
if err := c.db.Update(tableName, primaryKey, record); err != nil {
return err
}
cache, ok := c.cache.Load(tableName)
if !ok {
return nil
}
cache.(*sync.Map).Store(primaryKey, record)
return nil
}
func (c *CacheProxy) Delete(tableName string, primaryKey interface{}) error {
if err := c.db.Delete(tableName, primaryKey); err != nil {
return err
}
cache, ok := c.cache.Load(tableName)
if !ok {
return nil
}
cache.(*sync.Map).Delete(primaryKey)
return nil
}