-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpaging_deleter.go
60 lines (49 loc) · 1.03 KB
/
paging_deleter.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
package redistructs
import (
"context"
"errors"
"github.com/go-redis/redis/v8"
"github.com/octo-5/redistructs/util"
)
type PagingDeleter struct {
ctx context.Context
paging *Paging
ids []string
onlyMeta bool
}
func (d *PagingDeleter) With(ctx context.Context) *PagingDeleter {
d.ctx = ctx
return d
}
func (d *PagingDeleter) OnlyMeta() *PagingDeleter {
d.onlyMeta = true
return d
}
func (d *PagingDeleter) Exec() error {
if len(d.ids) == 0 {
return nil
}
operator := pangingOperator{
ctx: d.ctx,
metaKey: d.paging.metaKey,
}
keys := make([]string, len(d.ids))
for _, id := range d.ids {
keys = append(keys, util.GenerateKey(d.paging.dataPrefix, id))
}
txPipe := d.paging.redis.TxPipeline()
defer txPipe.Close()
if err := operator.deleteMeta(txPipe, keys...); err != nil {
return err
}
if !d.onlyMeta {
if err := operator.deleteData(txPipe, keys...); err != nil {
return err
}
}
_, err := txPipe.Exec(d.ctx)
if err != nil && !errors.Is(err, redis.Nil) {
return err
}
return nil
}