-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfra_component.go
100 lines (82 loc) · 1.84 KB
/
infra_component.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
package cache_proxy_demo
import (
"errors"
"fmt"
"sync"
"time"
"github.com/brianvoe/gofakeit/v6"
)
var ErrNotFound = errors.New("not found")
// cache
type Cache interface {
GetValue(key string, valType any) (val any, err error)
SetValue(key string, val any) error
}
func NewMutexCache() *MutexCache {
return &MutexCache{
data: make(map[string]any),
}
}
type MutexCache struct {
mu sync.Mutex
data map[string]any
}
func (cache *MutexCache) GetValue(key string, valType any) (any, error) {
cache.mu.Lock()
defer cache.mu.Unlock()
if v, ok := cache.data[key]; ok {
return v, nil
}
return valType, ErrNotFound
}
func (cache *MutexCache) SetValue(key string, val any) error {
cache.mu.Lock()
defer cache.mu.Unlock()
cache.data[key] = val
return nil
}
// database
type UserRepository interface {
QueryUserById(id string) (users *gofakeit.PersonInfo, err error)
}
func NewUserDatabase(size int) *UserDatabase {
newUsers := func(size int) map[string]*gofakeit.PersonInfo {
faker := gofakeit.NewCrypto()
gofakeit.SetGlobalFaker(faker)
users := make(map[string]*gofakeit.PersonInfo, size)
for i := 0; i < size; i++ {
id := fmt.Sprintf("id[%v]", i)
users[id] = gofakeit.Person()
}
return users
}
return &UserDatabase{
total: size,
users: newUsers(size),
}
}
type UserDatabase struct {
total int
qryCount int
mu sync.Mutex
users map[string]*gofakeit.PersonInfo
}
func (db *UserDatabase) QueryUserById(id string) (users *gofakeit.PersonInfo, err error) {
db.mu.Lock()
defer db.mu.Unlock()
db.qryCount++
time.Sleep(10 * time.Microsecond)
v, ok := db.users[id]
if !ok {
return nil, ErrNotFound
}
return v, nil
}
func (db *UserDatabase) GetUserIds() []string {
size := len(db.users)
ids := make([]string, 0, size)
for i := 0; i < size; i++ {
ids = append(ids, fmt.Sprintf("id[%v]", i))
}
return ids
}