Google_MemcacheCache

A persistent storage class based on the memcache, which is not really very persistent, as soon as you restart your memcache daemon 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).

/google-api-php-client/api-code/cache/Google_MemcacheCache.php  
  1. class Google_MemcacheCache extends Google_Cache { 
  2. private $connection = false; 
  3.  
  4. public function __construct() { 
  5. global $apiConfig; 
  6. if (! function_exists('memcache_connect')) { 
  7. throw new Google_CacheException("Memcache functions not available"); 
  8. $this->host = $apiConfig['ioMemCacheCache_host']; 
  9. $this->port = $apiConfig['ioMemCacheCache_port']; 
  10. if (empty($this->host) || empty($this->port)) { 
  11. throw new Google_CacheException("You need to supply a valid memcache host and port"); 
  12.  
  13. private function isLocked($key) { 
  14. $this->check(); 
  15. if ((@memcache_get($this->connection, $key . '.lock')) === false) { 
  16. return false; 
  17. return true; 
  18.  
  19. private function createLock($key) { 
  20. $this->check(); 
  21. // the interesting thing is that this could fail if the lock was created in the meantime.. 
  22. // but we'll ignore that out of convenience 
  23. @memcache_add($this->connection, $key . '.lock', '', 0, 5); 
  24.  
  25. private function removeLock($key) { 
  26. $this->check(); 
  27. // suppress all warnings, if some other process removed it that's ok too 
  28. @memcache_delete($this->connection, $key . '.lock'); 
  29.  
  30. private function waitForLock($key) { 
  31. $this->check(); 
  32. // 20 x 250 = 5 seconds 
  33. $tries = 20; 
  34. $cnt = 0; 
  35. do { 
  36. // 250 ms is a long time to sleep, but it does stop the server from burning all resources on polling locks.. 
  37. usleep(250); 
  38. $cnt ++; 
  39. } while ($cnt <= $tries && $this->isLocked($key)); 
  40. if ($this->isLocked($key)) { 
  41. // 5 seconds passed, assume the owning process died off and remove it 
  42. $this->removeLock($key); 
  43.  
  44. // I prefer lazy initialization since the cache isn't used every request 
  45. // so this potentially saves a lot of overhead 
  46. private function connect() { 
  47. if (! $this->connection = @memcache_pconnect($this->host, $this->port)) { 
  48. throw new Google_CacheException("Couldn't connect to memcache server"); 
  49.  
  50. private function check() { 
  51. if (! $this->connection) { 
  52. $this->connect(); 
  53.  
  54. /** 
  55. * @inheritDoc 
  56. */ 
  57. public function get($key, $expiration = false) { 
  58. $this->check(); 
  59. if (($ret = @memcache_get($this->connection, $key)) === false) { 
  60. return false; 
  61. if (is_numeric($expiration) && (time() - $ret['time'] > $expiration)) { 
  62. $this->delete($key); 
  63. return false; 
  64. return $ret['data']; 
  65.  
  66. /** 
  67. * @inheritDoc 
  68. * @param string $key 
  69. * @param string $value 
  70. * @throws Google_CacheException 
  71. */ 
  72. public function set($key, $value) { 
  73. $this->check(); 
  74. // we store it with the cache_time default expiration so objects will at least get cleaned eventually. 
  75. if (@memcache_set($this->connection, $key, array('time' => time(),  
  76. 'data' => $value), false) == false) { 
  77. throw new Google_CacheException("Couldn't store data in cache"); 
  78.  
  79. /** 
  80. * @inheritDoc 
  81. * @param String $key 
  82. */ 
  83. public function delete($key) { 
  84. $this->check(); 
  85. @memcache_delete($this->connection, $key);