From 53ede6765a1583538b2abc3142d858464b3cf01c Mon Sep 17 00:00:00 2001 From: Guillermo Serrahima Date: Thu, 16 Jul 2026 02:38:46 +0200 Subject: Implement fetched posts count tracking for catching gaps in cached posts --- fetchers/cache.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'fetchers/cache.go') diff --git a/fetchers/cache.go b/fetchers/cache.go index 3802823..b45856d 100644 --- a/fetchers/cache.go +++ b/fetchers/cache.go @@ -161,6 +161,31 @@ func (c *Cache) GetPostsByDID(did string) ([]notes.Note, error) { return posts, nil } +// returns how many posts we expect to have cached for an author +// for detecting gaps caused by uneven TTL expiry +func (c *Cache) GetPostCount(did string) (int, bool) { + key := fmt.Sprintf("post_count:%s", did) + count, err := c.client.Get(c.ctx, key).Int() + if err != nil { + return 0, false + } + return count, true +} + +// resets the expected count. Used after a fresh fetch +func (c *Cache) SetPostCount(did string, count int) { + key := fmt.Sprintf("post_count:%s", did) + c.client.Set(c.ctx, key, count, c.postExpiry) +} + +// increments the expected post count. Used for continuation fetches +// (fetching from a previous cursor) and jetstream events +func (c *Cache) AddPostCount(did string, incr int) { + key := fmt.Sprintf("post_count:%s", did) + c.client.IncrBy(c.ctx, key, int64(incr)) + c.client.Expire(c.ctx, key, c.postExpiry) +} + // track oldest sent post per profile func (c *Cache) GetOldestSent(did string) (time.Time, bool) { key := fmt.Sprintf("oldest_sent:%s", did) -- cgit v1.2.3