convert over in memory store

This commit is contained in:
Gregory Wells
2026-06-08 18:29:19 -04:00
parent 09e0683ae0
commit 2a97ec72be
2 changed files with 73 additions and 90 deletions
-90
View File
@@ -1,90 +0,0 @@
package session
// import (
// "sync"
// "time"
// "astraltech.xyz/accountmanager/src/logging"
// "astraltech.xyz/accountmanager/src/worker"
// )
// type MemoryStore struct {
// sessions map[string]*SessionData
// lock sync.RWMutex
// }
// func NewMemoryStore() *MemoryStore {
// logging.Debug("Creating new in memory session store")
// store := &MemoryStore{
// sessions: make(map[string]*SessionData),
// }
// worker.CreateWorker(time.Minute*5, store.cleanup)
// return store
// }
// func (m *MemoryStore) Create(sessionID string, session *SessionData) (err error) {
// hashedSession := hashSession(sessionID)
// m.lock.Lock()
// defer m.lock.Unlock()
// _, exist := m.sessions[hashedSession]
// if exist {
// return ErrSessionAlreadyExists
// }
// m.sessions[hashedSession] = session
// return nil
// }
// func (m *MemoryStore) Get(sessionID string) (*SessionData, error) {
// m.lock.RLock()
// hashed := hashSession(sessionID)
// data, exists := m.sessions[hashed]
// m.lock.RUnlock()
// if exists == false {
// return nil, ErrSessionNotFound
// }
// copy := *data
// return &copy, nil
// }
// func (m *MemoryStore) Update(sessionID string, session *SessionData) error {
// hashedSession := hashSession(sessionID)
// m.lock.Lock()
// defer m.lock.Unlock()
// _, exist := m.sessions[hashedSession]
// if !exist {
// return ErrSessionNotFound
// }
// m.sessions[hashedSession] = session
// return nil
// }
// func (m *MemoryStore) cleanup() {
// logging.Debug("Cleaning up memory store sessions")
// now := time.Now()
// m.lock.Lock()
// defer m.lock.Unlock()
// deleted := 0
// for id, session := range m.sessions {
// if now.After(session.ExpiresAt) {
// delete(m.sessions, id)
// deleted = deleted + 1
// }
// }
// logging.Infof("Cleaned up %d stale sessions", deleted)
// }
// func (m *MemoryStore) Delete(sessionID string) error {
// hashedSession := hashSession(sessionID)
// m.lock.Lock()
// defer m.lock.Unlock()
// _, exist := m.sessions[hashedSession]
// if !exist {
// return ErrSessionNotFound
// }
// delete(m.sessions, hashedSession)
// return nil
// }
+73
View File
@@ -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
}