Is `get key` the only way to check if a key exists?
Looking at stats output: STAT cmd_get 51 STAT get_hits 32 STAT get_misses 19 19+32=51, so the stats do make perfect sense. In my (very early stage) code I'm doing something very basic: if ( $cache = $mc-get($key) { // use cached data die($cache); }else{ // use output buffering with a callback function to // render the page normally, and then // cache it under $key then serve it $mc-set($key, $html); die( $html ); } The main reason I ask is, I was wondering if there is any smarter way to check if a key exists, because, I'm just thinking that it would be advantageous to be able to differentiate a get_miss for a reason different than the key wasn't cached yet, like application logic error, versus everything working correctly but my key isn't cached yet or it expired. In my mind, it seems like it would be cleaner from client-side code, to check if a key exists, then issue a get only if the key exists, and be able to see 100% get_hits, but this may not be possible.
Re: Is `get key` the only way to check if a key exists?
The main reason I ask is, I was wondering if there is any smarter way to check if a key exists, because, I'm just thinking that it would be advantageous to be able to differentiate a get_miss for a reason different than the key wasn't cached yet, like application logic error, versus everything working correctly but my key isn't cached yet or it expired. In my mind, it seems like it would be cleaner from client-side code, to check if a key exists, then issue a get only if the key exists, and be able to see 100% get_hits, but this may not be possible. Not sure how you'd expect memcached to know the difference. If your application logic fails, you wouldn't have stored anything in memcached, so there's no way to tell the difference. The only way you *could* tell the difference is if you issue a get and the key existed but was expired for some reason, but it doesn't return that info. If you're worried about the application never setting the keys, you could set a canary key at the very top of your app logic. ie: if ($cache = $mc-get($page_blah)) { # use $cache } else { $canary = $mc-get($page_canary); if ($canary) { die(Something *might* have gone wrong); } $mc-set($page_canary, chirp); # Do real logic, fetch from database, render page. $mc-set($page_blah, $page_data); $mc-delete($page_canary); } So if your page successfully works, the canary should't exist, but the cache entry will. I don't see that as being necessary though... if your app errors that should go to log somewhere, which you should really be watching anyway. -Dormando
Re: Is `get key` the only way to check if a key exists?
On Sat, Aug 20, 2011 at 11:56 AM, dormando dorma...@rydia.net wrote: The main reason I ask is, I was wondering if there is any smarter way to check if a key exists, because, I'm just thinking that it would be advantageous to be able to differentiate a get_miss for a reason different than the key wasn't cached yet, like application logic error, versus everything working correctly but my key isn't cached yet or it expired. In my mind, it seems like it would be cleaner from client-side code, to check if a key exists, then issue a get only if the key exists, and be able to see 100% get_hits, but this may not be possible. Not sure how you'd expect memcached to know the difference. If your application logic fails, you wouldn't have stored anything in memcached, so there's no way to tell the difference. The only way you *could* tell the difference is if you issue a get and the key existed but was expired for some reason, but it doesn't return that info. If you're worried about the application never setting the keys, you could set a canary key at the very top of your app logic. ie: if ($cache = $mc-get($page_blah)) { # use $cache } else { $canary = $mc-get($page_canary); if ($canary) { die(Something *might* have gone wrong); } $mc-set($page_canary, chirp); # Do real logic, fetch from database, render page. $mc-set($page_blah, $page_data); $mc-delete($page_canary); } So if your page successfully works, the canary should't exist, but the cache entry will. I don't see that as being necessary though... if your app errors that should go to log somewhere, which you should really be watching anyway. -Dormando Thanks Dormando, the canary example makes sense, and I can see why it's unnecessary. I think I'm just hung up on get_misses terminology seeming like an error occurred, but in reality it's a measure of how many requested cache items weren't there yet.