在浏览器中的cookie读/写原子 [英] Are cookie read/write atomic in browser

查看:125
本文介绍了在浏览器中的cookie读/写原子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为我的需要实现交叉选项卡互斥。我在此处找到了一个实现。这似乎很有前途。基本上,它实现了 Leslie Lamport的算法,需要创建互斥体的原子读/写。

I am trying to implement a cross tab mutex for my needs. I found a implementation here. which seems quite promising. Basically, it implements Leslie Lamport's algorithm with needs atomic read/writes for creating a mutex.

但是它依赖于localStorage提供原子读/写。这在大多数浏览器(Chrome除外)中运行良好。

However it relies on localStorage providing atomic read/writes. This works well in most browsers except for Chrome.

所以我的问题是,我可以使用cookie读/写吗?

So my question is, can I use cookie read/write instead? Are cookie reads/writes atomic in all mainstream browsers (IE, Chrome, Safari, Firefox)?

推荐答案

Cookie,也不是在所有主流浏览器(IE,Chrome,Safari, code> localStorage 提供原子事务。

Neither cookies, nor localStorage provide atomic transactions.

我认为你可能误解了这篇博文,在Chrome中不起作用,它不依赖于 localStorage 提供原子读/写。他说,Chrome中的正常 localStorage 访问会更加不稳定。我假设这是因为Chrome对每个标签使用单独的进程,而大多数其他浏览器往往对所有标签使用一个进程。他的代码在 localStorage 之上实现一个锁定系统,它应该防止被覆盖的东西。

I think you might have misunderstood that blog post, it doesn't say that his implementation doesn't work in Chrome, it does not rely on localStorage providing atomic read/writes. He says that normal localStorage access is more volatile in Chrome. I'm assuming this is related to the fact that Chrome uses a separate process for each tab, whereas most other browsers tend to use a single process for all tabs. His code implements a locking system on top of localStorage which should protect against things getting overwritten.

另一个解决方案是使用 IndexedDB 。 IndexedDB 确实提供原子事务。作为一个新的标准,它不支持在许多浏览器 localStorage ,但它在Firefox,Chrome和IE10的最近版本中有良好的支持。

Another solution would be to use IndexedDB. IndexedDB does provide atomic transactions. Being a new standard it is not supported in as many browsers as localStorage, but it does have good support in recent versions of Firefox, Chrome and IE10.

这篇关于在浏览器中的cookie读/写原子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