Skip to content

Commit

Permalink
track at which timestamp we cached events
Browse files Browse the repository at this point in the history
  • Loading branch information
pablof7z committed Sep 24, 2024
1 parent 98ca0f0 commit a800a71
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
1 change: 1 addition & 0 deletions ndk-cache-dexie/src/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Dexie, { type Table } from "dexie";

export interface Profile extends NDKUserProfile {
pubkey: string;
cachedAt: number;
}

export interface Event {
Expand Down
3 changes: 2 additions & 1 deletion ndk-cache-dexie/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ export default class NDKCacheAdapterDexie implements NDKCacheAdapter {
if (existingValue?.created_at && profile.created_at && existingValue.created_at >= profile.created_at) {
return;
}
this.profiles.set(pubkey, { pubkey, ...profile });
const cachedAt = Math.floor(Date.now() / 1000);
this.profiles.set(pubkey, { pubkey, ...profile, cachedAt });
this.debug("Saved profile for pubkey", pubkey, profile);
}

Expand Down
6 changes: 5 additions & 1 deletion ndk/src/cache/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import type { Hexpubkey, ProfilePointer } from "../user/index.js";
import type { NDKUserProfile } from "../user/profile.js";
import type { NDKLnUrlData } from "../zapper/ln.js";

export type NDKCacheEntry<T> = T & {
cachedAt?: number;
};

export interface NDKCacheAdapter {
/**
* Whether this cache adapter is expected to be fast.
Expand All @@ -31,7 +35,7 @@ export interface NDKCacheAdapter {
/**
* Special purpose
*/
fetchProfile?(pubkey: Hexpubkey): Promise<NDKUserProfile | null>;
fetchProfile?(pubkey: Hexpubkey): Promise<NDKCacheEntry<NDKUserProfile> | null>;
saveProfile?(pubkey: Hexpubkey, profile: NDKUserProfile): void;

/**
Expand Down

0 comments on commit a800a71

Please sign in to comment.