admin管理员组文章数量:1332339
I'm developing a project using Nuxt and I need to update the data 15 in 15 seconds, the API is a third party API and has a 100 API calls limit per day. I need to be able to make only one API call every 15 seconds without counting the number of users online, so if I have 10k online users it makes only 1 request.
What I've tried so far.
On my store I have this:
async getData({ mit }) {
await this.$axios
.get('/api/breeds/image/random', {
})
.then(function (response) {
mit('SET_DATA', response.data)
})
},
I created a middleware, and I'm making a call on server side so on Network tab on dev tools don't appear any request call, but unfortunately every time I refresh the page It stills counting as one request, I need some sort of cache.
export default async function ({ store, from }) {
let isInitialPageLoad = !from
if (isInitialPageLoad) {
await store.dispatch('getData')
}
}
and lastly I enabled the proxy on axios on nuxt.config.js
Any idea how to cache and update without making requests based on online users and without refreshing the page?
I'm developing a project using Nuxt and I need to update the data 15 in 15 seconds, the API is a third party API and has a 100 API calls limit per day. I need to be able to make only one API call every 15 seconds without counting the number of users online, so if I have 10k online users it makes only 1 request.
What I've tried so far.
On my store I have this:
async getData({ mit }) {
await this.$axios
.get('/api/breeds/image/random', {
})
.then(function (response) {
mit('SET_DATA', response.data)
})
},
I created a middleware, and I'm making a call on server side so on Network tab on dev tools don't appear any request call, but unfortunately every time I refresh the page It stills counting as one request, I need some sort of cache.
export default async function ({ store, from }) {
let isInitialPageLoad = !from
if (isInitialPageLoad) {
await store.dispatch('getData')
}
}
and lastly I enabled the proxy on axios on nuxt.config.js
Any idea how to cache and update without making requests based on online users and without refreshing the page?
Share Improve this question edited Oct 28, 2023 at 21:43 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Sep 30, 2020 at 9:12 Joao BairradaJoao Bairrada 931 gold badge2 silver badges8 bronze badges4 Answers
Reset to default 3I strongly remend that you use the service workers https://web.dev/service-worker-caching-and-http-caching/ (native browser functions) to handle this kind of caching.
With service workers, especially with a library called workbox, you have the ability to save any calls made by the client, and manage any type of call differently (some examples https://developers.google./web/tools/workbox/modules/workbox-strategies)
To use it on nuxt you must have the pwa module (section for workbox https://pwa.nuxtjs/workbox/)
ok you need to cache your api request on the server just install and use this package nuxt-perfect-cache
npm i nuxt-perfect-cache
// nuxt.config.js
modules: [
[
'nuxt-perfect-cache',
{
disable: false,
appendHost: true,
ignoreConnectionErrors:false, //it's better to be true in production
prefix: 'r-',
url: 'redis://127.0.0.1:6379',
getCacheData(route, context) {
return false//this is for page cache you don't need it
}
}
]
]
this module will enject cool method cacheFetch
so for cache api request
asyncData(ctx) {
return ctx.$cacheFetch({ key: 'myApiKey', expire: 60 * 2 }, () => {
console.log('my callback called*******')
return ctx.$axios.$get('https://jsonplaceholder.typicode./todos/1')
})
}
don't forget to install redis in your machine
I had a similar issue and Redis is ing to help.
I would remend to handle these kind of API calls in server side and inside your frontend just call your local API to feeding users. here is a solution for your problem with async-redis.
// IMPORT
const asyncRedis = require('async-redis')
// INIT CLIENT
const redis_client = asyncRedis.createClient()
redis_client.on('error', (err) => {
console.log('Error ' + err)
})
// API
const GET_RANDOM_IMG_CACHE_TIME = 15
app.post('/get_random_image', async (req, res) => {
const key = 'RANDOM_IMG'
let random_image_data = ''
const value = await redis_client.get(key)
if (value !== null) {
// console.log(key, ' :found in cache');
random_image_data = JSON.parse(value)
} else {
const get_random_image_responce = await this.$axios.get('/api/breeds/image/random', {})
random_image_data = get_random_image_responce.data
await redis_client.setex(key, GET_RANDOM_IMG_CACHE_TIME , JSON.stringify(random_image_data))
}
res.status(200).send(random_image_data)
})
Nuxt 3.8 now supports cache out of the box!
Check: https://github./nuxt/nuxt/issues/15445 And: https://www.youtube./watch?v=aQPR0xn-MMk
本文标签: javascriptNUXT caching API requestsStack Overflow
版权声明:本文标题:javascript - NUXT caching API requests - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742333466a2455157.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论