convert over in memory store
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"astraltech.xyz/accountmanager/src/logging"
|
||||
)
|
||||
|
||||
type MemoryStore[Value any] struct {
|
||||
sessions map[string]Value
|
||||
lock sync.RWMutex
|
||||
}
|
||||
|
||||
func NewMemoryStore[Value any]() *MemoryStore[Value] {
|
||||
logging.Debug("Creating new in memory session store")
|
||||
store := &MemoryStore[Value]{
|
||||
sessions: make(map[string]Value),
|
||||
}
|
||||
return store
|
||||
}
|
||||
|
||||
func (m *MemoryStore[Value]) Create(key string, session Value) (err error) {
|
||||
hashedkey := HashKey(key)
|
||||
|
||||
m.lock.Lock()
|
||||
defer m.lock.Unlock()
|
||||
_, exist := m.sessions[hashedkey]
|
||||
if exist {
|
||||
return ErrKeyAlreadyExists
|
||||
}
|
||||
|
||||
m.sessions[hashedkey] = session
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *MemoryStore[Value]) Get(key string) (Value, error) {
|
||||
var data Value
|
||||
|
||||
m.lock.RLock()
|
||||
hashedkey := HashKey(key)
|
||||
data, exists := m.sessions[hashedkey]
|
||||
m.lock.RUnlock()
|
||||
if exists == false {
|
||||
return data, ErrKeyNotFound
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func (m *MemoryStore[Value]) Update(sessionID string, session Value) error {
|
||||
hashedkey := HashKey(sessionID)
|
||||
|
||||
m.lock.Lock()
|
||||
defer m.lock.Unlock()
|
||||
_, exist := m.sessions[hashedkey]
|
||||
if !exist {
|
||||
return ErrKeyNotFound
|
||||
}
|
||||
m.sessions[hashedkey] = session
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *MemoryStore[Value]) Delete(sessionID string) error {
|
||||
hashedkey := HashKey(sessionID)
|
||||
|
||||
m.lock.Lock()
|
||||
defer m.lock.Unlock()
|
||||
_, exist := m.sessions[hashedkey]
|
||||
if !exist {
|
||||
return ErrKeyNotFound
|
||||
}
|
||||
delete(m.sessions, hashedkey)
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user