UPDATE: Change cursor to offset, bc FE dk implement
All checks were successful
Build and Release / release (push) Successful in 1m3s

This commit is contained in:
2026-04-08 13:35:18 +07:00
parent ff478f33b4
commit 82241b432e
17 changed files with 683 additions and 310 deletions

View File

@@ -18,6 +18,7 @@ type MediaRepository interface {
GetByID(ctx context.Context, id pgtype.UUID) (*models.MediaEntity, error)
GetByUserID(ctx context.Context, userId pgtype.UUID) ([]*models.MediaEntity, error)
Search(ctx context.Context, params sqlc.SearchMediasParams) ([]*models.MediaEntity, error)
Count(ctx context.Context, params sqlc.CountMediasParams) (int64, error)
Delete(ctx context.Context, id pgtype.UUID) error
Create(ctx context.Context, params sqlc.CreateMediaParams) (*models.MediaEntity, error)
}
@@ -85,6 +86,7 @@ func (r *mediaRepository) GetByID(ctx context.Context, id pgtype.UUID) (*models.
var media models.MediaEntity
err := r.c.Get(ctx, cacheId, &media)
if err == nil {
_ = r.c.Set(ctx, cacheId, media, constants.NormalCacheDuration)
return &media, nil
}
@@ -118,11 +120,10 @@ func (r *mediaRepository) Create(ctx context.Context, params sqlc.CreateMediaPar
go func() {
bgCtx := context.Background()
_ = r.c.DelByPattern(bgCtx, "media:target*")
_ = r.c.DelByPattern(bgCtx, "media:userId:*")
_ = r.c.DelByPattern(bgCtx, "media:search*")
_ = r.c.DelByPattern(bgCtx, "media:count*")
}()
media := models.MediaEntity{
ID: convert.UUIDToString(row.ID),
UserID: convert.UUIDToString(row.UserID),
@@ -155,7 +156,16 @@ func (r *mediaRepository) Search(ctx context.Context, params sqlc.SearchMediasPa
queryKey := r.generateQueryKey("media:search", params)
var cachedIDs []string
if err := r.c.Get(ctx, queryKey, &cachedIDs); err == nil && len(cachedIDs) > 0 {
return r.getByIDsWithFallback(ctx, cachedIDs)
listItem, err := r.getByIDsWithFallback(ctx, cachedIDs)
if err != nil {
return nil, err
}
newCachedIDs := make([]string, len(listItem))
for i, media := range listItem {
newCachedIDs[i] = media.ID
}
_ = r.c.Set(ctx, queryKey, newCachedIDs, constants.ListCacheDuration)
return listItem, err
}
rows, err := r.q.SearchMedias(ctx, params)
@@ -195,11 +205,35 @@ func (r *mediaRepository) Search(ctx context.Context, params sqlc.SearchMediasPa
return medias, nil
}
func (r *mediaRepository) Count(ctx context.Context, params sqlc.CountMediasParams) (int64, error) {
queryKey := r.generateQueryKey("media:count", params)
var count int64
if err := r.c.Get(ctx, queryKey, &count); err == nil {
_ = r.c.Set(ctx, queryKey, count, constants.ListCacheDuration)
return count, nil
}
count, err := r.q.CountMedias(ctx, params)
if err != nil {
return 0, err
}
_ = r.c.Set(ctx, queryKey, count, constants.ListCacheDuration)
return count, nil
}
func (r *mediaRepository) GetByUserID(ctx context.Context, userId pgtype.UUID) ([]*models.MediaEntity, error) {
queryKey := fmt.Sprintf("media:userId:%s", convert.UUIDToString(userId))
var cachedIDs []string
if err := r.c.Get(ctx, queryKey, &cachedIDs); err == nil && len(cachedIDs) > 0 {
return r.getByIDsWithFallback(ctx, cachedIDs)
listItem, err := r.getByIDsWithFallback(ctx, cachedIDs)
if err != nil {
return nil, err
}
newCachedIDs := make([]string, len(listItem))
for i, media := range listItem {
newCachedIDs[i] = media.ID
}
_ = r.c.Set(ctx, queryKey, newCachedIDs, constants.ListCacheDuration)
return listItem, nil
}
rows, err := r.q.GetMediasByUserID(ctx, userId)

View File

@@ -97,6 +97,7 @@ func (r *roleRepository) GetByID(ctx context.Context, id pgtype.UUID) (*models.R
var role models.RoleEntity
err := r.c.Get(ctx, cacheId, &role)
if err == nil {
_ = r.c.Set(ctx, cacheId, role, constants.NormalCacheDuration)
return &role, nil
}
@@ -122,6 +123,7 @@ func (r *roleRepository) GetByname(ctx context.Context, name string) (*models.Ro
var role models.RoleEntity
err := r.c.Get(ctx, cacheId, &role)
if err == nil {
_ = r.c.Set(ctx, cacheId, role, constants.NormalCacheDuration)
return &role, nil
}
row, err := r.q.GetRoleByName(ctx, name)
@@ -146,6 +148,11 @@ func (r *roleRepository) Create(ctx context.Context, name string) (*models.RoleE
if err != nil {
return nil, err
}
go func() {
bgCtx := context.Background()
_ = r.c.DelByPattern(bgCtx, "role:all*")
}()
role := models.RoleEntity{
ID: convert.UUIDToString(row.ID),
Name: row.Name,
@@ -183,24 +190,52 @@ func (r *roleRepository) Update(ctx context.Context, params sqlc.UpdateRoleParam
}
func (r *roleRepository) All(ctx context.Context) ([]*models.RoleEntity, error) {
queryKey := "role:all"
var cachedIDs []string
if err := r.c.Get(ctx, queryKey, &cachedIDs); err == nil && len(cachedIDs) > 0 {
listItem, err := r.getByIDsWithFallback(ctx, cachedIDs)
if err != nil {
return nil, err
}
newCachedIDs := make([]string, len(listItem))
for i, media := range listItem {
newCachedIDs[i] = media.ID
}
_ = r.c.Set(ctx, queryKey, newCachedIDs, constants.ListCacheDuration)
return listItem, err
}
rows, err := r.q.GetRoles(ctx)
if err != nil {
return nil, err
}
var roles []*models.RoleEntity
var ids []string
roleToCache := make(map[string]any)
var users []*models.RoleEntity
for _, row := range rows {
user := &models.RoleEntity{
role := &models.RoleEntity{
ID: convert.UUIDToString(row.ID),
Name: row.Name,
IsDeleted: row.IsDeleted,
CreatedAt: convert.TimeToPtr(row.CreatedAt),
UpdatedAt: convert.TimeToPtr(row.UpdatedAt),
}
users = append(users, user)
ids = append(ids, role.ID)
roles = append(roles, role)
roleToCache[fmt.Sprintf("role:id:%s", role.ID)] = role
}
return users, nil
if len(roleToCache) > 0 {
_ = r.c.MSet(ctx, roleToCache, constants.NormalCacheDuration)
}
if len(ids) > 0 {
_ = r.c.Set(ctx, queryKey, ids, constants.ListCacheDuration)
}
return roles, nil
}
func (r *roleRepository) Delete(ctx context.Context, id pgtype.UUID) error {

View File

@@ -20,6 +20,7 @@ type UserRepository interface {
GetByIDWithoutDeleted(ctx context.Context, id pgtype.UUID) (*models.UserEntity, error)
GetByEmail(ctx context.Context, email string) (*models.UserEntity, error)
Search(ctx context.Context, params sqlc.SearchUsersParams) ([]*models.UserEntity, error)
Count(ctx context.Context, params sqlc.CountUsersParams) (int64, error)
UpsertUser(ctx context.Context, params sqlc.UpsertUserParams) (*models.UserEntity, error)
CreateProfile(ctx context.Context, params sqlc.CreateUserProfileParams) (*models.UserProfileSimple, error)
UpdateProfile(ctx context.Context, params sqlc.UpdateUserProfileParams) (*models.UserEntity, error)
@@ -205,9 +206,8 @@ func (r *userRepository) UpsertUser(ctx context.Context, params sqlc.UpsertUserP
}
go func() {
bgCtx := context.Background()
_ = r.c.DelByPattern(bgCtx, "user:all*")
_ = r.c.DelByPattern(bgCtx, "user:search*")
_ = r.c.DelByPattern(bgCtx, "user:count*")
}()
return &models.UserEntity{
@@ -320,6 +320,22 @@ func (r *userRepository) Search(ctx context.Context, params sqlc.SearchUsersPara
return users, nil
}
func (r *userRepository) Count(ctx context.Context, params sqlc.CountUsersParams) (int64, error) {
queryKey := r.generateQueryKey("user:count", params)
var count int64
if err := r.c.Get(ctx, queryKey, &count); err == nil {
return count, nil
}
count, err := r.q.CountUsers(ctx, params)
if err != nil {
return 0, err
}
_ = r.c.Set(ctx, queryKey, count, constants.NormalCacheDuration)
return count, nil
}
func (r *userRepository) Delete(ctx context.Context, id pgtype.UUID) error {
user, err := r.GetByID(ctx, id)
if err != nil {