Skip to content

Commit 987bca8

Browse files
author
v_llxjliu
committed
开启缓存时候加读写锁
1 parent 7fbf064 commit 987bca8

File tree

4 files changed

+130
-10
lines changed

4 files changed

+130
-10
lines changed

filter/eq_json.go

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package filter
2+
3+
import (
4+
"encoding/json"
5+
"reflect"
6+
)
7+
8+
func equalJSON(jsonStr1, jsonStr2 string) (bool, error) {
9+
var i interface{}
10+
var i2 interface{}
11+
err := json.Unmarshal([]byte(jsonStr1), &i)
12+
if err != nil {
13+
return false, err
14+
}
15+
err = json.Unmarshal([]byte(jsonStr2), &i2)
16+
if err != nil {
17+
return false, err
18+
}
19+
return reflect.DeepEqual(i, i2), nil
20+
}
21+
22+
// EqualJSON 判断两个或者多个json字符串是否等价(有相同的键值,不同的顺序)
23+
func EqualJSON(jsonStr1, jsonStr2 string, moreJson ...string) (bool, error) {
24+
if len(moreJson) == 0 {
25+
return equalJSON(jsonStr1, jsonStr2)
26+
}
27+
equal, err := equalJSON(jsonStr1, jsonStr2)
28+
if err != nil {
29+
return false, err
30+
}
31+
if !equal {
32+
return false, nil
33+
}
34+
for _, js := range moreJson {
35+
eq, err := equalJSON(jsonStr1, js)
36+
if err != nil {
37+
return false, err
38+
}
39+
if !eq {
40+
return false, err
41+
}
42+
}
43+
return true, nil
44+
45+
}

filter/parser.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,12 @@ func getOmitTag(scene string, pkgInfo string, i int, typeOf reflect.Type) tagInf
104104
}
105105
fieldName := typeOf.Field(i).Name
106106
cacheKey := tagCache.key(pkgInfo, scene, fieldName, false)
107-
tagEl, exist := tagCache.fields[cacheKey]
107+
//tagEl, exist := tagCache.fields[cacheKey]
108+
tagEl, exist := tagCache.GetField(cacheKey)
108109
if !exist { //如果缓存里没取到
109110
omitTag = getFieldOmitTag(typeOf.Field(i), scene)
110-
tagCache.fields[cacheKey] = omitTag.tag
111+
//tagCache.fields[cacheKey] = omitTag.tag
112+
tagCache.SetField(cacheKey, omitTag.tag)
111113
return omitTag
112114
}
113115
omitTag.tag = tagEl
@@ -124,10 +126,11 @@ func getSelectTag(scene string, pkgInfo string, i int, typeOf reflect.Type) tagI
124126

125127
fieldName := typeOf.Field(i).Name
126128
cacheKey := tagCache.key(pkgInfo, scene, fieldName, true)
127-
tagEl, exist := tagCache.fields[cacheKey]
129+
//tagEl, exist := tagCache.fields[cacheKey]
130+
tagEl, exist := tagCache.GetField(cacheKey)
128131
if !exist { //如果缓存里没取到
129132
selectTag = getFieldSelectTag(typeOf.Field(i), scene)
130-
tagCache.fields[cacheKey] = selectTag.tag
133+
tagCache.SetField(cacheKey, selectTag.tag)
131134
return selectTag
132135
}
133136
selectTag.tag = tagEl

filter/tag_cache.go

+23-4
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,36 @@
11
package filter
22

3-
var tagCache cache
3+
import "sync"
4+
5+
var tagCache = cache{
6+
fields: make(map[string]tag),
7+
lock: &sync.RWMutex{},
8+
}
49

510
var enableCache = true
611

7-
func init() {
8-
tagCache.fields = make(map[string]tag)
9-
}
12+
//func init() {
13+
// tagCache.fields = make(map[string]tag)
14+
//}
1015

1116
type cache struct {
17+
lock *sync.RWMutex
1218
fields map[string]tag
1319
}
1420

21+
func (c *cache) GetField(key string) (tag, bool) {
22+
c.lock.RLock()
23+
v, ok := c.fields[key]
24+
c.lock.RUnlock()
25+
return v, ok
26+
}
27+
28+
func (c *cache) SetField(key string, tagEl tag) {
29+
c.lock.Lock()
30+
c.fields[key] = tagEl
31+
c.lock.Unlock()
32+
}
33+
1534
func (c *cache) key(pkgInfo string, scene string, fieldName string, isSelect bool) string {
1635
s := ""
1736
if !isSelect {

test/main.go

+55-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"fmt"
66
"github.com/liu-cn/json-filter/filter"
7+
"time"
78
)
89

910
func mustJson(v interface{}) string {
@@ -39,8 +40,60 @@ func (u *Us) GetAvatar2() string {
3940
return string(u.Avatar[:]) + ".jpg"
4041
}
4142

43+
func goOmit() {
44+
type (
45+
Tag struct {
46+
Icon string `json:"icon,omit(article)"`
47+
Name string `json:"name,omit(profile)"`
48+
}
49+
Articles struct {
50+
Password int `json:"password,omit($any)"` //$any表示任何场景都排除该字段
51+
Tags []Tag `json:"tags"`
52+
Hot int `json:"hot,select(img),func(GetHot)"` //热度 过滤时会调用GetHot方法获取该字段的值
53+
Like int `json:"-"`
54+
Collect int `json:"-"`
55+
}
56+
)
57+
58+
articles := Articles{Like: 100, Collect: 20, Tags: []Tag{{"icon", "foo"}, {"icon", "bar"}}}
59+
60+
for i := 0; i < 100; i++ {
61+
go func() {
62+
fmt.Println(filter.Omit("article", &articles)) //尽量传指针,不传指针func选择器中的用指针接收的方法无法被调用
63+
}()
64+
}
65+
66+
}
67+
func goSelect() {
68+
type (
69+
Tag struct {
70+
Icon string `json:"icon,omit(article)"`
71+
Name string `json:"name,omit(profile)"`
72+
}
73+
Articles struct {
74+
Password int `json:"password,omit($any)"` //$any表示任何场景都排除该字段
75+
Tags []Tag `json:"tags"`
76+
Hot int `json:"hot,select(img),func(GetHot)"` //热度 过滤时会调用GetHot方法获取该字段的值
77+
Like int `json:"-"`
78+
Collect int `json:"-"`
79+
}
80+
)
81+
82+
articles := Articles{Like: 100, Collect: 20, Tags: []Tag{{"icon", "foo"}, {"icon", "bar"}}}
83+
84+
for i := 0; i < 10000; i++ {
85+
go func() {
86+
fmt.Println(filter.Select("article", &articles)) //尽量传指针,不传指针func选择器中的用指针接收的方法无法被调用
87+
}()
88+
}
89+
90+
}
91+
4292
func main() {
4393

94+
//goOmit()
95+
goSelect()
96+
time.Sleep(time.Second * 10)
4497
//var bb = []byte(`{"a":"1"}`)
4598
//u := Us{
4699
// BB: [3]byte{1, 2, 4},
@@ -55,8 +108,8 @@ func main() {
55108
//fmt.Println(filter.Omit("1", &list))
56109
//fmt.Println(mustJson(u))
57110

58-
TestMap()
59-
TestMap()
111+
//TestMap()
112+
//TestMap()
60113
//for i := 0; i < 3; i++ {
61114
// ExampleOmit()
62115
//}

0 commit comments

Comments
 (0)