Commit Graph

6 Commits

Author SHA1 Message Date
bfcc6e27e9 rustfmt
Signed-off-by: William Casarin <jb55@jb55.com>
2024-12-09 09:35:34 -08:00
c67a0b5e06 cache: switch to Instant-based timestamps and improve test coverage
- Replaced `nostr::Timestamp` usage with `Instant` to streamline
  expiration checks.

- Adjusted `CacheEntry::is_expired()` to rely on `Instant::elapsed()`,
  simplifying logic.

- Added comprehensive tests for:
	- Contact lists, mute lists, relay lists retrieval.
	- Handling of expired entries.
	- Proper behavior with empty entries.
	- Re-insertion of events after empty entries.

- Fixed previously failing tests by ensuring expiration logic aligns
  with real-time durations.

Signed-off-by: William Casarin <jb55@jb55.com>
2024-12-09 13:49:08 +09:00
e3c9e2834e 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>
2024-12-09 13:49:08 +09:00
88939ba2d1 Better mute handling
This commit improves the robustness of mute list handling:
1. It listens to new mute lists from the relay interface, and saves them whenever there is a new one
2. It uses the user's own relay lists to fetch events (such as mute lists), helping to ensure we get their mute lists even when they are not stored in the Damus relay
3. It saves events it sees when fetching them from the network, if applicable

Changelog-Changed: Improved robustness of mute handling
Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
2024-12-09 13:49:08 +09:00
9ec713b807 Fix empty cache entry state
There was an issue where an empty cache entry would get interpreted as a
"no cache entry found" situation and cause notepush to always fetch a
new mutelist

Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
2024-09-02 15:32:59 -07:00
71258e3736 Add Nostr event cache
This commit adds a simple in-memory Nostr Event cache, to reduce the
amount of bandwidth used as well as to improve performance.

Testing
-------

Setup:
  - Two iPhone simulators running Damus and on different accounts
  - Damus version: 774da239b92ed630fbf91fce42d9e233661c0d7f
  - Notepush: This commit
  - Push notifications turned on, setup to connect to localhost, and configured to receive DM notifications
  - Run Notepush with `RUST_LOG=DEBUG` env variable for debug logging
Steps:
1. Send a DM from one account to another.
  - Push notification should arrive with a few seconds delay
  - Push notification logs should mention that the event was cached
2. Send a DM again.
  - Push notification should arrive immediately
3. Wait for more than a minute
4. Send a DM again.
  - Push notification should take a few seconds again
  - Push notification logs should mention that the cache item expired and was deleted

Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
Closes: https://github.com/damus-io/notepush/issues/3
2024-08-05 18:01:52 -07:00