-
Notifications
You must be signed in to change notification settings - Fork 0
/
store_redis.go
55 lines (43 loc) · 1.25 KB
/
store_redis.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
// Copyright 2024 Factorial GmbH. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"context"
"encoding/json"
"fmt"
"github.com/redis/go-redis/v9"
)
type RedisStore struct {
conn *redis.Client
}
func (s *RedisStore) SaveRun(ctx context.Context, run *Run) error {
b, _ := json.Marshal(run.SerializableRun)
s.conn.Set(ctx, fmt.Sprintf("%s:static", run.ID), string(b), RunTTL)
return nil
}
// Load loads a Run from Redis.
func (s *RedisStore) LoadRun(ctx context.Context, id string) (*Run, bool) {
var run *Run
reply := s.conn.Get(ctx, fmt.Sprintf("%s:static", id))
if err := reply.Err(); err != nil {
return nil, false
}
json.Unmarshal([]byte(reply.Val()), &run)
return run, true
}
func (s *RedisStore) DeleteRun(ctx context.Context, run string) {
s.conn.Del(
ctx,
fmt.Sprintf("%s:static", run),
fmt.Sprintf("%s:live:seen", run),
)
}
func (s *RedisStore) SawURL(ctx context.Context, run string, url string) {
s.conn.SAdd(ctx, fmt.Sprintf("%s:live:seen", run), url)
}
func (s *RedisStore) HasSeenURL(ctx context.Context, run string, url string) bool {
reply := s.conn.SIsMember(ctx, fmt.Sprintf("%s:live:seen", run), url)
return reply.Val()
}