googleApcCache

A persistent storage class based on the APC cache, which is not really very persistent, as soon as you restart your web server the storage will be wiped, however for debugging and/or speed it can be useful, kinda, and cache is a lot cheaper then storage.

Defined (1)

The class is defined in the following location(s).

/gdwpm-api/cache/Google_ApcCache.php  
  1. class googleApcCache extends Google_Cache { 
  2.  
  3. public function __construct() { 
  4. if (! function_exists('apc_add')) { 
  5. throw new Google_CacheException("Apc functions not available"); 
  6.  
  7. private function isLocked($key) { 
  8. if ((@apc_fetch($key . '.lock')) === false) { 
  9. return false; 
  10. return true; 
  11.  
  12. private function createLock($key) { 
  13. // the interesting thing is that this could fail if the lock was created in the meantime.. 
  14. // but we'll ignore that out of convenience 
  15. @apc_add($key . '.lock', '', 5); 
  16.  
  17. private function removeLock($key) { 
  18. // suppress all warnings, if some other process removed it that's ok too 
  19. @apc_delete($key . '.lock'); 
  20.  
  21. private function waitForLock($key) { 
  22. // 20 x 250 = 5 seconds 
  23. $tries = 20; 
  24. $cnt = 0; 
  25. do { 
  26. // 250 ms is a long time to sleep, but it does stop the server from burning all resources on polling locks.. 
  27. usleep(250); 
  28. $cnt ++; 
  29. } while ($cnt <= $tries && $this->isLocked($key)); 
  30. if ($this->isLocked($key)) { 
  31. // 5 seconds passed, assume the owning process died off and remove it 
  32. $this->removeLock($key); 
  33.  
  34. /** 
  35. * @inheritDoc 
  36. */ 
  37. public function get($key, $expiration = false) { 
  38.  
  39. if (($ret = @apc_fetch($key)) === false) { 
  40. return false; 
  41. if (!$expiration || (time() - $ret['time'] > $expiration)) { 
  42. $this->delete($key); 
  43. return false; 
  44. return unserialize($ret['data']); 
  45.  
  46. /** 
  47. * @inheritDoc 
  48. */ 
  49. public function set($key, $value) { 
  50. if (@apc_store($key, array('time' => time(), 'data' => serialize($value))) == false) { 
  51. throw new Google_CacheException("Couldn't store data"); 
  52.  
  53. /** 
  54. * @inheritDoc 
  55. * @param String $key 
  56. */ 
  57. public function delete($key) { 
  58. @apc_delete($key);