forked from motiv-labs/janus
/
in_memory.go
104 lines (84 loc) · 2.27 KB
/
in_memory.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
package store
import (
"encoding/json"
"sync"
"github.com/hellofresh/janus/pkg/notifier"
log "github.com/sirupsen/logrus"
)
type noopNotification struct {
topic string
data []byte
}
// InMemoryStore is the redis store.
type InMemoryStore struct {
sync.Mutex
data map[string]string
client chan noopNotification
}
// NewInMemoryStore returns an instance of memory store.
func NewInMemoryStore() *InMemoryStore {
return NewInMemoryStoreWithOptions()
}
// NewInMemoryStoreWithOptions returns an instance of memory store with custom options.
func NewInMemoryStoreWithOptions() *InMemoryStore {
return &InMemoryStore{
data: make(map[string]string),
client: make(chan noopNotification),
}
}
// Exists checks if a key exists in the store
func (s *InMemoryStore) Exists(key string) (bool, error) {
s.Lock()
defer s.Unlock()
return s.exists(key)
}
// Get retreives a value from the store
func (s *InMemoryStore) Get(key string) (string, error) {
s.Lock()
defer s.Unlock()
return s.get(key)
}
// Remove a value from the store
func (s *InMemoryStore) Remove(key string) error {
s.Lock()
defer s.Unlock()
return s.remove(key)
}
// Set a value in the store
func (s *InMemoryStore) Set(key string, value string, expire int64) error {
s.Lock()
defer s.Unlock()
return s.set(key, value, expire)
}
// Publish publishes in memory
func (s *InMemoryStore) Publish(topic string, data []byte) error {
s.client <- noopNotification{topic, data}
return nil
}
// Subscribe subscribes to messages in memory
func (s *InMemoryStore) Subscribe(channel string, callback func(notifier.Notification)) error {
for v := range s.client {
notification := notifier.Notification{}
if marshallErr := json.Unmarshal(v.data, ¬ification); marshallErr != nil {
log.WithError(marshallErr).Error("Unmarshalling message body failed, malformed")
return marshallErr
}
callback(notification)
}
return nil
}
func (s *InMemoryStore) exists(key string) (bool, error) {
_, exists := s.data[key]
return exists, nil
}
func (s *InMemoryStore) remove(key string) error {
delete(s.data, key)
return nil
}
func (s *InMemoryStore) get(key string) (string, error) {
return s.data[key], nil
}
func (s *InMemoryStore) set(key string, value string, expire int64) error {
s.data[key] = value
return nil
}