fully convert redis over to custom key value store type

This commit is contained in:
Gregory Wells
2026-06-08 18:24:59 -04:00
parent f6016bbdb1
commit 09e0683ae0
5 changed files with 94 additions and 85 deletions
+76 -76
View File
@@ -1,90 +1,90 @@
package session
import (
"sync"
"time"
// import (
// "sync"
// "time"
"astraltech.xyz/accountmanager/src/logging"
"astraltech.xyz/accountmanager/src/worker"
)
// "astraltech.xyz/accountmanager/src/logging"
// "astraltech.xyz/accountmanager/src/worker"
// )
type MemoryStore struct {
sessions map[string]*SessionData
lock sync.RWMutex
}
// 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 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)
// 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.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.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
}
// 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()
// func (m *MemoryStore) cleanup() {
// logging.Debug("Cleaning up memory store sessions")
// now := time.Now()
m.lock.Lock()
defer m.lock.Unlock()
// 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)
}
// 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)
// 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
}
// m.lock.Lock()
// defer m.lock.Unlock()
// _, exist := m.sessions[hashedSession]
// if !exist {
// return ErrSessionNotFound
// }
// delete(m.sessions, hashedSession)
// return nil
// }