Created
June 23, 2020 22:23
-
-
Save jackcallister/3272637ae198854210c4f6ac470f0640 to your computer and use it in GitHub Desktop.
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
import axios from 'axios' | |
import cache from '../utils/cache' | |
export function get(slug) { | |
const url = `${process.env.GILBERT_URL}/api/v1/content/pages/${slug}` | |
const call = axios.get(url) | |
return cache({ | |
key: slug, | |
exp: 120, | |
callback: call | |
}) | |
} | |
export default { | |
get | |
} |
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
import axios from 'axios' | |
import redis from './redis' | |
const client = redis.createClient() | |
export default function cache({key, exp, callback}) { | |
return new Promise((resolve, reject) => { | |
return client.get(key, (err, data) => { | |
if (data !== null) { | |
resolve(JSON.parse(data)) | |
} else { | |
callback.then((response) => { | |
client.setex(key, exp, JSON.stringify(response.data)) | |
resolve(response.data) | |
}).catch(reject) | |
} | |
}) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment