Passed
Pull Request — main (#166)
by Yume
02:23
created

user.NewRedisRepository   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
package user
2
3
import (
4
	"context"
5
	"time"
6
7
	"github.com/memnix/memnix-rest/pkg/utils"
8
	"github.com/redis/go-redis/v9"
9
)
10
11
const defaultExpireTime = 6 * time.Hour
12
13
// RedisRepository is the interface for the redis repository.
14
type RedisRepository struct {
15
	RedisConn *redis.Client // RedisConn is the redis connection.
16
}
17
18
// NewRedisRepository returns a new redis repository.
19
func NewRedisRepository(redisConn *redis.Client) IRedisRepository {
20
	return &RedisRepository{
21
		RedisConn: redisConn,
22
	}
23
}
24
25
// Get gets the user by id.
26
func (r *RedisRepository) Get(ctx context.Context, id uint) (string, error) {
27
	return r.RedisConn.Get(ctx, keyPrefix+utils.ConvertUIntToStr(id)).Result()
28
}
29
30
// Set sets the user by id.
31
func (r *RedisRepository) Set(ctx context.Context, id uint, value string) error {
32
	return r.RedisConn.Set(ctx, keyPrefix+utils.ConvertUIntToStr(id), value, defaultExpireTime).Err()
33
}
34