-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueueredis.go
78 lines (61 loc) · 1.14 KB
/
queueredis.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package gocache
import (
"context"
"errors"
"github.com/mekramy/gocast"
"github.com/redis/go-redis/v9"
)
type redisQueue struct {
name string
client *redis.Client
}
func (driver redisQueue) Push(value any) error {
return driver.client.LPush(
context.Background(),
driver.name,
value,
).Err()
}
func (driver redisQueue) Pull() (any, error) {
val, err := driver.client.LPop(
context.Background(),
driver.name,
).Result()
if errors.Is(err, redis.Nil) {
return nil, nil
}
if err != nil {
return nil, err
}
return val, nil
}
func (driver redisQueue) Pop() (any, error) {
val, err := driver.client.RPop(
context.Background(),
driver.name,
).Result()
if errors.Is(err, redis.Nil) {
return nil, nil
}
if err != nil {
return nil, err
}
return val, nil
}
func (driver redisQueue) Length() (int64, error) {
val, err := driver.client.LLen(
context.Background(),
driver.name,
).Result()
if errors.Is(err, redis.Nil) {
return 0, nil
}
if err != nil {
return 0, err
}
return val, nil
}
func (driver redisQueue) Cast() (gocast.Caster, error) {
val, err := driver.Pull()
return gocast.NewCaster(val), err
}