1
0

Memcached.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. /**
  3. * PHPIDS
  4. *
  5. * Requirements: PHP5, SimpleXML
  6. *
  7. * Copyright (c) 2008 PHPIDS group (https://phpids.org)
  8. *
  9. * PHPIDS is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU Lesser General Public License as published by
  11. * the Free Software Foundation, version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * PHPIDS is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public License
  20. * along with PHPIDS. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * PHP version 5.1.6+
  23. *
  24. * @category Security
  25. * @package PHPIDS
  26. * @author Mario Heiderich <mario.heiderich@gmail.com>
  27. * @author Christian Matthies <ch0012@gmail.com>
  28. * @author Lars Strojny <lars@strojny.net>
  29. * @license http://www.gnu.org/licenses/lgpl.html LGPL
  30. * @link http://php-ids.org/
  31. */
  32. require_once 'IDS/Caching/Interface.php';
  33. /**
  34. * File caching wrapper
  35. *
  36. * This class inhabits functionality to get and set cache via memcached.
  37. *
  38. * @category Security
  39. * @package PHPIDS
  40. * @author Christian Matthies <ch0012@gmail.com>
  41. * @author Mario Heiderich <mario.heiderich@gmail.com>
  42. * @author Lars Strojny <lars@strojny.net>
  43. * @copyright 2007-2009 The PHPIDS Groupoup
  44. * @license http://www.gnu.org/licenses/lgpl.html LGPL
  45. * @version Release: $Id:Memcached.php 517 2007-09-15 15:04:13Z mario $
  46. * @link http://php-ids.org/
  47. * @since Version 0.4
  48. */
  49. class IDS_Caching_Memcached implements IDS_Caching_Interface
  50. {
  51. /**
  52. * Caching type
  53. *
  54. * @var string
  55. */
  56. private $type = null;
  57. /**
  58. * Cache configuration
  59. *
  60. * @var array
  61. */
  62. private $config = null;
  63. /**
  64. * Flag if the filter storage has been found in memcached
  65. *
  66. * @var boolean
  67. */
  68. private $isCached = false;
  69. /**
  70. * Memcache object
  71. *
  72. * @var object
  73. */
  74. private $memcache = null;
  75. /**
  76. * Holds an instance of this class
  77. *
  78. * @var object
  79. */
  80. private static $cachingInstance = null;
  81. /**
  82. * Constructor
  83. *
  84. * @param string $type caching type
  85. * @param array $init the IDS_Init object
  86. *
  87. * @return void
  88. */
  89. public function __construct($type, $init)
  90. {
  91. $this->type = $type;
  92. $this->config = $init->config['Caching'];
  93. $this->_connect();
  94. }
  95. /**
  96. * Returns an instance of this class
  97. *
  98. * @param string $type caching type
  99. * @param object $init the IDS_Init object
  100. *
  101. * @return object $this
  102. */
  103. public static function getInstance($type, $init)
  104. {
  105. if (!self::$cachingInstance) {
  106. self::$cachingInstance = new IDS_Caching_Memcached($type, $init);
  107. }
  108. return self::$cachingInstance;
  109. }
  110. /**
  111. * Writes cache data
  112. *
  113. * @param array $data the caching data
  114. *
  115. * @return object $this
  116. */
  117. public function setCache(array $data)
  118. {
  119. if(!$this->isCached) {
  120. $this->memcache->set(
  121. $this->config['key_prefix'] . '.storage',
  122. $data, false, $this->config['expiration_time']
  123. );
  124. }
  125. return $this;
  126. }
  127. /**
  128. * Returns the cached data
  129. *
  130. * Note that this method returns false if either type or file cache is
  131. * not set
  132. *
  133. * @return mixed cache data or false
  134. */
  135. public function getCache()
  136. {
  137. $data = $this->memcache->get(
  138. $this->config['key_prefix'] .
  139. '.storage'
  140. );
  141. $this->isCached = !empty($data);
  142. return $data;
  143. }
  144. /**
  145. * Connect to the memcached server
  146. *
  147. * @throws Exception if connection parameters are insufficient
  148. * @return void
  149. */
  150. private function _connect()
  151. {
  152. if ($this->config['host'] && $this->config['port']) {
  153. // establish the memcache connection
  154. $this->memcache = new Memcache;
  155. $this->memcache->pconnect(
  156. $this->config['host'],
  157. $this->config['port']
  158. );
  159. } else {
  160. throw new Exception('Insufficient connection parameters');
  161. }
  162. }
  163. }
  164. /**
  165. * Local variables:
  166. * tab-width: 4
  167. * c-basic-offset: 4
  168. * End:
  169. * vim600: sw=4 ts=4 expandtab
  170. */