gpt4 book ai didi

javascript - 跨页面缓存客户端代码结果

转载 作者:行者123 更新时间:2023-12-02 17:46:53 25 4
gpt4 key购买 nike

在我们的应用程序中,我们使用 JavaScript/jQuery 绘制导航组件,并且由于授权,这涉及复杂的逻辑。

几乎所有经过身份验证的页面都需要导航组件,因此每当用户从一个页面导航到另一个页面时,每个页面都会重复复杂的逻辑

我确信在特定条件下,如此复杂的计算结果在一段时间内不会改变,因此我觉得在这种情况下没有必要重新计算。

所以我想在浏览器/客户端存储/缓存结果。我认为解决方案之一是使用结果创建一个cookie

如果这是一个好方法,我需要建议。如果没有,我还能在这里做什么?

最佳答案

<小时/>

如果您可以信赖现代浏览器,HTML 5 网络存储选项是一个不错的选择。

http://www.html5rocks.com/en/features/storage引用上面的内容

There are several reasons to use client-side storage. First, you can make your app work when the user is offline, possibly sync'ing data back once the network is connected again. Second, it's a performance booster; you can show a large corpus of data as soon as the user clicks on to your site, instead of waiting for it to download again. Third, it's an easier programming model, with no server infrastructure required. Of course, the data is more vulnerable and the user can't access it from multiple clients, so you should only use it for non-critical data, in particular cached versions of data that's also "in the cloud". See "Offline": What does it mean and why should I care? for a general discussion of offline technologies, of which client-side storage is one component.

if(typeof(Storage)!=="undefined")
{
// this will store and retrieve key / value for the browser session
sessionStorage.setItem('your_key', 'your_value');
sessionStorage.getItem('your_key');

// this will store and retrieve key / value permanently for the domain
localStorage.setItem('your_key', 'your_value');
localStorage.getItem('your_key');
}

关于javascript - 跨页面缓存客户端代码结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21671687/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com