Skip to content

Commit

Permalink
fix: caching implementation on user profile API (#1053)
Browse files Browse the repository at this point in the history
* fix: caching implementation on user profile API

* fix: cache code review comments
  • Loading branch information
mohammeds1992 authored Jan 31, 2024
1 parent a4747ed commit 5dc0dee
Show file tree
Hide file tree
Showing 5 changed files with 494 additions and 454 deletions.
1 change: 1 addition & 0 deletions packages/restapi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"immer": "^10.0.2",
"joi": "^17.9.2",
"livepeer": "^2.5.8",
"lru-cache": "^10.1.0",
"openpgp": "^5.5.0",
"simple-peer": "^9.11.1",
"socket.io-client": "^4.7.2",
Expand Down
12 changes: 10 additions & 2 deletions packages/restapi/src/lib/pushapi/PushAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import {
import { ALPHA_FEATURE_CONFIG } from '../config';
import { Video } from './video';
import { isValidCAIP10NFTAddress } from '../helpers';
import { LRUCache } from 'lru-cache';
import { cache } from './cache';

export class PushAPI {
private signer?: SignerType;
Expand All @@ -28,6 +30,7 @@ export class PushAPI {
private pgpPublicKey?: string;
private env: ENV;
private progressHook?: (progress: ProgressHookType) => void;
private cache: LRUCache<string, any>;

public chat: Chat; // Public instances to be accessed from outside the class
public video: Video;
Expand Down Expand Up @@ -65,6 +68,9 @@ export class PushAPI {
// Instantiate the notification classes
this.channel = new Channel(this.signer, this.env, this.account);
this.notification = new Notification(this.signer, this.env, this.account);

this.cache = cache;

// Initialize the instances of the four classes
this.chat = new Chat(
this.account,
Expand All @@ -77,6 +83,7 @@ export class PushAPI {
this.profile = new Profile(
this.account,
this.env,
this.cache,
this.decryptedPgpPvtKey,
this.progressHook
);
Expand All @@ -90,12 +97,13 @@ export class PushAPI {
);
this.user = new User(this.account, this.env);

this.video = new Video(this.account,
this.video = new Video(
this.account,
this.env,
this.decryptedPgpPvtKey,
this.signer
);

this.errors = initializationErrors || [];
}
// Overloaded initialize method signatures
Expand Down
13 changes: 13 additions & 0 deletions packages/restapi/src/lib/pushapi/cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { LRUCache } from 'lru-cache';

export const cache = new LRUCache<string, any>({
max: 200,
maxSize: 500 * 1024, // 500KB
sizeCalculation: (value, key) => {
return typeof value === 'string'
? value.length
: new TextEncoder().encode(JSON.stringify(value)).length;
},
ttl: 1000 * 60 * 5, // 5 minutes
allowStale: false,
});
24 changes: 19 additions & 5 deletions packages/restapi/src/lib/pushapi/profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,54 @@ import * as PUSH_USER from '../user';
import { ENV } from '../constants';
import { PushAPI } from './PushAPI';
import { InfoOptions } from './pushAPITypes';
import { LRUCache } from 'lru-cache';

export class Profile {

constructor(
private account: string,
private env: ENV,
private cache: LRUCache<string, any>,
private decryptedPgpPvtKey?: string,
private progressHook?: (progress: ProgressHookType) => void
) {}

async info(options?: InfoOptions) {
const accountToUse = options?.overrideAccount || this.account;
const cacheKey = `profile-${accountToUse}`;

// Check if the profile is already in the cache
if (this.cache.has(cacheKey)) {
return this.cache.get(cacheKey);
}

// If not in cache, fetch from API
const response = await PUSH_USER.get({
account: accountToUse,
env: this.env,
});
// Cache the profile data
this.cache.set(cacheKey, response.profile);
return response.profile;
}

async update(options: { name?: string; desc?: string; picture?: string }) {
if (!this.decryptedPgpPvtKey) {
throw new Error(PushAPI.ensureSignerMessage());
}

const { name, desc, picture } = options;
const response = await PUSH_USER.profile.update({
pgpPrivateKey: this.decryptedPgpPvtKey,
account: this.account,
profile: {
name: name,
desc: desc,
picture: picture,
},
profile: { name, desc, picture },
env: this.env,
progressHook: this.progressHook,
});

const cacheKey = `profile-${this.account}`;
this.cache.delete(cacheKey);

return response.profile;
}
}
Loading

0 comments on commit 5dc0dee

Please sign in to comment.