-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathg2cache_helper.go
121 lines (107 loc) · 3.16 KB
/
g2cache_helper.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
package g2cache
import (
"bytes"
"errors"
"fmt"
"github.com/google/uuid"
"github.com/mohae/deepcopy"
"reflect"
"strconv"
"sync/atomic"
)
var (
CacheKeyEmpty = errors.New("cache key is empty")
CacheObjNil = errors.New("cache object is nil")
LoadDataSourceFuncNil = errors.New("cache load func is nil")
LocalStorageClose = errors.New("local storage close !!! ")
OutStorageClose = errors.New("out storage close !!! ")
CacheClose = errors.New("g2cache close !!! ")
DataSourceLoadNil = errors.New("data source load nil")
OutStorageLoadNil = errors.New("out storage load nil")
CacheNotImplementPubSub = errors.New("cache not implement pubsub interface")
)
func clone(src, dst interface{}) (err error) {
defer func() {
if e := recover(); e != nil {
err = errors.New(fmt.Sprint(e))
return
}
}()
v := deepcopy.Copy(src)
if reflect.ValueOf(v).IsValid() {
reflect.ValueOf(dst).Elem().Set(reflect.Indirect(reflect.ValueOf(v)))
}
return err
}
func GenKey(args ...interface{}) string {
var buf bytes.Buffer
for i := range args {
addBuf(args[i], &buf)
if i < len(args)-1 {
addBuf(":", &buf)
}
}
return buf.String()
}
func addBuf(i interface{}, buf *bytes.Buffer) {
switch v := i.(type) {
case int:
buf.WriteString(strconv.Itoa(v))
case int8:
buf.WriteString(strconv.Itoa(int(v)))
case int16:
buf.WriteString(strconv.Itoa(int(v)))
case int32:
buf.WriteString(strconv.Itoa(int(v)))
case int64:
buf.WriteString(strconv.Itoa(int(v)))
case uint8:
buf.WriteString(strconv.Itoa(int(v)))
case uint16:
buf.WriteString(strconv.Itoa(int(v)))
case uint32:
buf.WriteString(strconv.Itoa(int(v)))
case uint64:
buf.WriteString(strconv.Itoa(int(v)))
case string:
buf.WriteString(v)
}
}
func wrapFuncErr(f func() error) func() {
return func() {
err := f()
if err != nil {
LogErr(err)
}
}
}
type HitStatistics struct {
HitOutStorageTotalRate float64 `json:"hit_out_storage_total_rate"`
HitDataSourceTotalRate float64 `json:"hit_data_source_total_rate"`
HitLocalStorageTotalRate float64 `json:"hit_local_storage_total_rate"`
HitDataSourceTotal int64 `json:"hit_data_source_total"`
HitLocalStorageTotal int64 `json:"hit_local_storage_total"`
HitOutStorageTotal int64 `json:"hit_out_storage_total"`
AccessGetTotal int64 `json:"access_get_total"`
}
func (h *HitStatistics) String() string {
v, _ := json.MarshalToString(h)
return v
}
func (h *HitStatistics) Calculation() {
h.StatisticsDataSource()
h.StatisticsOutStorage()
h.StatisticsLocalStorage()
}
func (h *HitStatistics) StatisticsDataSource() {
h.HitDataSourceTotalRate = float64(atomic.LoadInt64(&h.HitDataSourceTotal)) / float64(atomic.LoadInt64(&h.AccessGetTotal))
}
func (h *HitStatistics) StatisticsOutStorage() {
h.HitOutStorageTotalRate = float64(atomic.LoadInt64(&h.HitOutStorageTotal)) / float64(atomic.LoadInt64(&h.AccessGetTotal))
}
func (h *HitStatistics) StatisticsLocalStorage() {
h.HitLocalStorageTotalRate = float64(atomic.LoadInt64(&h.HitLocalStorageTotal)) / float64(atomic.LoadInt64(&h.AccessGetTotal))
}
func NewUUID() (string, error) {
return uuid.New().String(), nil
}