/wp-content/plugins/google-analytics-dashboard-for-wp/tools/src/Google/Cache/Memcache.php

https://gitlab.com/bhargavi_dcw/dflocal · PHP · 178 lines · 127 code · 9 blank · 42 comment · 17 complexity · 6f00fdadef28f8a1f02522dfd8e3d1f9 MD5 · raw file

  1. <?php
  2. /*
  3. * Copyright 2008 Google Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. require_once realpath(dirname(__FILE__) . '/../../../autoload.php');
  18. /**
  19. * A persistent storage class based on the memcache, which is not
  20. * really very persistent, as soon as you restart your memcache daemon
  21. * the storage will be wiped.
  22. *
  23. * Will use either the memcache or memcached extensions, preferring
  24. * memcached.
  25. *
  26. * @author Chris Chabot <chabotc@google.com>
  27. */
  28. class Google_Cache_Memcache extends Google_Cache_Abstract
  29. {
  30. private $connection = false;
  31. private $mc = false;
  32. private $host;
  33. private $port;
  34. /**
  35. *
  36. * @var Google_Client the current client
  37. */
  38. private $client;
  39. public function __construct(Google_Client $client)
  40. {
  41. if (! function_exists('memcache_connect') && ! class_exists("Memcached")) {
  42. $error = "Memcache functions not available";
  43. $client->getLogger()->error($error);
  44. throw new Google_Cache_Exception($error);
  45. }
  46. $this->client = $client;
  47. if ($client->isAppEngine()) {
  48. // No credentials needed for GAE.
  49. $this->mc = new Memcached();
  50. $this->connection = true;
  51. } else {
  52. $this->host = $client->getClassConfig($this, 'host');
  53. $this->port = $client->getClassConfig($this, 'port');
  54. if (empty($this->host) || (empty($this->port) && (string) $this->port != "0")) {
  55. $error = "You need to supply a valid memcache host and port";
  56. $client->getLogger()->error($error);
  57. throw new Google_Cache_Exception($error);
  58. }
  59. }
  60. }
  61. /**
  62. * @inheritDoc
  63. */
  64. public function get($key, $expiration = false)
  65. {
  66. $this->connect();
  67. $ret = false;
  68. if ($this->mc) {
  69. $ret = $this->mc->get($key);
  70. } else {
  71. $ret = memcache_get($this->connection, $key);
  72. }
  73. if ($ret === false) {
  74. $this->client->getLogger()->debug('Memcache cache miss', array(
  75. 'key' => $key
  76. ));
  77. return false;
  78. }
  79. if (is_numeric($expiration) && (time() - $ret['time'] > $expiration)) {
  80. $this->client->getLogger()->debug('Memcache cache miss (expired)', array(
  81. 'key' => $key,
  82. 'var' => $ret
  83. ));
  84. $this->delete($key);
  85. return false;
  86. }
  87. $this->client->getLogger()->debug('Memcache cache hit', array(
  88. 'key' => $key,
  89. 'var' => $ret
  90. ));
  91. return $ret['data'];
  92. }
  93. /**
  94. * @inheritDoc
  95. *
  96. * @param string $key
  97. * @param string $value
  98. * @throws Google_Cache_Exception
  99. */
  100. public function set($key, $value)
  101. {
  102. $this->connect();
  103. // we store it with the cache_time default expiration so objects will at
  104. // least get cleaned eventually.
  105. $data = array(
  106. 'time' => time(),
  107. 'data' => $value
  108. );
  109. $rc = false;
  110. if ($this->mc) {
  111. $rc = $this->mc->set($key, $data);
  112. } else {
  113. $rc = memcache_set($this->connection, $key, $data, false);
  114. }
  115. if ($rc == false) {
  116. $this->client->getLogger()->error('Memcache cache set failed', array(
  117. 'key' => $key,
  118. 'var' => $data
  119. ));
  120. throw new Google_Cache_Exception("Couldn't store data in cache");
  121. }
  122. $this->client->getLogger()->debug('Memcache cache set', array(
  123. 'key' => $key,
  124. 'var' => $data
  125. ));
  126. }
  127. /**
  128. * @inheritDoc
  129. *
  130. * @param String $key
  131. */
  132. public function delete($key)
  133. {
  134. $this->connect();
  135. if ($this->mc) {
  136. $this->mc->delete($key, 0);
  137. } else {
  138. memcache_delete($this->connection, $key, 0);
  139. }
  140. $this->client->getLogger()->debug('Memcache cache delete', array(
  141. 'key' => $key
  142. ));
  143. }
  144. /**
  145. * Lazy initialiser for memcache connection.
  146. * Uses pconnect for to take
  147. * advantage of the persistence pool where possible.
  148. */
  149. private function connect()
  150. {
  151. if ($this->connection) {
  152. return;
  153. }
  154. if (class_exists("Memcached")) {
  155. $this->mc = new Memcached();
  156. $this->mc->addServer($this->host, $this->port);
  157. $this->connection = true;
  158. } else {
  159. $this->connection = memcache_pconnect($this->host, $this->port);
  160. }
  161. if (! $this->connection) {
  162. $error = "Couldn't connect to memcache server";
  163. $this->client->getLogger()->error($error);
  164. throw new Google_Cache_Exception($error);
  165. }
  166. }
  167. }