-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
133 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package cache | ||
|
||
import "time" | ||
|
||
type Cache32[V any] interface { | ||
Get(k string) (v V, ok bool) | ||
Delete(k string) | ||
Set(k string, v V) bool | ||
SetWithTTL(k string, v V, d time.Duration) bool | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package cache_memory | ||
|
||
import ( | ||
"encoding/binary" | ||
"encoding/hex" | ||
"time" | ||
|
||
"github.com/dgraph-io/ristretto" | ||
) | ||
|
||
type RistrettoCache[V any] struct { | ||
Cache *ristretto.Cache[string, V] | ||
} | ||
|
||
func New32[V any](max int64) *RistrettoCache[V] { | ||
cache, _ := ristretto.NewCache(&ristretto.Config[string, V]{ | ||
NumCounters: max * 10, | ||
MaxCost: max, | ||
BufferItems: 64, | ||
KeyToHash: func(key string) (uint64, uint64) { return h32(key), 0 }, | ||
}) | ||
return &RistrettoCache[V]{Cache: cache} | ||
} | ||
|
||
func (s RistrettoCache[V]) Get(k string) (v V, ok bool) { return s.Cache.Get(k) } | ||
func (s RistrettoCache[V]) Delete(k string) { s.Cache.Del(k) } | ||
func (s RistrettoCache[V]) Set(k string, v V) bool { return s.Cache.Set(k, v, 1) } | ||
func (s RistrettoCache[V]) SetWithTTL(k string, v V, d time.Duration) bool { | ||
return s.Cache.SetWithTTL(k, v, 1, d) | ||
} | ||
|
||
func h32(key string) uint64 { | ||
// we get an event id or pubkey as hex, | ||
// so just extract the last 8 bytes from it and turn them into a uint64 | ||
return shortUint64(key) | ||
} | ||
|
||
func shortUint64(idOrPubkey string) uint64 { | ||
length := len(idOrPubkey) | ||
if length < 8 { | ||
return 0 | ||
} | ||
b, err := hex.DecodeString(idOrPubkey[length-8:]) | ||
if err != nil { | ||
return 0 | ||
} | ||
return uint64(binary.BigEndian.Uint32(b)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package sdk | ||
|
||
type Follow struct { | ||
Pubkey string | ||
Relay string | ||
Petname string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package sdk | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/nbd-wtf/go-nostr" | ||
"github.com/nbd-wtf/go-nostr/sdk/cache" | ||
) | ||
|
||
type System struct { | ||
relaysCache cache.Cache32[[]Relay] | ||
followsCache cache.Cache32[[]Follow] | ||
metadataCache cache.Cache32[*ProfileMetadata] | ||
pool *nostr.SimplePool | ||
metadataRelays []string | ||
relayListRelays []string | ||
} | ||
|
||
func (sys System) FetchRelaysForPubkey(ctx context.Context, pubkey string) []Relay { | ||
if v, ok := sys.relaysCache.Get(pubkey); ok { | ||
return v | ||
} | ||
|
||
ctx, cancel := context.WithTimeout(ctx, time.Second*5) | ||
defer cancel() | ||
res := FetchRelaysForPubkey(ctx, sys.pool, pubkey, sys.relayListRelays...) | ||
sys.relaysCache.SetWithTTL(pubkey, res, time.Hour*6) | ||
return res | ||
} | ||
|
||
func (sys System) FetchOutboxRelaysForPubkey(ctx context.Context, pubkey string) []string { | ||
relays := sys.FetchRelaysForPubkey(ctx, pubkey) | ||
result := make([]string, 0, len(relays)) | ||
for _, relay := range relays { | ||
if relay.Outbox { | ||
result = append(result, relay.URL) | ||
} | ||
} | ||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters