-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #275 from sunrise-stake/feature/optimisations
Feature/optimisations
- Loading branch information
Showing
67 changed files
with
537 additions
and
1,086 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file was deleted.
Oops, something went wrong.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { TTLCache } from "@brokerloop/ttlcache"; | ||
|
||
const cache = new TTLCache<string, Response>({ | ||
ttl: 3000, | ||
max: 10, | ||
clock: Date, | ||
}); | ||
|
||
const CACHEABLE_RPC_METHODS = [ | ||
"getBalance", | ||
"getAccountInfo", | ||
"getEpochInfo", | ||
"getTokenAccountBalance", | ||
"getTokenSupply", | ||
]; | ||
|
||
const cacheableRequest = (options: RequestInit): boolean => { | ||
const parsedBody = JSON.parse(options.body as string); | ||
return CACHEABLE_RPC_METHODS.includes(parsedBody.method); | ||
}; | ||
|
||
const requestToKey = (url: RequestInfo | URL, options: RequestInit): string => { | ||
const parsedBody = JSON.parse(options.body as string); | ||
delete parsedBody.id; | ||
return `${JSON.stringify(url)}${JSON.stringify(parsedBody)}`; | ||
}; | ||
|
||
export const cachedRPCFetch: typeof fetch = async (url, options) => { | ||
if (options && cacheableRequest(options)) { | ||
const cacheKey = requestToKey(url, options); | ||
const cachedResponse = cache.get(cacheKey); | ||
if (cachedResponse) { | ||
console.log("cache hit"); | ||
return cachedResponse.clone(); | ||
} else { | ||
console.log("cache miss - key: ", cacheKey); | ||
} | ||
} | ||
const response = await fetch(url, options); | ||
if (response.ok && options && cacheableRequest(options)) { | ||
const cacheKey = requestToKey(url, options); | ||
cache.set(cacheKey, response.clone()); | ||
} | ||
return response; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.