-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinmemory.go
61 lines (51 loc) · 1.31 KB
/
inmemory.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
package phada
import (
"errors"
"time"
"github.com/orcaman/concurrent-map"
)
// InMemorySessionStore
type InMemorySessionStore struct {
SessionStore
lastWriteTime time.Time
data cmap.ConcurrentMap
}
// NewInMemorySessionStore
//
// Creates an inmemory store that uses a concurrent map to store sessions
func NewInMemorySessionStore() *InMemorySessionStore {
return &InMemorySessionStore{
lastWriteTime: time.Now(),
data: cmap.New(),
}
}
// PutHop
func (m *InMemorySessionStore) PutHop(ussdRequest *UssdRequestSession) error {
e, ok := m.data.Get(ussdRequest.SessionID)
if !ok {
m.data.Set(ussdRequest.SessionID, ussdRequest)
// return errors.New("Failed to read session data from memory store")
}
if e == nil {
m.data.Set(ussdRequest.SessionID, *ussdRequest)
return nil
}
existing := e.(UssdRequestSession)
existing.RecordHop(ussdRequest.Text)
m.data.Set(ussdRequest.SessionID, existing)
m.lastWriteTime = time.Now()
return nil
}
// Delete
func (m *InMemorySessionStore) Delete(sessionID string) {
m.data.Remove(sessionID)
}
// Get
func (m *InMemorySessionStore) Get(sessionID string) (*UssdRequestSession, error) {
e, ok := m.data.Get(sessionID)
if !ok {
return nil, errors.New("Session does not exist in SessionStore")
}
session := e.(UssdRequestSession)
return &session, nil
}