-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.go
86 lines (74 loc) · 1.36 KB
/
store.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
package main
import (
"context"
"log"
"sync"
"github.com/bjjb/urleen/base62"
"github.com/go-redis/redis/v8"
)
type store interface {
put(string, string) string
get(string) string
}
type redisStore struct {
client *redis.Client
counter, url string
}
func (r *redisStore) open() {
if r.client != nil {
return
}
opts, err := redis.ParseURL(r.url)
if err != nil {
log.Fatal(err)
}
r.client = redis.NewClient(opts)
}
func (r *redisStore) ping() error {
r.open()
ctx := context.Background()
return r.client.Ping(ctx).Err()
}
func (r *redisStore) put(id, s string) string {
r.open()
ctx := context.Background()
if id == "" {
id = base62.Encode(uint64(r.client.Incr(ctx, r.counter).Val()))
}
r.client.Set(ctx, id, s, 0)
return id
}
func (r *redisStore) get(id string) string {
r.open()
ctx := context.Background()
s, _ := r.client.Get(ctx, id).Result()
return s
}
type mapStore struct {
counter uint64
data map[string]string
mutex *sync.Mutex
}
func (m *mapStore) put(id, s string) string {
m.open()
m.mutex.Lock()
if id == "" {
id = base62.Encode(m.counter)
}
m.counter++
m.mutex.Unlock()
m.data[id] = s
return id
}
func (m *mapStore) get(id string) string {
m.open()
return m.data[id]
}
func (m *mapStore) open() {
if m.mutex == nil {
m.mutex = new(sync.Mutex)
}
if m.data == nil {
m.data = make(map[string]string)
}
}