-
Notifications
You must be signed in to change notification settings - Fork 54
/
cache.go
44 lines (38 loc) · 823 Bytes
/
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
/*
* @Author: seekwe
* @Date: 2019-05-24 19:15:39
* @Last Modified by: seekwe
* @Last Modified time: 2019-05-25 15:29:33
*/
package zcache
import (
"errors"
"sync"
)
var (
// ErrKeyNotFound ErrKeyNotFound
ErrKeyNotFound = errors.New("key is not in cache")
// ErrKeyNotFoundAndNotCallback ErrKeyNotFoundAndNotCallback
ErrKeyNotFoundAndNotCallback = errors.New("key is not in cache and no callback is set")
cache = make(map[string]*Table)
mutex sync.RWMutex
)
// New New
func New(table string) *Table {
mutex.RLock()
t, ok := cache[table]
mutex.RUnlock()
if !ok {
mutex.Lock()
t, ok = cache[table]
if !ok {
t = &Table{
name: table,
items: make(map[interface{}]*CacheItem),
}
cache[table] = t
}
mutex.Unlock()
}
return t
}