aboutsummaryrefslogtreecommitdiff
path: root/fetchers/cache.go
diff options
context:
space:
mode:
Diffstat (limited to 'fetchers/cache.go')
-rw-r--r--fetchers/cache.go39
1 files changed, 29 insertions, 10 deletions
diff --git a/fetchers/cache.go b/fetchers/cache.go
index 4196792..3802823 100644
--- a/fetchers/cache.go
+++ b/fetchers/cache.go
@@ -12,12 +12,13 @@ import (
)
type Cache struct {
- client *redis.Client
- ctx context.Context
- expiryTime time.Duration
+ client *redis.Client
+ ctx context.Context
+ postExpiry time.Duration
+ profileExpiry time.Duration
}
-func NewCache(addr, password string, expiryMinutes int) (*Cache, error) {
+func NewCache(addr, password string, postExpiryMinutes, profileExpiryMinutes int) (*Cache, error) {
client := redis.NewClient(&redis.Options{
Addr: addr,
Password: password,
@@ -30,9 +31,10 @@ func NewCache(addr, password string, expiryMinutes int) (*Cache, error) {
}
return &Cache{
- client: client,
- ctx: ctx,
- expiryTime: time.Duration(expiryMinutes) * time.Minute,
+ client: client,
+ ctx: ctx,
+ postExpiry: time.Duration(postExpiryMinutes) * time.Minute,
+ profileExpiry: time.Duration(profileExpiryMinutes) * time.Minute,
}, nil
}
@@ -48,6 +50,7 @@ func (c *Cache) Get(uri string) (*notes.Note, bool) {
return nil, false
}
+ c.client.Expire(c.ctx, key, c.postExpiry)
return &note, true
}
@@ -58,7 +61,7 @@ func (c *Cache) Set(uri string, note *notes.Note) {
return
}
- c.client.Set(c.ctx, key, data, c.expiryTime)
+ c.client.Set(c.ctx, key, data, c.postExpiry)
}
func (c *Cache) Delete(uri string) {
@@ -71,7 +74,17 @@ func (c *Cache) DeleteAuthor(did string) {
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 {
@@ -83,6 +96,10 @@ func (c *Cache) GetAuthor(did string) (*notes.Author, bool) {
return nil, false
}
+ if refresh {
+ c.client.Expire(c.ctx, key, c.profileExpiry)
+ }
+
return &author, true
}
@@ -93,7 +110,7 @@ func (c *Cache) SetAuthor(did string, author *notes.Author) {
return
}
- c.client.Set(c.ctx, key, data, c.expiryTime)
+ c.client.Set(c.ctx, key, data, c.profileExpiry)
}
func (c *Cache) GetCursor(did string) (string, bool) {
@@ -120,7 +137,8 @@ func (c *Cache) GetPostsByDID(did string) ([]notes.Note, error) {
iter := c.client.Scan(c.ctx, 0, pattern, 0).Iterator()
for iter.Next(c.ctx) {
- data, err := c.client.Get(c.ctx, iter.Val()).Bytes()
+ key := iter.Val()
+ data, err := c.client.Get(c.ctx, key).Bytes()
if err != nil {
continue
}
@@ -128,6 +146,7 @@ func (c *Cache) GetPostsByDID(did string) ([]notes.Note, error) {
if err := json.Unmarshal(data, &note); err != nil {
continue
}
+ c.client.Expire(c.ctx, key, c.postExpiry)
posts = append(posts, note)
}