Skip to content

Instantly share code, notes, and snippets.

@jackcallister
Created June 23, 2020 22:23
Show Gist options
  • Save jackcallister/3272637ae198854210c4f6ac470f0640 to your computer and use it in GitHub Desktop.
Save jackcallister/3272637ae198854210c4f6ac470f0640 to your computer and use it in GitHub Desktop.
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
}
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