简体   繁体   English

React jQuery AJAX全局缓存

[英]React jQuery AJAX global caching

I am developing React application and for frontend AJAX requests I use jQuery, but I want to cache my requests like angular http.get(url, {cache: true }) does. 我正在开发React应用程序,并且我使用jQuery来处理前端AJAX请求,但是我想像角度http.get(url, {cache: true })一样缓存我的请求。 Is there any way which can help me do this global caching for GET requests. 有什么方法可以帮助我对GET请求进行全局缓存。 I tried to add cache: true property to request but it seems not working. 我尝试向请求添加cache: true属性,但似乎无法正常工作。

For example my code looks like this 例如我的代码看起来像这样

$.ajax(source, {
    method: 'GET',
    data: {
        c: count,
        p: period
    },
    cache: true,
    success: (response) => {

    }
})

I have tried also 我也尝试过

$.ajaxSetup({
    cache:true
});

for all requests, but unfortunatley I can see request under Chrome devtools network tab, as well as in my server logs. 对于所有请求,但是很遗憾,我可以在Chrome devtools的“网络”标签下以及服务器日志中看到请求。 So I want to prevent from doing same request if data and url is same. 因此,如果数据和URL相同,我想防止执行相同的请求。 I can create some storage myself, but I think there should be default way for doing this. 我可以自己创建一些存储,但是我认为应该有默认的存储方式。

Thanks in advance! 提前致谢!

One approach could be checking if the last request data are the same than the current. 一种方法可能是检查最后一个请求数据是否与当前相同。

 var lastRequestedData = {}; function myAjaxRequest(requestData) { if (JSON.stringify(requestData) != JSON.stringify(lastRequestedData)) { lastRequestedData = requestData; alert('Makes the ajax request: '+ JSON.stringify(requestData)); //$.ajax(...) } } myAjaxRequest({"c":1,"p":2}); // Fire myAjaxRequest({"c":1,"p":2}); // Not fire myAjaxRequest({"c":2,"p":3}); // Fire 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM