sdk: only instantiate caches by default when they are necessary.

This commit is contained in:
fiatjaf 2025-02-11 14:14:48 -03:00
parent 7189cd7e02
commit 3de002aca1
5 changed files with 55 additions and 16 deletions

View File

@ -6,6 +6,7 @@ import (
"strings"
"github.com/nbd-wtf/go-nostr"
cache_memory "github.com/nbd-wtf/go-nostr/sdk/cache/memory"
)
type EventRef struct{ nostr.Pointer }
@ -13,11 +14,19 @@ type EventRef struct{ nostr.Pointer }
func (e EventRef) Value() string { return e.Pointer.AsTagReference() }
func (sys *System) FetchBookmarkList(ctx context.Context, pubkey string) GenericList[EventRef] {
if sys.BookmarkListCache == nil {
sys.BookmarkListCache = cache_memory.New32[GenericList[EventRef]](1000)
}
ml, _ := fetchGenericList(sys, ctx, pubkey, 10003, kind_10003, parseEventRef, sys.BookmarkListCache)
return ml
}
func (sys *System) FetchPinList(ctx context.Context, pubkey string) GenericList[EventRef] {
if sys.PinListCache == nil {
sys.PinListCache = cache_memory.New32[GenericList[EventRef]](1000)
}
ml, _ := fetchGenericList(sys, ctx, pubkey, 10001, kind_10001, parseEventRef, sys.PinListCache)
return ml
}

View File

