Refactor: remove pkg mem cache

This commit is contained in:
Grail Finder
2025-07-03 14:26:52 +03:00
parent 873c35ab08
commit 9e058b04e0
8 changed files with 60 additions and 228 deletions

View File

@ -5,10 +5,8 @@ import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"fmt"
"gralias/models"
"gralias/pkg/cache"
"gralias/utils"
"html/template"
"net/http"
@ -187,32 +185,5 @@ func makeCookie(username string, remote string) (*http.Cookie, error) {
if err := repo.SessionCreate(context.Background(), session); err != nil {
return nil, err
}
// set user in session
if err := cacheSetSession(sessionToken, session); err != nil {
return nil, err
}
return cookie, nil
}
//nolint: unused
func cacheGetSession(key string) (*models.Session, error) {
userSessionB, err := cache.MemCache.Get(key)
if err != nil {
return nil, err
}
var us *models.Session
if err := json.Unmarshal(userSessionB, &us); err != nil {
return nil, err
}
return us, nil
}
func cacheSetSession(key string, session *models.Session) error {
sesb, err := json.Marshal(session)
if err != nil {
return err
}
cache.MemCache.Set(key, sesb)
cache.MemCache.Expire(key, cfg.SessionLifetime)
return nil
}

View File

@ -6,7 +6,6 @@ import (
"crypto/sha256"
"encoding/base64"
"gralias/models"
"gralias/pkg/cache"
"net/http"
)
@ -71,7 +70,8 @@ func GetSession(next http.Handler) http.Handler {
return
}
if userSession.IsExpired() {
cache.MemCache.RemoveKey(sessionToken)
repo.SessionDelete(r.Context(), sessionToken)
// cache.MemCache.RemoveKey(sessionToken)
msg := "session is expired"
log.Debug(msg, "error", err, "token", sessionToken)
next.ServeHTTP(w, r)
@ -81,13 +81,13 @@ func GetSession(next http.Handler) http.Handler {
models.CtxUsernameKey, userSession.Username)
ctx = context.WithValue(ctx,
models.CtxSessionKey, userSession)
if err := cacheSetSession(sessionToken,
userSession); err != nil {
msg := "failed to marshal user session"
log.Warn(msg, "error", err)
next.ServeHTTP(w, r)
return
}
// if err := cacheSetSession(sessionToken,
// userSession); err != nil {
// msg := "failed to marshal user session"
// log.Warn(msg, "error", err)
// next.ServeHTTP(w, r)
// return
// }
next.ServeHTTP(w, r.WithContext(ctx))
})
}