Compare commits

...

4 Commits

Author SHA1 Message Date
2b2fa217d6 Implementing usergroups caching
Signed-off-by: Ethan Wellenreiter <ewellenreiter@gmail.com>
2025-05-07 00:01:04 -04:00
064faeadca Adding UserGroups struct to handle the list of groups a user is in
Signed-off-by: Ethan Wellenreiter <ewellenreiter@gmail.com>
2025-05-07 00:00:30 -04:00
a99ef367c7 Implementing roles caching
Signed-off-by: Ethan Wellenreiter <ewellenreiter@gmail.com>
2025-05-06 23:53:09 -04:00
29bf0bec88 Centralized the cache expiration times
Signed-off-by: Ethan Wellenreiter <ewellenreiter@gmail.com>
2025-05-06 23:52:28 -04:00
7 changed files with 112 additions and 11 deletions

View File

@ -34,8 +34,8 @@ type Storage struct {
Delete(ctx context.Context, name string)
}
UserGroups interface {
Get(ctx context.Context, userid int64) (*[]int64, error)
Set(ctx context.Context, groups *[]int64) error
Get(ctx context.Context, userid int64) (*storage.UserGroups, error)
Set(ctx context.Context, groups *storage.UserGroups) error
Delete(ctx context.Context, userid int64)
}
Receipts interface {

View File

@ -0,0 +1,47 @@
package cache
import (
"context"
"encoding/json"
"git.ewellenr.ca/receipt_indexer/backend/internal/storage"
"github.com/redis/go-redis/v9"
)
type RolesStore struct {
rdb *redis.Client
}
func (s *RolesStore) Get(ctx context.Context, name string) (*storage.Role, error) {
data, err := s.rdb.Get(ctx, name).Result()
if err == redis.Nil {
return nil, nil
} else if err != nil {
return nil, err
}
var role storage.Role
if data != "" {
err := json.Unmarshal([]byte(data), &role)
if err != nil {
return nil, err
}
}
return &role, nil
}
func (s *RolesStore) Set(ctx context.Context, role *storage.Role) error {
json, err := json.Marshal(role)
if err != nil {
return err
}
return s.rdb.Set(ctx, role.Name, json, RolesExpTime).Err()
}
func (s *RolesStore) Delete(ctx context.Context, name string) {
s.rdb.Del(ctx, name)
}

View File

@ -4,9 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"time"
// auth_storage "git.ewellenr.ca/receipt_indexer/backend/internal/storage/auth"
"git.ewellenr.ca/receipt_indexer/backend/internal/storage"
"github.com/redis/go-redis/v9"
)
@ -15,8 +13,6 @@ type UserStore struct {
rdb *redis.Client
}
const UserExpTime = time.Minute
func (s *UserStore) Get(ctx context.Context, userID int64) (*storage.User, error) {
cacheKey := fmt.Sprintf("user-%d", userID)

View File

@ -0,0 +1,51 @@
package cache
import (
"context"
"encoding/json"
"fmt"
"git.ewellenr.ca/receipt_indexer/backend/internal/storage"
"github.com/redis/go-redis/v9"
)
type UserGroupsStore struct {
rdb *redis.Client
}
func (s *UserGroupsStore) Get(ctx context.Context, userid int64) (*storage.UserGroups, error) {
cacheKey := fmt.Sprintf("user-%d", userid)
data, err := s.rdb.Get(ctx, cacheKey).Result()
if err == redis.Nil {
return nil, nil
} else if err != nil {
return nil, err
}
var usergroups storage.UserGroups
if data != "" {
err := json.Unmarshal([]byte(data), &usergroups)
if err != nil {
return nil, err
}
}
return &usergroups, nil
}
func (s *UserGroupsStore) Set(ctx context.Context, groups *storage.UserGroups) error {
cacheKey := fmt.Sprintf("user-%d", groups.UserID)
json, err := json.Marshal(groups)
if err != nil {
return err
}
return s.rdb.Set(ctx, cacheKey, json, RolesExpTime).Err()
}
func (s *UserGroupsStore) Delete(ctx context.Context, userid int64) {
cacheKey := fmt.Sprintf("user-%d", userid)
s.rdb.Del(ctx, cacheKey)
}

View File

@ -7,3 +7,8 @@ type Group struct {
Owner int64 `json:"owner"`
Moderators []int64 `json:"moderators"`
}
type UserGroups struct {
UserID int64 `json:"user_id"`
GroupIDs []int64 `json:"group_ids"`
}

View File

@ -48,9 +48,11 @@ func (s *SQLGroupsStore) GetByID(ctx context.Context, id int64) (*Group, error)
return s.getByID(ctx, id)
}
func (s *SQLGroupsStore) GetUserGroups(ctx context.Context, userID int64) ([]int64, error) {
func (s *SQLGroupsStore) GetUserGroups(ctx context.Context, userID int64) (*UserGroups, error) {
// Implement logic to retrieve user's groups from the database
var groups []int64
usergroups := &UserGroups{
UserID: userID,
}
ctx, cancel := context.WithTimeout(ctx, QueryTimeoutDuration)
defer cancel()
@ -68,9 +70,9 @@ func (s *SQLGroupsStore) GetUserGroups(ctx context.Context, userID int64) ([]int
if err != nil {
return nil, err
}
groups = append(groups, groupid)
usergroups.GroupIDs = append(usergroups.GroupIDs, groupid)
}
return groups, nil
return usergroups, nil
}
// func (s *SQLGroupsStore) GetUsersInGroup(ctx context.Context, groupId int64) ([]*User, error) {

View File

@ -66,7 +66,7 @@ type Storage struct {
}
Groups interface {
GetByID(context.Context, int64) (*Group, error)
GetUserGroups(context.Context, int64) ([]int64, error)
GetUserGroups(context.Context, int64) (*UserGroups, error)
// GetUsersInGroup(context.Context, int64) ([]*User, error)
Create(context.Context, *Group) error
Delete(context.Context, int64) error