mirror of
https://github.com/nostrlabs-io/notepush.git
synced 2025-06-22 05:42:50 +00:00
cache: refactor cache structure and reduce allocations
This commit reworks the cache data structures and operations to significantly cut down on unnecessary allocations and improve overall cache management logic. Key changes include: - Replacing event-specific CacheEntry types with a generic CacheEntry<T> that stores values and timestamps. - Removing the old Arc references in HashMap entries, reducing reference-counted pointer overhead. - Providing helper methods (new(), maybe(), empty(), value()) on CacheEntry for more ergonomic and explicit value handling. - Simplifying the retrieval functions (get_mute_list, get_relay_list, get_contact_list) by using a shared helper (get_cache_entry) that checks for expiration and removes stale entries automatically. - Streamlining add_event and related cache insertion methods to insert the correct CacheEntry variant depending on the event kind. - Removing unnecessary methods (e.g., references_pubkey) and simplifying extensions in nostr_event_extensions.rs. - Enhancing code clarity by separating expiration logic from retrieval and insertion, making the cache easier to maintain, debug, and extend. These improvements should reduce runtime memory overhead and increase code clarity, while maintaining the existing external behavior of the cache. Future changes will be easier to implement, as the new generic CacheEntry abstraction and simplified code paths provide a more maintainable foundation. Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:

committed by
Daniel D’Aquino

parent
88939ba2d1
commit
e3c9e2834e
@ -3,9 +3,6 @@ use nostr_sdk::{EventId, Kind, TagKind};
|
||||
|
||||
/// Temporary scaffolding of old methods that have not been ported to use native Event methods
|
||||
pub trait ExtendedEvent {
|
||||
/// Checks if the note references a given pubkey
|
||||
fn references_pubkey(&self, pubkey: &PublicKey) -> bool;
|
||||
|
||||
/// Retrieves a set of pubkeys referenced by the note
|
||||
fn referenced_pubkeys(&self) -> std::collections::HashSet<nostr::PublicKey>;
|
||||
|
||||
@ -21,11 +18,6 @@ pub trait ExtendedEvent {
|
||||
|
||||
// This is a wrapper around the Event type from strfry-policies, which adds some useful methods
|
||||
impl ExtendedEvent for nostr::Event {
|
||||
/// Checks if the note references a given pubkey
|
||||
fn references_pubkey(&self, pubkey: &PublicKey) -> bool {
|
||||
self.referenced_pubkeys().contains(pubkey)
|
||||
}
|
||||
|
||||
/// Retrieves a set of pubkeys referenced by the note
|
||||
fn referenced_pubkeys(&self) -> std::collections::HashSet<nostr::PublicKey> {
|
||||
self.get_tags_content(SingleLetter(SingleLetterTag::lowercase(Alphabet::P)))
|
||||
@ -149,6 +141,7 @@ impl MaybeConvertibleToRelayList for nostr::Event {
|
||||
let extracted_relay_list_owned = extracted_relay_list.into_iter()
|
||||
.map(|(url, metadata)| (url.clone(), metadata.as_ref().map(|m| m.clone())))
|
||||
.collect();
|
||||
|
||||
Some(extracted_relay_list_owned)
|
||||
}
|
||||
}
|
||||
@ -216,6 +209,7 @@ impl Codable for MuteList {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct TimestampedMuteList {
|
||||
pub mute_list: MuteList,
|
||||
pub timestamp: nostr::Timestamp,
|
||||
|
Reference in New Issue
Block a user