diff --git a/cache.go b/cache.go index 4050281..f6c7d4f 100644 --- a/cache.go +++ b/cache.go @@ -127,6 +127,15 @@ func (c *Cache) Get(key string) ([]byte, bool) { return val, true } +func (c *Cache) GetJSON(key string, recv any) bool { + b, ok := c.Get(key) + if !ok { + return ok + } + json.Unmarshal(b, recv) + return true +} + func (c *Cache) Set(key string, value []byte) { err := c.DB.Update(func(txn *badger.Txn) error { return txn.Set([]byte(key), value) @@ -136,6 +145,11 @@ func (c *Cache) Set(key string, value []byte) { } } +func (c *Cache) SetJSON(key string, value any) { + j, _ := json.Marshal(value) + c.Set(key, j) +} + func (c *Cache) SetWithTTL(key string, value []byte, ttl time.Duration) { err := c.DB.Update(func(txn *badger.Txn) error { return txn.Set([]byte(key), value) @@ -146,3 +160,8 @@ func (c *Cache) SetWithTTL(key string, value []byte, ttl time.Duration) { c.expiringKeys[key] = time.Now().Add(ttl) c.refreshTimers <- struct{}{} } + +func (c *Cache) SetJSONWithTTL(key string, value any, ttl time.Duration) { + j, _ := json.Marshal(value) + c.SetWithTTL(key, j, ttl) +} diff --git a/nostr.go b/nostr.go index 47acc00..dd795c9 100644 --- a/nostr.go +++ b/nostr.go @@ -100,14 +100,8 @@ func getEvent(ctx context.Context, code string) (*nostr.Event, error) { if author != "" { // fetch relays for author - ctx, cancel := context.WithTimeout(ctx, time.Millisecond*1500) - defer cancel() - - for _, relay := range sdk.FetchRelaysForPubkey(ctx, pool, author, relays...) { - if relay.Outbox { - relays = append(relays, relay.URL) - } - } + authorRelays := relaysForPubkey(ctx, author, relays...) + relays = append(relays, authorRelays...) } for len(relays) < 5 { @@ -136,17 +130,9 @@ func getLastNotes(ctx context.Context, code string) []*nostr.Event { if pp == nil { return nil } - relays := pp.Relays - { - ctx, cancel := context.WithTimeout(ctx, time.Millisecond*1500) - defer cancel() - for _, relay := range sdk.FetchRelaysForPubkey(ctx, pool, pp.PublicKey, pp.Relays...) { - if relay.Outbox { - relays = append(relays, relay.URL) - } - } - } + pubkeyRelays := relaysForPubkey(ctx, pp.PublicKey, pp.Relays...) + relays := append(pp.Relays, pubkeyRelays...) ctx, cancel := context.WithTimeout(ctx, time.Second*4) defer cancel() @@ -166,3 +152,18 @@ func getLastNotes(ctx context.Context, code string) []*nostr.Event { } return lastNotes } + +func relaysForPubkey(ctx context.Context, pubkey string, extraRelays ...string) []string { + pubkeyRelays := make([]string, 12) + if ok := cache.GetJSON("io:"+pubkey, &pubkeyRelays); !ok { + ctx, cancel := context.WithTimeout(ctx, time.Millisecond*1500) + for _, relay := range sdk.FetchRelaysForPubkey(ctx, pool, pubkey, extraRelays...) { + if relay.Outbox { + pubkeyRelays = append(pubkeyRelays, relay.URL) + } + } + cancel() + cache.SetJSONWithTTL("io:"+pubkey, pubkeyRelays, time.Hour*24*7) + } + return pubkeyRelays +} diff --git a/render.go b/render.go index 76c4ada..a28cfa2 100644 --- a/render.go +++ b/render.go @@ -71,15 +71,12 @@ func render(w http.ResponseWriter, r *http.Request) { key := "ln:" + event.PubKey var lastNotes []*nostr.Event - if b, ok := cache.Get(key); ok { - json.Unmarshal(b, &lastNotes) - } else { + if ok := cache.GetJSON(key, &lastNotes); !ok { ctx, cancel := context.WithTimeout(r.Context(), time.Second*4) lastNotes = getLastNotes(ctx, code) cancel() if !s.DisableCache { - b, _ := json.Marshal(lastNotes) - cache.SetWithTTL(key, b, time.Hour*24) + cache.SetJSONWithTTL(key, lastNotes, time.Hour*24) } }