开发者

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

开发者 https://www.devze.com 2022-12-22 11:31 出处:网络
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

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.


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.


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.


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消