Last active
February 23, 2024 08:10
-
-
Save hemc4/3a888d967ab15c40bbdac05bf1f99752 to your computer and use it in GitHub Desktop.
Create an in memory cache with ttl. Fetch and serve data from this cache
This file contains hidden or 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
class InMemoryCache { | |
constructor() { | |
this._cache = {}; | |
this._timeouts = {}; | |
} | |
set(key, value, time) { | |
if (this._timeouts[key]) { | |
clearTimeout(this._timeouts[key]); | |
delete this._timeouts[key]; | |
} | |
this._cache[key] = value; | |
if (!isNaN(time)) { | |
this._timeouts[key] = setTimeout(() => this.del(key), time); | |
} | |
} | |
get(key) { | |
if (typeof key === 'undefined') { | |
return this._cache; | |
} | |
return this._cache.hasOwnProperty(key) ? this._cache[key] : null; | |
} | |
del(key) { | |
clearTimeout(this._timeouts[key]); | |
delete this._timeouts[key]; | |
if (!(key in this._cache)) { | |
return false; | |
} | |
delete this._cache[key]; | |
return true; | |
} | |
} | |
const myCache = new InMemoryCache(); | |
function getCompanyNameFromCache(userName) { | |
const companyName = myCache.get(userName); | |
// return companyName ? companyName : null; | |
return companyName ? `${companyName} :from cache` : null; | |
} | |
function setCompanyNameInCache(userName, companyName) { | |
// time in ms | |
myCache.set(userName, companyName, 2000); | |
} | |
async function getAllUsersFromAPI() { | |
try { | |
const res = await fetch("https://3dc15088-204f-4ec5-90eb-af7cd840d7ec.mock.pstmn.io/users"); | |
const users = await res.json(); | |
return users; | |
} catch (err) { | |
console.log(err.message); // can be console.error | |
} | |
} | |
async function getUserCompanyName(userName) { | |
const companyNameFromCache = getCompanyNameFromCache(userName); | |
if (companyNameFromCache) { | |
return companyNameFromCache; | |
} | |
const users = await getAllUsersFromAPI(); | |
if (!users) { | |
return null; | |
} | |
const user = users.find((user) => user.username === userName); | |
const companyName = user?.company?.name; | |
setCompanyNameInCache(userName, companyName); | |
return companyName; | |
} | |
async function fetchAndCache() { | |
const userName1 = "Moriah.Stanton"; | |
const userName2 = "Maxime_Nienow"; | |
let companyName = await getUserCompanyName(userName1); // from API | |
console.log(companyName); | |
companyName = await getUserCompanyName(userName1); // from cache | |
console.log(companyName); | |
// wait for 5 seconds | |
await new Promise((resolve) => setTimeout(resolve, 5000)); | |
companyName = await getUserCompanyName(userName1); // from API | |
console.log(companyName); | |
companyName = await getUserCompanyName(userName2); // from API | |
console.log(companyName); | |
} | |
fetchAndCache(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment