把数据保存到cook_将数据保存在cookie中以节省数据库的访问量(saving data in cookies to save trips to database)...

将数据保存在cookie中以节省数据库的访问量(saving data in cookies to save trips to database)

我有一个网页,一旦页面加载就使用getJSON查询数据库,以填充下拉框。 然后,当用户从该初始框中选择一个选项时,将进行第二次getJSON调用以使用相关数据填充第二个下拉列表。 为了保存对数据库的调用(特别是如果用户在第一个组合框中保持切换值),我正在考虑为cookie中的下拉菜单保存数据/但是我想做一些事情。 cookie仅适用于当前会话...因为从技术上讲,数据库中的数据可能会发生变化。 它不会经常改变......但它可以改变。

我找到了以下帖子:

我正在考虑尝试。

我正在考虑实现的逻辑是这样的:

一个。 在准备好文件时,检查cookie是否存在。 如果没有,请查询db并获取数据。 创建cookie b。 当会话被杀死时,杀死cookie。

我的问题如下:

这是正确的方法吗?

如何修改上面的stackoverflow帖子中的示例,以便cookie只对当前会话有用?

仅供参考。 目标浏览器是IE 9。

谢谢。

编辑1:

编辑2:

我想我也会查看sessionstorage。

I have a web page that queries a database using getJSON as soon as the page is loaded, in order to populate a drop down box. Then when the user selects an option from this initial box, a second getJSON call is made to populate the second drop down with data that is relevant. In an attempt to save calls to the database (especially if the user keeps toggling values in the first combo box), I'm thinking of saving the data for the drop down menus in cookies/ But I'd like to do something where the cookie is only good for the current session... because technically, it is possible that the data in the database will change. It won't change often... but it can change.

I found the following post:

which I'm thinking of trying out.

The logic I'm thinking of implementing would be something like this:

a. on document ready, check if cookie exists. If not, query db and get data. Create cookie b. when session is killed, kill cookies.

My questions are as follows:

Is this the right approach?

how can I modify the example in the above stackoverflow post so that the cookies are only good for the current session?

FYI. The target browser is IE 9.

Thanks.

EDIT 1:

I found this post for creating session cookies with jquery: create session based cookie, jquery I'll give that a try.

EDIT 2:

I think I'll also check out sessionstorage while i'm at it.

原文:https://stackoverflow.com/questions/17111656

更新时间:2019-12-19 00:42

最满意答案

绝对是一个有趣的方法。

使用此cookie插件,如果您未指定expire ,您的cookie将仅保留会话的长度。 所以你可以简单地做:

$.cookie("basket-data", JSON.stringify($("#ArticlesHolder").data()));

它只会留在本次会议中。

只是为了记录 - 会话存储肯定是比cookie更好的选择。

Definitely an interesting approach.

With this cookies plugin, if you don't specify the expire, your cookie will only stay for the length of the session. So you can simply do:

$.cookie("basket-data", JSON.stringify($("#ArticlesHolder").data()));

And it will stay for this session only.

Just for the record - session storage is definitely a better choice than cookies.

2013-06-14

相关问答

我将查看您的数据库的计划备份以及代码的版本控制 I'd look into a scheduled backup for your db, and version control for your code

我建议你通过继承瓶子默认值来实现你自己的Session和SessionInterface。 基本上,你需要定义你自己的会话类和会话接口类。 class MyDatabaseSession(CallbackDict, SessionMixin):

def __init__(self, initial=None, sid=None):

CallbackDict.__init__(self, initial)

self.sid = sid

sel

...

绝对是一个有趣的方法。 使用此cookie插件,如果您未指定expire ,您的cookie将仅保留会话的长度。 所以你可以简单地做: $.cookie("basket-data", JSON.stringify($("#ArticlesHolder").data()));

它只会留在本次会议中。 只是为了记录 - 会话存储肯定是比cookie更好的选择。 Definitely an interesting approach. With this cookies plugin, if you d

...

您正在设置一些其他元素属性,而不是设置图像的属性 $("#own").click(function(e) {

e.preventDefault();

var own = $("input#img-link").val();

$(".backstretch img").attr("src", own);

$.cookie("backstretch-img", own);

});

var own = $.cookie("backstretch-img");

if (

...

该字符串是PHP serialize()函数的结果。 Serialize从Array或Object创建一个字符串。 要在PHP中将Array恢复为可用状态,请在其上使用unserialize() 所以 $new_array = unserialize($the_table_column_name);

$name = $new_array['name'];

That string is the result of a PHP serialize() function. Serialize crea

...

很多时候,Django在缓存数据库结果方面做得不错。 如果你想拥有更多的控制权,你可以做这样的事情(前提是你没有太多的类型) class Point(models.Model):

components = models.ArrayField(models.IntegerField(), default=[])

def save_components(self, geocode):

_components = []

_types = {t.type:

...

您不需要对设置文件进行所有中间件更改。 我刚刚尝试设置一个简单的代码演示,它完美无缺。 首先,尝试将代码最小化为仅设置'实验'的Cookie。 你可以在这里试试这个演示。 一个视图可能是设置cookie的主视图的索引。 在另一个视图上可能是另一个应用程序的索引视图测试cookie。 From the docs you can only test using another page request I quote

*test_cookie_worked()* "Returns either

...

您需要继承QNetworkCookieJar,并且在该类中您应该实现自己的持久存储。 class MyNetworkCookieJar : public QNetworkCookieJar {

public:

bool saveCookiesToDisk() {

// .. my implementation

return true; // if i did

}

bool loadCookiesFromDisk() {

// .. load from disk

return false;

...

您选择的部分内容取决于您对可以访问保存数据的用户的关注程度以及游戏的真正含义。 json或xml是很好的保存格式,您可以将所有信息转储到以后轻松读取。 但是,如果您将其保存在本地计算机上,则用户显然可以访问它。 同样在本地存储东西意味着你不依赖网络任何总是好的东西,除非你的游戏已经依赖于网络。 如果您有任何在线游戏,在大多数情况下,本地保存数据显然是不好的。 反对它真的取决于你的游戏在做什么。 最后,在使用本地保存的运行中获得一些东西要快得多。 由于代理或任何妨碍他们获取保存数据的内容,用户不必担

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值