Google_Cache_Memcache

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.

Defined (1)

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

/lib/vendor/google/apiclient/src/Google/Cache/Memcache.php  
  1. class Google_Cache_Memcache extends Google_Cache_Abstract 
  2. private $connection = false; 
  3. private $mc = false; 
  4. private $host; 
  5. private $port; 
  6.  
  7. public function __construct(Google_Client $client) 
  8. if (!function_exists('memcache_connect') && !class_exists("Memcached")) { 
  9. throw new Google_Cache_Exception("Memcache functions not available"); 
  10. if ($client->isAppEngine()) { 
  11. // No credentials needed for GAE. 
  12. $this->mc = new Memcached(); 
  13. $this->connection = true; 
  14. } else { 
  15. $this->host = $client->getClassConfig($this, 'host'); 
  16. $this->port = $client->getClassConfig($this, 'port'); 
  17. if (empty($this->host) || (empty($this->port) && (string) $this->port != "0")) { 
  18. throw new Google_Cache_Exception("You need to supply a valid memcache host and port"); 
  19.  
  20. /** 
  21. * @inheritDoc 
  22. */ 
  23. public function get($key, $expiration = false) 
  24. $this->connect(); 
  25. $ret = false; 
  26. if ($this->mc) { 
  27. $ret = $this->mc->get($key); 
  28. } else { 
  29. $ret = memcache_get($this->connection, $key); 
  30. if ($ret === false) { 
  31. return false; 
  32. if (is_numeric($expiration) && (time() - $ret['time'] > $expiration)) { 
  33. $this->delete($key); 
  34. return false; 
  35. return $ret['data']; 
  36.  
  37. /** 
  38. * @inheritDoc 
  39. * @param string $key 
  40. * @param string $value 
  41. * @throws Google_Cache_Exception 
  42. */ 
  43. public function set($key, $value) 
  44. $this->connect(); 
  45. // we store it with the cache_time default expiration so objects will at 
  46. // least get cleaned eventually. 
  47. $data = array('time' => time(), 'data' => $value); 
  48. $rc = false; 
  49. if ($this->mc) { 
  50. $rc = $this->mc->set($key, $data); 
  51. } else { 
  52. $rc = memcache_set($this->connection, $key, $data, false); 
  53. if ($rc == false) { 
  54. throw new Google_Cache_Exception("Couldn't store data in cache"); 
  55.  
  56. /** 
  57. * @inheritDoc 
  58. * @param String $key 
  59. */ 
  60. public function delete($key) 
  61. $this->connect(); 
  62. if ($this->mc) { 
  63. $this->mc->delete($key, 0); 
  64. } else { 
  65. memcache_delete($this->connection, $key, 0); 
  66.  
  67. /** 
  68. * Lazy initialiser for memcache connection. Uses pconnect for to take  
  69. * advantage of the persistence pool where possible.  
  70. */ 
  71. private function connect() 
  72. if ($this->connection) { 
  73. return; 
  74.  
  75. if (class_exists("Memcached")) { 
  76. $this->mc = new Memcached(); 
  77. $this->mc->addServer($this->host, $this->port); 
  78. $this->connection = true; 
  79. } else { 
  80. $this->connection = memcache_pconnect($this->host, $this->port); 
  81.  
  82. if (! $this->connection) { 
  83. throw new Google_Cache_Exception("Couldn't connect to memcache server");