"add", "setnx", "insert ignore" feature in CHI

9 views
Skip to first unread message

Ask Bjørn Hansen

unread,
Aug 25, 2010, 1:21:29 AM8/25/10
to Perl-Cache Discuss
Hi,

For a module I'm making I need to only store a value if there's not
already another value stored with that key to avoid a race condition.

Basically I try setting the value and then fetch it again. It's a
rarely set, often read sort of thing (hourly token secrets).

With MySQL / DBI just do an insert (and ignore errors).

With Redix "SETNX".

With memcached "add".

etc etc...

In my module I'm using CHI for caching of these values (in memory and/
or memcached) and then I have some custom code to write to the "real
storage". However, I'm quickly going down the path of re-implementing
too much of CHI (and badly).

So: Would there be any interest in adding an "add" command to CHI?
Obviously only some backends would support it; but that's not
unprecedented.


- ask

--
http://askask.com/

Jonathan Swartz

unread,
Aug 25, 2010, 1:18:02 PM8/25/10
to Ask Bjørn Hansen, Perl-Cache Discuss
Yes, I've often thought we should have extra methods for the backends that can handle atomic operations. That includes Memcached, DBI, and Memory, and FastMmap and BerkeleyDB too if we use locking.

The trick is deciding what methods to support and what to call them. Probably makes sense to stick to memcached's standards ("add", "incr", "decr").

Patches/git forks welcome. :)

Jon

Reply all
Reply to author
Forward
0 new messages