@ -6,6 +6,7 @@ import (
"strings"
"github.com/nbd-wtf/go-nostr"
cache_memory "github.com/nbd-wtf/go-nostr/sdk/cache/memory"
)
type ProfileRef struct {
@ -17,16 +18,28 @@ type ProfileRef struct {
func (f ProfileRef) Value() string { return f.Pubkey }
func (sys *System) FetchFollowList(ctx context.Context, pubkey string) GenericList[ProfileRef] {
if sys.FollowListCache == nil {
sys.FollowListCache = cache_memory.New32[GenericList[ProfileRef]](1000)
}
fl, _ := fetchGenericList(sys, ctx, pubkey, 3, kind_3, parseProfileRef, sys.FollowListCache)
return fl
}
func (sys *System) FetchMuteList(ctx context.Context, pubkey string) GenericList[ProfileRef] {
if sys.MuteListCache == nil {
sys.MuteListCache = cache_memory.New32[GenericList[ProfileRef]](1000)
}
ml, _ := fetchGenericList(sys, ctx, pubkey, 10000, kind_10000, parseProfileRef, sys.MuteListCache)
return ml
}
func (sys *System) FetchFollowSets(ctx context.Context, pubkey string) GenericSets[ProfileRef] {
if sys.FollowSetsCache == nil {
sys.FollowSetsCache = cache_memory.New32[GenericSets[ProfileRef]](1000)
}
ml, _ := fetchGenericSets(sys, ctx, pubkey, 30000, kind_30000, parseProfileRef, sys.FollowSetsCache)
return ml
}

View File

@ -4,6 +4,7 @@ import (
"context"
"github.com/nbd-wtf/go-nostr"
cache_memory "github.com/nbd-wtf/go-nostr/sdk/cache/memory"
)
type Relay struct {
@ -24,16 +25,28 @@ func (sys *System) FetchRelayList(ctx context.Context, pubkey string) GenericLis
}
func (sys *System) FetchBlockedRelayList(ctx context.Context, pubkey string) GenericList[RelayURL] {
if sys.BlockedRelayListCache == nil {
sys.BlockedRelayListCache = cache_memory.New32[GenericList[RelayURL]](1000)
}
ml, _ := fetchGenericList(sys, ctx, pubkey, 10006, kind_10006, parseRelayURL, sys.BlockedRelayListCache)
return ml
}
func (sys *System) FetchSearchRelayList(ctx context.Context, pubkey string) GenericList[RelayURL] {
if sys.SearchRelayListCache == nil {
sys.SearchRelayListCache = cache_memory.New32[GenericList[RelayURL]](1000)
}
ml, _ := fetchGenericList(sys, ctx, pubkey, 10007, kind_10007, parseRelayURL, sys.SearchRelayListCache)
return ml
}
func (sys *System) FetchRelaySets(ctx context.Context, pubkey string) GenericSets[RelayURL] {
if sys.RelaySetsCache == nil {
sys.RelaySetsCache = cache_memory.New32[GenericSets[RelayURL]](1000)
}
ml, _ := fetchGenericSets(sys, ctx, pubkey, 30002, kind_30002, parseRelayURL, sys.RelaySetsCache)
return ml
}

View File

@ -4,6 +4,7 @@ import (
"context"
"github.com/nbd-wtf/go-nostr"
cache_memory "github.com/nbd-wtf/go-nostr/sdk/cache/memory"
)
type Topic string
@ -11,11 +12,19 @@ type Topic string
func (r Topic) Value() string { return string(r) }
func (sys *System) FetchTopicList(ctx context.Context, pubkey string) GenericList[Topic] {
if sys.TopicListCache == nil {
sys.TopicListCache = cache_memory.New32[GenericList[Topic]](1000)
}
ml, _ := fetchGenericList(sys, ctx, pubkey, 10015, kind_10015, parseTopicString, sys.TopicListCache)
return ml
}
func (sys *System) FetchTopicSets(ctx context.Context, pubkey string) GenericSets[Topic] {
if sys.TopicSetsCache == nil {
sys.TopicSetsCache = cache_memory.New32[GenericSets[Topic]](1000)
}
ml, _ := fetchGenericSets(sys, ctx, pubkey, 30015, kind_30015, parseTopicString, sys.TopicSetsCache)
return ml
}

View File

@ -66,18 +66,6 @@ func (rs *RelayStream) Next() string {
func NewSystem(mods ...SystemModifier) *System {
sys := &System{
KVStore: kvstore_memory.NewStore(),
MetadataCache: cache_memory.New32[ProfileMetadata](8000),
RelayListCache: cache_memory.New32[GenericList[Relay]](8000),
FollowListCache: cache_memory.New32[GenericList[ProfileRef]](1000),
MuteListCache: cache_memory.New32[GenericList[ProfileRef]](1000),
BookmarkListCache: cache_memory.New32[GenericList[EventRef]](1000),
PinListCache: cache_memory.New32[GenericList[EventRef]](1000),
BlockedRelayListCache: cache_memory.New32[GenericList[RelayURL]](1000),
SearchRelayListCache: cache_memory.New32[GenericList[RelayURL]](1000),
TopicListCache: cache_memory.New32[GenericList[Topic]](1000),
RelaySetsCache: cache_memory.New32[GenericSets[RelayURL]](1000),
FollowSetsCache: cache_memory.New32[GenericSets[ProfileRef]](1000),
TopicSetsCache: cache_memory.New32[GenericSets[Topic]](1000),
RelayListRelays: NewRelayStream("wss://purplepag.es", "wss://user.kindpag.es", "wss://relay.nos.social"),
FollowListRelays: NewRelayStream("wss://purplepag.es", "wss://user.kindpag.es", "wss://relay.nos.social"),
MetadataRelays: NewRelayStream("wss://purplepag.es", "wss://user.kindpag.es", "wss://relay.nos.social"),
@ -117,6 +105,13 @@ func NewSystem(mods ...SystemModifier) *System {
mod(sys)
}
if sys.MetadataCache == nil {
sys.MetadataCache = cache_memory.New32[ProfileMetadata](8000)
}
if sys.RelayListCache == nil {
sys.RelayListCache = cache_memory.New32[GenericList[Relay]](8000)
}
if sys.Store == nil {
sys.Store = &nullstore.NullStore{}
sys.Store.Init()