From 3de002aca1e0925820305cc16fb1b7c08e73bbaf Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Tue, 11 Feb 2025 14:14:48 -0300 Subject: [PATCH] sdk: only instantiate caches by default when they are necessary. --- sdk/lists_event.go | 9 +++++++++ sdk/lists_profile.go | 13 +++++++++++++ sdk/lists_relay.go | 13 +++++++++++++ sdk/lists_topics.go | 9 +++++++++ sdk/system.go | 27 +++++++++++---------------- 5 files changed, 55 insertions(+), 16 deletions(-) diff --git a/sdk/lists_event.go b/sdk/lists_event.go index 1822cc2..1a18005 100644 --- a/sdk/lists_event.go +++ b/sdk/lists_event.go @@ -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 } diff --git a/sdk/lists_profile.go b/sdk/lists_profile.go index c589871..972fa93 100644 --- a/sdk/lists_profile.go +++ b/sdk/lists_profile.go @@ -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 } diff --git a/sdk/lists_relay.go b/sdk/lists_relay.go index 7b41b0a..47f11be 100644 --- a/sdk/lists_relay.go +++ b/sdk/lists_relay.go @@ -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 } diff --git a/sdk/lists_topics.go b/sdk/lists_topics.go index 15f1889..fd57a85 100644 --- a/sdk/lists_topics.go +++ b/sdk/lists_topics.go @@ -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 } diff --git a/sdk/system.go b/sdk/system.go index 56626d6..34a21cd 100644 --- a/sdk/system.go +++ b/sdk/system.go @@ -65,22 +65,10 @@ 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"), + KVStore: kvstore_memory.NewStore(), + 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"), FallbackRelays: NewRelayStream( "wss://relay.damus.io", "wss://nostr.mom", @@ -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()