Reputation: 5767
I'm trying to find a way to prevent a particular key in memcache from being written for a specified period of time, e.g. 5 minutes.
I can imagine doing something like below, but it would require an extra "get" for every "set"
function lock($key,$expiration) {
memcache::set($key,'DONTUSEME', $expiration);
}
function set_key($key,$val) {
if(memcache::get($key) == 'DONETUSEME') {
# no-op
} else {
memcache::set($key,$val);
}
}
function get_key($key) {
$val = memcache::get($key);
if($val == 'DONTUSEME') {
return '';
} else {
return $val;
}
}
Upvotes: 0
Views: 2159
Reputation: 5767
A year later - memcache doesn't seem able to do this.
Redis definitely has locking functinoality, via SETNX. Redis is competitive with Memcache, performance-wise.
http://redis.io/commands/setnx
SETNX key value
Set key to hold string value if key does not exist. In that case, it is equal to SET. When key already holds a value, no operation is performed. SETNX is short for "SET if N ot e X ists.
Upvotes: 0
Reputation: 5343
Memcached does not allow you to lock keys. You could potentially look into Couchbase though since it does have a "get and lock" command and uses the same protocol as memcached. Couchbase is a database, but it contains a memcached caching layer on top. I can provide more details if you want to try this solution. There may also be other caching solutions that do implement a lock command, but I can not think of any off the top of my head.
Upvotes: 1