Total Lines | 32 |
Duplicated Lines | 0 % |
Changes | 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 |