добавлено кеширование юзеров
This commit is contained in:
parent
59dc02c3b1
commit
fcbf9b62a2
1
main.go
1
main.go
@ -37,6 +37,7 @@ func main() {
|
|||||||
Jwt: jwtUtil,
|
Jwt: jwtUtil,
|
||||||
Bcrypt: bcryptUtil,
|
Bcrypt: bcryptUtil,
|
||||||
Db: db,
|
Db: db,
|
||||||
|
Cache: src.NewCacheInmem[string, src.UserDTO](60 * 60),
|
||||||
})
|
})
|
||||||
|
|
||||||
r := gin.New()
|
r := gin.New()
|
||||||
|
|||||||
76
src/cache_inmem.go
Normal file
76
src/cache_inmem.go
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
package src
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Cache[K comparable, V any] interface {
|
||||||
|
Get(key K) (V, bool)
|
||||||
|
Set(key K, value V, ttlSeconds int)
|
||||||
|
Del(key K)
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewCacheInmem[K comparable, V any](ttlSeconds int) Cache[K, V] {
|
||||||
|
return &cacheInmem[K, V]{
|
||||||
|
m: &sync.Mutex{},
|
||||||
|
data: map[K]*cacheInmemItem[V]{},
|
||||||
|
ttlSeconds: ttlSeconds,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type cacheInmemItem[T any] struct {
|
||||||
|
Value T
|
||||||
|
Ttl int64
|
||||||
|
Expiration int64
|
||||||
|
}
|
||||||
|
|
||||||
|
type cacheInmem[K comparable, V any] struct {
|
||||||
|
m *sync.Mutex
|
||||||
|
data map[K]*cacheInmemItem[V]
|
||||||
|
ttlSeconds int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *cacheInmem[K, V]) Get(key K) (V, bool) {
|
||||||
|
c.m.Lock()
|
||||||
|
defer c.m.Unlock()
|
||||||
|
|
||||||
|
item, ok := c.data[key]
|
||||||
|
if !ok {
|
||||||
|
var v V
|
||||||
|
return v, false
|
||||||
|
}
|
||||||
|
|
||||||
|
timestamp := time.Now().Unix()
|
||||||
|
if item.Expiration > timestamp {
|
||||||
|
item.Expiration = timestamp + item.Ttl
|
||||||
|
return item.Value, true
|
||||||
|
}
|
||||||
|
|
||||||
|
delete(c.data, key)
|
||||||
|
|
||||||
|
var v V
|
||||||
|
return v, false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *cacheInmem[K, V]) Set(key K, value V, ttlSeconds int) {
|
||||||
|
c.m.Lock()
|
||||||
|
defer c.m.Unlock()
|
||||||
|
|
||||||
|
ttl := int64(c.ttlSeconds)
|
||||||
|
|
||||||
|
expiration := time.Now().Unix() + ttl
|
||||||
|
item := &cacheInmemItem[V]{
|
||||||
|
Value: value,
|
||||||
|
Ttl: ttl,
|
||||||
|
Expiration: expiration,
|
||||||
|
}
|
||||||
|
c.data[key] = item
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *cacheInmem[K, V]) Del(key K) {
|
||||||
|
c.m.Lock()
|
||||||
|
defer c.m.Unlock()
|
||||||
|
|
||||||
|
delete(c.data, key)
|
||||||
|
}
|
||||||
@ -27,6 +27,7 @@ type UserServiceDeps struct {
|
|||||||
Db DB
|
Db DB
|
||||||
Jwt JwtUtil
|
Jwt JwtUtil
|
||||||
Bcrypt BCryptUtil
|
Bcrypt BCryptUtil
|
||||||
|
Cache Cache[string, UserDTO]
|
||||||
}
|
}
|
||||||
|
|
||||||
type userService struct {
|
type userService struct {
|
||||||
@ -64,6 +65,8 @@ func (u *userService) CreateUser(ctx context.Context, params UserCreateParams) (
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
u.deps.Cache.Set(result.Id, *result, -1)
|
||||||
|
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -85,22 +88,39 @@ func (u *userService) AuthenticateUser(ctx context.Context, login, password stri
|
|||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
u.deps.Cache.Set(user.Id, *user, -1)
|
||||||
|
|
||||||
return jwt, nil
|
return jwt, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (u *userService) getUserById(ctx context.Context, userId string) (*UserDTO, error) {
|
||||||
|
if user, ok := u.deps.Cache.Get(userId); ok {
|
||||||
|
return &user, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
user, err := u.deps.Db.GetUserById(ctx, userId)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if user == nil {
|
||||||
|
return nil, ErrUserNotExists
|
||||||
|
}
|
||||||
|
|
||||||
|
u.deps.Cache.Set(user.Id, *user, -1)
|
||||||
|
|
||||||
|
return user, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (u *userService) ValidateToken(ctx context.Context, tokenStr string) (*UserDTO, error) {
|
func (u *userService) ValidateToken(ctx context.Context, tokenStr string) (*UserDTO, error) {
|
||||||
payload, err := u.deps.Jwt.Parse(tokenStr)
|
payload, err := u.deps.Jwt.Parse(tokenStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, ErrUserWrongToken
|
return nil, ErrUserWrongToken
|
||||||
}
|
}
|
||||||
|
|
||||||
user, err := u.deps.Db.GetUserById(ctx, payload.UserId)
|
user, err := u.getUserById(ctx, payload.UserId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if user == nil {
|
|
||||||
return nil, ErrUserNotExists
|
|
||||||
}
|
|
||||||
|
|
||||||
return user, nil
|
return user, nil
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user