cache relays from nip65.

This commit is contained in:
fiatjaf
2023-07-12 10:53:32 -03:00
parent 512d91e773
commit 4b8f75e375
3 changed files with 40 additions and 23 deletions

View File

@@ -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)
}

View File

@@ -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
}

View File

@@ -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)
}
}