aboutsummaryrefslogtreecommitdiff
path: root/fetchers/cache.go
blob: 38028236e41c0aabe74b925b20e2afa4f362b91a (plain)
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package fetchers

import (
	"context"
	"encoding/json"
	"fmt"
	"llostre/notes"
	"sort"
	"time"

	"github.com/redis/go-redis/v9"
)

type Cache struct {
	client        *redis.Client
	ctx           context.Context
	postExpiry    time.Duration
	profileExpiry time.Duration
}

func NewCache(addr, password string, postExpiryMinutes, profileExpiryMinutes int) (*Cache, error) {
	client := redis.NewClient(&redis.Options{
		Addr:     addr,
		Password: password,
		DB:       0,
	})

	ctx := context.Background()
	if err := client.Ping(ctx).Err(); err != nil {
		return nil, fmt.Errorf("valkey connection failed: %w", err)
	}

	return &Cache{
		client:        client,
		ctx:           ctx,
		postExpiry:    time.Duration(postExpiryMinutes) * time.Minute,
		profileExpiry: time.Duration(profileExpiryMinutes) * time.Minute,
	}, nil
}

func (c *Cache) Get(uri string) (*notes.Note, bool) {
	key := fmt.Sprintf("post:%s", uri)
	data, err := c.client.Get(c.ctx, key).Bytes()
	if err != nil {
		return nil, false
	}

	var note notes.Note
	if err := json.Unmarshal(data, &note); err != nil {
		return nil, false
	}

	c.client.Expire(c.ctx, key, c.postExpiry)
	return &note, true
}

func (c *Cache) Set(uri string, note *notes.Note) {
	key := fmt.Sprintf("post:%s", uri)
	data, err := json.Marshal(note)
	if err != nil {
		return
	}

	c.client.Set(c.ctx, key, data, c.postExpiry)
}

func (c *Cache) Delete(uri string) {
	key := fmt.Sprintf("post:%s", uri)
	c.client.Del(c.ctx, key)
}

func (c *Cache) DeleteAuthor(did string) {
	key := fmt.Sprintf("profile:%s", did)
	c.client.Del(c.ctx, key)
}

// GetAuthor returns a cached author and refreshes its TTL.
func (c *Cache) GetAuthor(did string) (*notes.Author, bool) {
	return c.getAuthor(did, true)
}

// readAuthor is the same as GetAuthor but doesn't refresh the TTL.
func (c *Cache) readAuthor(did string) (*notes.Author, bool) {
	return c.getAuthor(did, false)
}

func (c *Cache) getAuthor(did string, refresh bool) (*notes.Author, bool) {
	key := fmt.Sprintf("profile:%s", did)
	data, err := c.client.Get(c.ctx, key).Bytes()
	if err != nil {
		return nil, false
	}

	var author notes.Author
	if err := json.Unmarshal(data, &author); err != nil {
		return nil, false
	}

	if refresh {
		c.client.Expire(c.ctx, key, c.profileExpiry)
	}

	return &author, true
}

func (c *Cache) SetAuthor(did string, author *notes.Author) {
	key := fmt.Sprintf("profile:%s", did)
	data, err := json.Marshal(author)
	if err != nil {
		return
	}

	c.client.Set(c.ctx, key, data, c.profileExpiry)
}

func (c *Cache) GetCursor(did string) (string, bool) {
	key := fmt.Sprintf("cursor:%s", did)
	cursor, err := c.client.Get(c.ctx, key).Result()
	if err != nil {
		return "", false
	}
	return cursor, true
}

func (c *Cache) SetCursor(did, cursor string) {
	key := fmt.Sprintf("cursor:%s", did)
	if cursor == "" {
		c.client.Del(c.ctx, key)
		return
	}
	c.client.Set(c.ctx, key, cursor, 24*time.Hour)
}

func (c *Cache) GetPostsByDID(did string) ([]notes.Note, error) {
	pattern := fmt.Sprintf("post:at://%s/*", did)
	var posts []notes.Note

	iter := c.client.Scan(c.ctx, 0, pattern, 0).Iterator()
	for iter.Next(c.ctx) {
		key := iter.Val()
		data, err := c.client.Get(c.ctx, key).Bytes()
		if err != nil {
			continue
		}
		var note notes.Note
		if err := json.Unmarshal(data, &note); err != nil {
			continue
		}
		c.client.Expire(c.ctx, key, c.postExpiry)
		posts = append(posts, note)
	}

	if err := iter.Err(); err != nil {
		return nil, err
	}

	sort.Slice(posts, func(i, j int) bool {
		return posts[i].CreatedAt.After(posts[j].CreatedAt)
	})

	return posts, nil
}

// track oldest sent post per profile
func (c *Cache) GetOldestSent(did string) (time.Time, bool) {
	key := fmt.Sprintf("oldest_sent:%s", did)
	ts, err := c.client.Get(c.ctx, key).Int64()
	if err != nil {
		return time.Time{}, false
	}
	return time.Unix(ts, 0), true
}

func (c *Cache) SetOldestSent(did string, timestamp time.Time) {
	key := fmt.Sprintf("oldest_sent:%s", did)
	c.client.Set(c.ctx, key, timestamp.Unix(), 24*time.Hour)
}

// count posts older than timestamp for a profile
func (c *Cache) CountPostsOlderThan(did string, timestamp time.Time) (int, error) {
	posts, err := c.GetPostsByDID(did)
	if err != nil {
		return 0, err
	}
	for i, p := range posts {
		if p.CreatedAt.Before(timestamp) {
			return len(posts) - i, nil
		}
	}
	return 0, nil
}

func (c *Cache) Close() error {
	return c.client.Close()
}