Move user caching functionality to dedicated package
This commit is contained in:
86
cache/user.go
vendored
Normal file
86
cache/user.go
vendored
Normal file
@ -0,0 +1,86 @@
|
|||||||
|
package cache
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/fossyy/filekeeper/db"
|
||||||
|
"github.com/fossyy/filekeeper/logger"
|
||||||
|
"github.com/fossyy/filekeeper/utils"
|
||||||
|
"github.com/google/uuid"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type UserWithExpired struct {
|
||||||
|
UserID uuid.UUID
|
||||||
|
Username string
|
||||||
|
Email string
|
||||||
|
Password string
|
||||||
|
AccessAt time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
type UserCache struct {
|
||||||
|
users map[string]*UserWithExpired
|
||||||
|
mu sync.Mutex
|
||||||
|
}
|
||||||
|
|
||||||
|
var log *logger.AggregatedLogger
|
||||||
|
var userCache *UserCache
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
log = logger.Logger()
|
||||||
|
|
||||||
|
userCache = &UserCache{users: make(map[string]*UserWithExpired)}
|
||||||
|
ticker := time.NewTicker(time.Hour * 8)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
<-ticker.C
|
||||||
|
currentTime := time.Now()
|
||||||
|
cacheClean := 0
|
||||||
|
cleanID := utils.GenerateRandomString(10)
|
||||||
|
log.Info(fmt.Sprintf("Cache cleanup [user] [%s] initiated at %02d:%02d:%02d", cleanID, currentTime.Hour(), currentTime.Minute(), currentTime.Second()))
|
||||||
|
|
||||||
|
userCache.mu.Lock()
|
||||||
|
for _, user := range userCache.users {
|
||||||
|
if currentTime.Sub(user.AccessAt) > time.Hour*8 {
|
||||||
|
DeleteUser(user.Email)
|
||||||
|
cacheClean++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
userCache.mu.Unlock()
|
||||||
|
|
||||||
|
log.Info(fmt.Sprintf("Cache cleanup [user] [%s] completed: %d entries removed. Finished at %s", cleanID, cacheClean, time.Since(currentTime)))
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetUser(email string) (*UserWithExpired, error) {
|
||||||
|
userCache.mu.Lock()
|
||||||
|
defer userCache.mu.Unlock()
|
||||||
|
|
||||||
|
if user, ok := userCache.users[email]; ok {
|
||||||
|
return user, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
userData, err := db.DB.GetUser(email)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
userCache.users[email] = &UserWithExpired{
|
||||||
|
UserID: userData.UserID,
|
||||||
|
Username: userData.Username,
|
||||||
|
Email: userData.Email,
|
||||||
|
Password: userData.Password,
|
||||||
|
AccessAt: time.Now(),
|
||||||
|
}
|
||||||
|
|
||||||
|
return userCache.users[email], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func DeleteUser(email string) {
|
||||||
|
userCache.mu.Lock()
|
||||||
|
defer userCache.mu.Unlock()
|
||||||
|
|
||||||
|
delete(userCache.users, email)
|
||||||
|
}
|
@ -1,85 +0,0 @@
|
|||||||
package user
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/fossyy/filekeeper/db"
|
|
||||||
"github.com/fossyy/filekeeper/logger"
|
|
||||||
"github.com/google/uuid"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Cache struct {
|
|
||||||
users map[string]*UserWithExpired
|
|
||||||
mu sync.Mutex
|
|
||||||
}
|
|
||||||
|
|
||||||
type UserWithExpired struct {
|
|
||||||
UserID uuid.UUID
|
|
||||||
Username string
|
|
||||||
Email string
|
|
||||||
Password string
|
|
||||||
AccessAt time.Time
|
|
||||||
}
|
|
||||||
|
|
||||||
var log *logger.AggregatedLogger
|
|
||||||
var UserCache *Cache
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
log = logger.Logger()
|
|
||||||
|
|
||||||
UserCache = &Cache{users: make(map[string]*UserWithExpired)}
|
|
||||||
ticker := time.NewTicker(time.Hour * 8)
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
for {
|
|
||||||
<-ticker.C
|
|
||||||
currentTime := time.Now()
|
|
||||||
cacheClean := 0
|
|
||||||
log.Info(fmt.Sprintf("Cache cleanup initiated at %02d:%02d:%02d", currentTime.Hour(), currentTime.Minute(), currentTime.Second()))
|
|
||||||
|
|
||||||
UserCache.mu.Lock()
|
|
||||||
for _, user := range UserCache.users {
|
|
||||||
if currentTime.Sub(user.AccessAt) > time.Hour*8 {
|
|
||||||
delete(UserCache.users, user.Email)
|
|
||||||
cacheClean++
|
|
||||||
}
|
|
||||||
}
|
|
||||||
UserCache.mu.Unlock()
|
|
||||||
|
|
||||||
log.Info(fmt.Sprintf("Cache cleanup completed: %d entries removed. Finished at %s", cacheClean, time.Since(currentTime)))
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
|
|
||||||
func Get(email string) (*UserWithExpired, error) {
|
|
||||||
UserCache.mu.Lock()
|
|
||||||
defer UserCache.mu.Unlock()
|
|
||||||
|
|
||||||
if user, ok := UserCache.users[email]; ok {
|
|
||||||
return user, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
userData, err := db.DB.GetUser(email)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
UserCache.users[email] = &UserWithExpired{
|
|
||||||
UserID: userData.UserID,
|
|
||||||
Username: userData.Username,
|
|
||||||
Email: userData.Email,
|
|
||||||
Password: userData.Password,
|
|
||||||
AccessAt: time.Now(),
|
|
||||||
}
|
|
||||||
|
|
||||||
return UserCache.users[email], nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func DeleteCache(email string) {
|
|
||||||
UserCache.mu.Lock()
|
|
||||||
defer UserCache.mu.Unlock()
|
|
||||||
|
|
||||||
delete(UserCache.users, email)
|
|
||||||
}
|
|
@ -5,12 +5,13 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/fossyy/filekeeper/cache"
|
||||||
|
"github.com/google/uuid"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/fossyy/filekeeper/db"
|
|
||||||
"github.com/fossyy/filekeeper/email"
|
"github.com/fossyy/filekeeper/email"
|
||||||
"github.com/fossyy/filekeeper/logger"
|
"github.com/fossyy/filekeeper/logger"
|
||||||
"github.com/fossyy/filekeeper/types"
|
"github.com/fossyy/filekeeper/types"
|
||||||
@ -45,11 +46,12 @@ func init() {
|
|||||||
<-ticker.C
|
<-ticker.C
|
||||||
currentTime := time.Now()
|
currentTime := time.Now()
|
||||||
cacheClean := 0
|
cacheClean := 0
|
||||||
log.Info(fmt.Sprintf("Cache cleanup initiated at %02d:%02d:%02d", currentTime.Hour(), currentTime.Minute(), currentTime.Second()))
|
cleanID := utils.GenerateRandomString(10)
|
||||||
|
log.Info(fmt.Sprintf("Cache cleanup [Forgot Password] [%s] initiated at %02d:%02d:%02d", cleanID, currentTime.Hour(), currentTime.Minute(), currentTime.Second()))
|
||||||
|
|
||||||
for _, data := range ListForgotPassword {
|
for _, data := range ListForgotPassword {
|
||||||
data.mu.Lock()
|
data.mu.Lock()
|
||||||
if currentTime.Sub(data.CreateTime) > time.Minute*1 {
|
if currentTime.Sub(data.CreateTime) > time.Minute*10 {
|
||||||
delete(ListForgotPassword, data.User.Email)
|
delete(ListForgotPassword, data.User.Email)
|
||||||
delete(UserForgotPassword, data.Code)
|
delete(UserForgotPassword, data.Code)
|
||||||
cacheClean++
|
cacheClean++
|
||||||
@ -57,7 +59,7 @@ func init() {
|
|||||||
data.mu.Unlock()
|
data.mu.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Info(fmt.Sprintf("Cache cleanup completed: %d entries removed. Finished at %s", cacheClean, time.Since(currentTime)))
|
log.Info(fmt.Sprintf("Cache cleanup [Forgot Password] [%s] completed: %d entries removed. Finished at %s", cleanID, cacheClean, time.Since(currentTime)))
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
@ -85,7 +87,7 @@ func POST(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
emailForm := r.Form.Get("email")
|
emailForm := r.Form.Get("email")
|
||||||
|
|
||||||
user, err := db.DB.GetUser(emailForm)
|
user, err := cache.GetUser(emailForm)
|
||||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
component := forgotPasswordView.Main(fmt.Sprintf("Account with this email address %s is not found", emailForm), types.Message{
|
component := forgotPasswordView.Main(fmt.Sprintf("Account with this email address %s is not found", emailForm), types.Message{
|
||||||
Code: 0,
|
Code: 0,
|
||||||
@ -100,7 +102,14 @@ func POST(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = verifyForgot(user)
|
userData := &models.User{
|
||||||
|
UserID: uuid.UUID{},
|
||||||
|
Username: user.Username,
|
||||||
|
Email: user.Email,
|
||||||
|
Password: "",
|
||||||
|
}
|
||||||
|
|
||||||
|
err = verifyForgot(userData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
log.Error(err.Error())
|
log.Error(err.Error())
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
package forgotPasswordVerifyHandler
|
package forgotPasswordVerifyHandler
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/fossyy/filekeeper/cache"
|
||||||
"github.com/fossyy/filekeeper/db"
|
"github.com/fossyy/filekeeper/db"
|
||||||
"github.com/fossyy/filekeeper/db/model/user"
|
|
||||||
forgotPasswordHandler "github.com/fossyy/filekeeper/handler/forgotPassword"
|
forgotPasswordHandler "github.com/fossyy/filekeeper/handler/forgotPassword"
|
||||||
"github.com/fossyy/filekeeper/logger"
|
"github.com/fossyy/filekeeper/logger"
|
||||||
"github.com/fossyy/filekeeper/session"
|
"github.com/fossyy/filekeeper/session"
|
||||||
@ -98,7 +98,7 @@ func POST(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
session.RemoveAllSessions(data.User.Email)
|
session.RemoveAllSessions(data.User.Email)
|
||||||
|
|
||||||
user.DeleteCache(data.User.Email)
|
cache.DeleteUser(data.User.Email)
|
||||||
|
|
||||||
component := forgotPasswordView.ChangeSuccess("Forgot Password Page")
|
component := forgotPasswordView.ChangeSuccess("Forgot Password Page")
|
||||||
err = component.Render(r.Context(), w)
|
err = component.Render(r.Context(), w)
|
||||||
|
@ -2,10 +2,10 @@ package signinHandler
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"github.com/fossyy/filekeeper/cache"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/fossyy/filekeeper/db/model/user"
|
|
||||||
"github.com/fossyy/filekeeper/logger"
|
"github.com/fossyy/filekeeper/logger"
|
||||||
"github.com/fossyy/filekeeper/session"
|
"github.com/fossyy/filekeeper/session"
|
||||||
"github.com/fossyy/filekeeper/types"
|
"github.com/fossyy/filekeeper/types"
|
||||||
@ -41,7 +41,7 @@ func POST(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
email := r.Form.Get("email")
|
email := r.Form.Get("email")
|
||||||
password := r.Form.Get("password")
|
password := r.Form.Get("password")
|
||||||
userData, err := user.Get(email)
|
userData, err := cache.GetUser(email)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
component := signinView.Main("Sign in Page", types.Message{
|
component := signinView.Main("Sign in Page", types.Message{
|
||||||
Code: 0,
|
Code: 0,
|
||||||
|
@ -45,7 +45,8 @@ func init() {
|
|||||||
<-ticker.C
|
<-ticker.C
|
||||||
currentTime := time.Now()
|
currentTime := time.Now()
|
||||||
cacheClean := 0
|
cacheClean := 0
|
||||||
log.Info(fmt.Sprintf("Cache cleanup initiated at %02d:%02d:%02d", currentTime.Hour(), currentTime.Minute(), currentTime.Second()))
|
cleanID := utils.GenerateRandomString(10)
|
||||||
|
log.Info(fmt.Sprintf("Cache cleanup [signup] [%s] initiated at %02d:%02d:%02d", cleanID, currentTime.Hour(), currentTime.Minute(), currentTime.Second()))
|
||||||
|
|
||||||
for _, data := range VerifyUser {
|
for _, data := range VerifyUser {
|
||||||
data.mu.Lock()
|
data.mu.Lock()
|
||||||
@ -57,7 +58,7 @@ func init() {
|
|||||||
data.mu.Unlock()
|
data.mu.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Info(fmt.Sprintf("Cache cleanup completed: %d entries removed. Finished at %s", cacheClean, time.Since(currentTime)))
|
log.Info(fmt.Sprintf("Cache cleanup [signup] [%s] completed: %d entries removed. Finished at %s", cleanID, cacheClean, time.Since(currentTime)))
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
@ -125,7 +125,7 @@ func Getenv(key string) string {
|
|||||||
|
|
||||||
func GenerateRandomString(length int) string {
|
func GenerateRandomString(length int) string {
|
||||||
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
||||||
seededRand := rand.New(rand.NewSource(time.Now().UnixNano()))
|
seededRand := rand.New(rand.NewSource(time.Now().UnixNano() + int64(rand.Intn(9999))))
|
||||||
var result strings.Builder
|
var result strings.Builder
|
||||||
for i := 0; i < length; i++ {
|
for i := 0; i < length; i++ {
|
||||||
randomIndex := seededRand.Intn(len(charset))
|
randomIndex := seededRand.Intn(len(charset))
|
||||||
|
Reference in New Issue
Block a user