如何在键/值存储之上实现 incr/decr?

发布于 2024-08-24 22:31:46 字数 169 浏览 6 评论 0原文

如何在键/值存储之上实现 incr/decr?

我正在使用不支持 incr 和 decr 的键值存储,但这就是我想创建它的原因。我使用过 Redis 和 Memcached incr 和 decr,所以正如一些答案中提到的,这是我希望 incr 和 decr 如何表现的完美示例,所以感谢那些提到这一点的人。

How can I implement incr/decr on top of a key/value store?

I'm using a key value store that doesn't support incr and decr though which is why I want to create this. I have used Redis and Memcached incr and decr, so as mentioned in some of the answers then this is a perfect example of how I want the incr and decr to behave, so thanks to those who mentioned this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

旧城空念 2024-08-31 22:31:46

拥有 incr() 函数的要点是它是存储的内部函数。您不必将数据拉出然后将其推回。

您所做的听起来像是您想在代码中添加一些逻辑来拉出数据,递增数据并将其推回...虽然事实并非如此非常难(我想我刚刚描述了你会如何做),它确实在某种程度上违背了这一点。

为了获得好处,您需要更改密钥存储的来源。可能很容易。

但很多缓存已经有这个了。如果你真的需要这个速度,也许你应该找到一个像 memcached 这样支持它的替代存储。

The point of having a incr() function is it's all internal to the store. You don't have to pull data out and push it back in.

What you're doing sounds like you want to put some logic in your code that pulls the data out, increments it and pushes it back in... While it's not very hard (I think I've just described how you'd do it), it does defeat the point somewhat.

To get the benefit you'd need to change the source of your key store. Might be easy.

But a lot of caches already have this. If you really need this for speed, perhaps you should find an alternate store like memcached that does support it.

残疾 2024-08-31 22:31:46

Memcache 具有此功能 内置

编辑:看起来你不会获得原子更新而不更新源,因为似乎没有锁定功能。如果有(这不太好),您可以锁定该值,获取它,在应用程序中递增它,放置它,然后解锁它。但不太理想。

Memcache has this functionality built in

edit: it looks like you're not going to get an atomic update without updating the source, as there doesn't appear to be a lock function. If there is (and this is not pretty), you can lock the value, get it, increment it in your application, put it, and unlock it. Suboptimal though.

无妨# 2024-08-31 22:31:46

看起来好像没有compareAndSet 那么你就不走运了。但从另一个角度考虑问题会有所帮助。例如,如果您正在实现一个显示某个问题的赞成票数的原子计数器,那么一种方法是为每个问题创建一个“表”,并为每个赞成票添加 +1,为每个反对票添加 -1。然后为了“得到”,你需要对“表”求和。为此,我假设“表”很便宜,并且您不关心“get”需要多长时间来计算,您只提到了 incr/decr。

it kind of seems like without a compareAndSet then you are out of luck. But it will help to consider the problem from another angle. For example, if you were implementing an atomic counter that shows the number of upvotes for a question, then one way would be to have a "table" per question and to put a +1 for each upvote and -1 for each downvote. Then to "get" you would sum the "table". For this to work I assume "tables" are inexpensive and you don't care how long "get" takes to compute, you only mentioned incr/decr.

亣腦蒛氧 2024-08-31 22:31:46

如果您希望以原子方式递增或递减与 string 类型的键关联的 int 值,并且如果您在必须执行之前就知道所有键对其中任何一个的原子操作,使用 Dictionary 并为每个键值使用单项数组预填充字典。然后就可以通过 Threading.Interlocked.Increment(MyDict[keyString][0]); 等代码对项目执行原子操作(例如增量)。如果您需要能够处理事先未知的键,则可能需要使用 ConcurrentDictionary 而不是 Dictionary,但如果两个线程尝试同时为同一键创建字典条目。

If you wish to atomically increment or decrement an int value associated with a key of e.g. type string, and if you'll know all of the keys in advance of having to perform the atomic operations on any of them, use Dictionary<string, int[]> and pre-populate the dictionary with a single-item array for each key value. It will then be possible to perform atomic operations (e.g. increment) on items via code like Threading.Interlocked.Increment(MyDict[keyString][0]);. If you need to be able to deal with keys that are not known in advance, you may need to use a ConcurrentDictionary instead of Dictionary, but you need to be careful if two threads try to simultaneously create dictionary entries for the same key.

極樂鬼 2024-08-31 22:31:46

由于递增和递减是简单的“可交换”加法和减法运算,因此您需要实现的是 PN-Counter。它是一种 CRDT(交换复制数据类型)。网络和 Github 上提供了如何在 Riak 上实现此功能的各种示例。

Since increment and decrement are simple addition and subtraction operations that are "commutative", what you need to implement is a PN-Counter. It is a CRDT (commutative replicated data type). Various examples of how to implement this on Riak are available around the web and on Github.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文