Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F3512951
D13582.id44848.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
1 KB
Referenced Files
None
Subscribers
None
D13582.id44848.diff
View Options
diff --git a/lib/utils/neynar-client.js b/lib/utils/neynar-client.js
--- a/lib/utils/neynar-client.js
+++ b/lib/utils/neynar-client.js
@@ -63,9 +63,16 @@
class NeynarClient {
apiKey: string;
+ ledChannelsCache: Map<
+ string,
+ { channels: NeynarChannel[], timestamp: number },
+ >;
+ cacheTTL: number;
- constructor(apiKey: string) {
+ constructor(apiKey: string, cacheTTL: number = 60000) {
this.apiKey = apiKey;
+ this.ledChannelsCache = new Map();
+ this.cacheTTL = cacheTTL; // Default TTL of 60 seconds
}
// We're using the term "friend" for a bidirectional follow
@@ -153,11 +160,31 @@
return this.fetchFollowedFarcasterChannelsWithFilter(fid, () => true);
}
- fetchLedFarcasterChannels(fid: string): Promise<NeynarChannel[]> {
- return this.fetchFollowedFarcasterChannelsWithFilter(
+ cleanExpiredCacheEntries() {
+ const now = Date.now();
+ for (const [fid, { timestamp }] of this.ledChannelsCache.entries()) {
+ if (now - timestamp >= this.cacheTTL) {
+ this.ledChannelsCache.delete(fid);
+ }
+ }
+ }
+
+ async fetchLedFarcasterChannels(fid: string): Promise<NeynarChannel[]> {
+ this.cleanExpiredCacheEntries();
+
+ const cachedEntry = this.ledChannelsCache.get(fid);
+ if (cachedEntry && Date.now() - cachedEntry.timestamp < this.cacheTTL) {
+ return cachedEntry.channels;
+ }
+
+ const channels = await this.fetchFollowedFarcasterChannelsWithFilter(
fid,
channel => channel.lead.fid === parseInt(fid),
);
+
+ this.ledChannelsCache.set(fid, { channels, timestamp: Date.now() });
+
+ return channels;
}
async fetchFarcasterChannelByName(
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sun, Dec 22, 9:17 PM (17 h, 49 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2692668
Default Alt Text
D13582.id44848.diff (1 KB)
Attached To
Mode
D13582: [lib] cache led channels response from neynar
Attached
Detach File
Event Timeline
Log In to Comment