GoogleGAL_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).

/core/Google/Cache/Memcache.php  
  1. class GoogleGAL_Cache_Memcache extends GoogleGAL_Cache_Abstract 
  2. private $connection = false; 
  3. private $mc = false; 
  4. private $host; 
  5. private $port; 
  6.  
  7. /** 
  8. * @var GoogleGAL_Client the current client 
  9. */ 
  10. private $client; 
  11.  
  12. public function __construct(GoogleGAL_Client $client) 
  13. if (!function_exists('memcache_connect') && !class_exists("Memcached")) { 
  14. $error = "Memcache functions not available"; 
  15.  
  16. $client->getLogger()->error($error); 
  17. throw new GoogleGAL_Cache_Exception($error); 
  18.  
  19. $this->client = $client; 
  20.  
  21. if ($client->isAppEngine()) { 
  22. // No credentials needed for GAE. 
  23. $this->mc = new Memcached(); 
  24. $this->connection = true; 
  25. } else { 
  26. $this->host = $client->getClassConfig($this, 'host'); 
  27. $this->port = $client->getClassConfig($this, 'port'); 
  28. if (empty($this->host) || (empty($this->port) && (string) $this->port != "0")) { 
  29. $error = "You need to supply a valid memcache host and port"; 
  30.  
  31. $client->getLogger()->error($error); 
  32. throw new GoogleGAL_Cache_Exception($error); 
  33.  
  34. /** 
  35. * @inheritDoc 
  36. */ 
  37. public function get($key, $expiration = false) 
  38. $this->connect(); 
  39. $ret = false; 
  40. if ($this->mc) { 
  41. $ret = $this->mc->get($key); 
  42. } else { 
  43. $ret = memcache_get($this->connection, $key); 
  44. if ($ret === false) { 
  45. $this->client->getLogger()->debug( 
  46. 'Memcache cache miss',  
  47. array('key' => $key) 
  48. ); 
  49. return false; 
  50. if (is_numeric($expiration) && (time() - $ret['time'] > $expiration)) { 
  51. $this->client->getLogger()->debug( 
  52. 'Memcache cache miss (expired)',  
  53. array('key' => $key, 'var' => $ret) 
  54. ); 
  55. $this->delete($key); 
  56. return false; 
  57.  
  58. $this->client->getLogger()->debug( 
  59. 'Memcache cache hit',  
  60. array('key' => $key, 'var' => $ret) 
  61. ); 
  62.  
  63. return $ret['data']; 
  64.  
  65. /** 
  66. * @inheritDoc 
  67. * @param string $key 
  68. * @param string $value 
  69. * @throws GoogleGAL_Cache_Exception 
  70. */ 
  71. public function set($key, $value) 
  72. $this->connect(); 
  73. // we store it with the cache_time default expiration so objects will at 
  74. // least get cleaned eventually. 
  75. $data = array('time' => time(), 'data' => $value); 
  76. $rc = false; 
  77. if ($this->mc) { 
  78. $rc = $this->mc->set($key, $data); 
  79. } else { 
  80. $rc = memcache_set($this->connection, $key, $data, false); 
  81. if ($rc == false) { 
  82. $this->client->getLogger()->error( 
  83. 'Memcache cache set failed',  
  84. array('key' => $key, 'var' => $data) 
  85. ); 
  86.  
  87. throw new GoogleGAL_Cache_Exception("Couldn't store data in cache"); 
  88.  
  89. $this->client->getLogger()->debug( 
  90. 'Memcache cache set',  
  91. array('key' => $key, 'var' => $data) 
  92. ); 
  93.  
  94. /** 
  95. * @inheritDoc 
  96. * @param String $key 
  97. */ 
  98. public function delete($key) 
  99. $this->connect(); 
  100. if ($this->mc) { 
  101. $this->mc->delete($key, 0); 
  102. } else { 
  103. memcache_delete($this->connection, $key, 0); 
  104.  
  105. $this->client->getLogger()->debug( 
  106. 'Memcache cache delete',  
  107. array('key' => $key) 
  108. ); 
  109.  
  110. /** 
  111. * Lazy initialiser for memcache connection. Uses pconnect for to take 
  112. * advantage of the persistence pool where possible. 
  113. */ 
  114. private function connect() 
  115. if ($this->connection) { 
  116. return; 
  117.  
  118. if (class_exists("Memcached")) { 
  119. $this->mc = new Memcached(); 
  120. $this->mc->addServer($this->host, $this->port); 
  121. $this->connection = true; 
  122. } else { 
  123. $this->connection = memcache_pconnect($this->host, $this->port); 
  124.  
  125. if (! $this->connection) { 
  126. $error = "Couldn't connect to memcache server"; 
  127.  
  128. $this->client->getLogger()->error($error); 
  129. throw new GoogleGAL_Cache_Exception($error);