CacheResource_Memcache.class.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /**
  3. * Memcache and memcached cache handler
  4. *
  5. * Based on example memcache resource included with smarty
  6. *
  7. * @author Christopher Han <xiphux@gmail.com>
  8. * @copyright Copyright (c) 2011 Christopher Han
  9. * @package GitPHP
  10. * @subpackage Cache
  11. */
  12. class GitPHP_CacheResource_Memcache extends Smarty_CacheResource_KeyValueStore
  13. {
  14. /**
  15. * Memcache extension type constant
  16. */
  17. const Memcache = 1;
  18. /**
  19. * Memcached extension type constant
  20. */
  21. const Memcached = 2;
  22. /**
  23. * Memcache object
  24. *
  25. * @var Memcached|Memcache|null
  26. */
  27. protected $memcacheObj = null;
  28. /**
  29. * Memcache extension type
  30. *
  31. * @var int
  32. */
  33. protected $memcacheType = 0;
  34. /**
  35. * Memcache server array
  36. *
  37. * @var array[]
  38. */
  39. protected $servers;
  40. /**
  41. * Constructor
  42. *
  43. * @param array[] $servers server array
  44. */
  45. public function __construct($servers)
  46. {
  47. if ((!$servers) || (!is_array($servers)) || (count($servers) < 1)) {
  48. throw new GitPHP_MessageException('No Memcache servers defined', true, 500);
  49. }
  50. if (class_exists('Memcached')) {
  51. $this->memcacheObj = new Memcached();
  52. $this->memcacheType = GitPHP_CacheResource_Memcache::Memcached;
  53. $this->memcacheObj->addServers($servers);
  54. } else if (class_exists('Memcache')) {
  55. $this->memcacheObj = new Memcache();
  56. $this->memcacheType = GitPHP_CacheResource_Memcache::Memcache;
  57. foreach ($servers as $server) {
  58. if (is_array($server)) {
  59. $host = $server[0];
  60. $port = 11211;
  61. if (isset($server[1]))
  62. $port = $server[1];
  63. $weight = 1;
  64. if (isset($server[2]))
  65. $weight = $server[2];
  66. $this->memcacheObj->addServer($host, $port, true, $weight);
  67. }
  68. }
  69. } else {
  70. throw new GitPHP_MissingMemcacheException();
  71. }
  72. $this->servers = $servers;
  73. }
  74. /**
  75. * Read cached data
  76. *
  77. * @param array $keys array of keys to load
  78. * @return array key/value cached data
  79. */
  80. protected function read(array $keys)
  81. {
  82. $keymap = array();
  83. $hashedkeys = array();
  84. foreach ($keys as $key) {
  85. $newkey = sha1($key);
  86. $keymap[$newkey] = $key;
  87. $hashedkeys[] = $newkey;
  88. }
  89. $data = false;
  90. $cachedata = array();
  91. if ($this->memcacheType == GitPHP_CacheResource_Memcache::Memcache) {
  92. $cachedata = $this->memcacheObj->get($hashedkeys);
  93. } else if ($this->memcacheType == GitPHP_CacheResource_Memcache::Memcached) {
  94. $cachedata = $this->memcacheObj->getMulti($hashedkeys);
  95. }
  96. if ($cachedata) {
  97. foreach ($cachedata as $key => $value) {
  98. $origkey = $keymap[$key];
  99. if (!empty($origkey)) {
  100. $data[$origkey] = $value;
  101. }
  102. }
  103. }
  104. return $data;
  105. }
  106. /**
  107. * Write data to cache
  108. *
  109. * @param array $keys array of key/value data to store
  110. * @param int $expire expiration time
  111. * @return boolean true on success
  112. */
  113. protected function write(array $keys, $expire = null)
  114. {
  115. if ($this->memcacheType == GitPHP_CacheResource_Memcache::Memcache) {
  116. foreach ($keys as $key => $value) {
  117. $this->memcacheObj->set(sha1($key), $value, 0, $expire);
  118. }
  119. return true;
  120. } else if ($this->memcacheType == GitPHP_CacheResource_Memcache::Memcached) {
  121. $mapped = array();
  122. foreach ($keys as $key => $value) {
  123. $mapped[sha1($key)] = $value;
  124. }
  125. $this->memcacheObj->setMulti($mapped, $expire);
  126. return true;
  127. }
  128. return false;
  129. }
  130. /**
  131. * Delete data from cache
  132. *
  133. * @param array $keys array of keys to delete
  134. * @return boolean true on success
  135. */
  136. protected function delete(array $keys)
  137. {
  138. foreach ($keys as $key) {
  139. $this->memcacheObj->delete(sha1($key));
  140. }
  141. return true;
  142. }
  143. /**
  144. * Delete all data from cache
  145. *
  146. * @return boolean true on success
  147. */
  148. protected function purge()
  149. {
  150. return $this->memcacheObj->flush();
  151. }
  152. }