update: updateing caching controle in role repositories
This commit is contained in:
parent
202566117f
commit
dd7fca697d
|
@ -1,7 +1,9 @@
|
|||
package repositories
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/pahmiudahgede/senggoldong/config"
|
||||
"github.com/pahmiudahgede/senggoldong/domain"
|
||||
|
@ -9,18 +11,66 @@ import (
|
|||
|
||||
func GetUserRoleByID(id string) (domain.UserRole, error) {
|
||||
var role domain.UserRole
|
||||
err := config.DB.Where("id = ?", id).First(&role).Error
|
||||
|
||||
ctx := config.Context()
|
||||
cachedRole, err := config.RedisClient.Get(ctx, "userRole:"+id).Result()
|
||||
|
||||
if err == nil {
|
||||
|
||||
err := json.Unmarshal([]byte(cachedRole), &role)
|
||||
if err != nil {
|
||||
return role, errors.New("gagal mendekode data cache Redis")
|
||||
}
|
||||
return role, nil
|
||||
}
|
||||
|
||||
err = config.DB.Where("id = ?", id).First(&role).Error
|
||||
if err != nil {
|
||||
return role, errors.New("userRole tidak ditemukan")
|
||||
}
|
||||
|
||||
roleJSON, err := json.Marshal(role)
|
||||
if err != nil {
|
||||
return role, errors.New("gagal mendekode data untuk Redis")
|
||||
}
|
||||
|
||||
err = config.RedisClient.Set(ctx, "userRole:"+id, roleJSON, time.Hour*24).Err()
|
||||
if err != nil {
|
||||
return role, errors.New("gagal menyimpan data di Redis")
|
||||
}
|
||||
|
||||
return role, nil
|
||||
}
|
||||
|
||||
func GetAllUserRoles() ([]domain.UserRole, error) {
|
||||
var roles []domain.UserRole
|
||||
err := config.DB.Find(&roles).Error
|
||||
|
||||
ctx := config.Context()
|
||||
cachedRoles, err := config.RedisClient.Get(ctx, "allUserRoles").Result()
|
||||
|
||||
if err == nil {
|
||||
|
||||
err := json.Unmarshal([]byte(cachedRoles), &roles)
|
||||
if err != nil {
|
||||
return roles, errors.New("gagal mendekode data cache Redis")
|
||||
}
|
||||
return roles, nil
|
||||
}
|
||||
|
||||
err = config.DB.Find(&roles).Error
|
||||
if err != nil {
|
||||
return nil, errors.New("gagal mengambil data UserRole")
|
||||
}
|
||||
|
||||
rolesJSON, err := json.Marshal(roles)
|
||||
if err != nil {
|
||||
return roles, errors.New("gagal mendekode data untuk Redis")
|
||||
}
|
||||
|
||||
err = config.RedisClient.Set(ctx, "allUserRoles", rolesJSON, time.Hour*24).Err()
|
||||
if err != nil {
|
||||
return roles, errors.New("gagal menyimpan data di Redis")
|
||||
}
|
||||
|
||||
return roles, nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue