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

View File

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

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"github.com/nbd-wtf/go-nostr" "github.com/nbd-wtf/go-nostr"
cache_memory "github.com/nbd-wtf/go-nostr/sdk/cache/memory"
) )
type Relay struct { 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] { 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) ml, _ := fetchGenericList(sys, ctx, pubkey, 10006, kind_10006, parseRelayURL, sys.BlockedRelayListCache)
return ml return ml
} }
func (sys *System) FetchSearchRelayList(ctx context.Context, pubkey string) GenericList[RelayURL] { 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) ml, _ := fetchGenericList(sys, ctx, pubkey, 10007, kind_10007, parseRelayURL, sys.SearchRelayListCache)
return ml return ml
} }
func (sys *System) FetchRelaySets(ctx context.Context, pubkey string) GenericSets[RelayURL] { 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) ml, _ := fetchGenericSets(sys, ctx, pubkey, 30002, kind_30002, parseRelayURL, sys.RelaySetsCache)
return ml return ml
} }

View File

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

View File

@ -66,18 +66,6 @@ func (rs *RelayStream) Next() string {
func NewSystem(mods ...SystemModifier) *System { func NewSystem(mods ...SystemModifier) *System {
sys := &System{ sys := &System{
KVStore: kvstore_memory.NewStore(), 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"), 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"), 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"), MetadataRelays: NewRelayStream("wss://purplepag.es", "wss://user.kindpag.es", "wss://relay.nos.social"),
@ -117,6 +105,13 @@ func NewSystem(mods ...SystemModifier) *System {
mod(sys) 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 { if sys.Store == nil {
sys.Store = &nullstore.NullStore{} sys.Store = &nullstore.NullStore{}
sys.Store.Init() sys.Store.Init()