Modifying naming and adding cache key generating function

Signed-off-by: Ethan Wellenreiter <ewellenreiter@gmail.com>
This commit is contained in:
Ethan Wellenreiter 2025-05-07 00:11:21 -04:00
parent d8b2bd7226
commit d3f03f2cae
3 changed files with 28 additions and 13 deletions

View File

@ -8,13 +8,18 @@ import (
"github.com/redis/go-redis/v9"
)
type RolesStore struct {
type RoleStore struct {
rdb *redis.Client
}
func (s *RolesStore) Get(ctx context.Context, name string) (*storage.Role, error) {
func (s *RoleStore) generateCacheKey(name string) string {
return name
}
data, err := s.rdb.Get(ctx, name).Result()
func (s *RoleStore) Get(ctx context.Context, name string) (*storage.Role, error) {
cacheKey := s.generateCacheKey(name)
data, err := s.rdb.Get(ctx, cacheKey).Result()
if err == redis.Nil {
return nil, nil
} else if err != nil {
@ -32,16 +37,18 @@ func (s *RolesStore) Get(ctx context.Context, name string) (*storage.Role, error
return &role, nil
}
func (s *RolesStore) Set(ctx context.Context, role *storage.Role) error {
func (s *RoleStore) Set(ctx context.Context, role *storage.Role) error {
cacheKey := s.generateCacheKey(role.Name)
json, err := json.Marshal(role)
if err != nil {
return err
}
return s.rdb.Set(ctx, role.Name, json, RolesExpTime).Err()
return s.rdb.Set(ctx, cacheKey, json, RolesExpTime).Err()
}
func (s *RolesStore) Delete(ctx context.Context, name string) {
s.rdb.Del(ctx, name)
func (s *RoleStore) Delete(ctx context.Context, name string) {
cacheKey := s.generateCacheKey(name)
s.rdb.Del(ctx, cacheKey)
}

View File

@ -13,8 +13,12 @@ type UserStore struct {
rdb *redis.Client
}
func (s *UserStore) generateCacheKey(id int64) string {
return fmt.Sprintf("user-%d", id)
}
func (s *UserStore) Get(ctx context.Context, userID int64) (*storage.User, error) {
cacheKey := fmt.Sprintf("user-%d", userID)
cacheKey := s.generateCacheKey(userID)
data, err := s.rdb.Get(ctx, cacheKey).Result()
if err == redis.Nil {
@ -35,7 +39,7 @@ func (s *UserStore) Get(ctx context.Context, userID int64) (*storage.User, error
}
func (s *UserStore) Set(ctx context.Context, user *storage.User) error {
cacheKey := fmt.Sprintf("user-%d", user.ID)
cacheKey := s.generateCacheKey(user.ID)
json, err := json.Marshal(user)
if err != nil {
@ -46,6 +50,6 @@ func (s *UserStore) Set(ctx context.Context, user *storage.User) error {
}
func (s *UserStore) Delete(ctx context.Context, userID int64) {
cacheKey := fmt.Sprintf("user-%d", userID)
cacheKey := s.generateCacheKey(userID)
s.rdb.Del(ctx, cacheKey)
}

View File

@ -13,8 +13,12 @@ type UserGroupsStore struct {
rdb *redis.Client
}
func (s *UserGroupsStore) generateCacheKey(id int64) string {
return fmt.Sprintf("user-%d", id)
}
func (s *UserGroupsStore) Get(ctx context.Context, userid int64) (*storage.UserGroups, error) {
cacheKey := fmt.Sprintf("user-%d", userid)
cacheKey := s.generateCacheKey(userid)
data, err := s.rdb.Get(ctx, cacheKey).Result()
if err == redis.Nil {
@ -35,7 +39,7 @@ func (s *UserGroupsStore) Get(ctx context.Context, userid int64) (*storage.UserG
}
func (s *UserGroupsStore) Set(ctx context.Context, groups *storage.UserGroups) error {
cacheKey := fmt.Sprintf("user-%d", groups.UserID)
cacheKey := s.generateCacheKey(groups.UserID)
json, err := json.Marshal(groups)
if err != nil {
@ -46,6 +50,6 @@ func (s *UserGroupsStore) Set(ctx context.Context, groups *storage.UserGroups) e
}
func (s *UserGroupsStore) Delete(ctx context.Context, userid int64) {
cacheKey := fmt.Sprintf("user-%d", userid)
cacheKey := s.generateCacheKey(userid)
s.rdb.Del(ctx, cacheKey)
}