Apc.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. * APC caching wrapper
  35. *
  36. * This class inhabits functionality to get and set cache via memcached.
  37. *
  38. * @category Security
  39. * @package PHPIDS
  40. * @author Yves Berkholz <godzilla80@gmx.net>
  41. * @copyright 2007-2009 The PHPIDS Groupoup
  42. * @license http://www.gnu.org/licenses/lgpl.html LGPL
  43. * @version Release: $Id$
  44. * @link http://php-ids.org/
  45. * @since Version 0.6.5
  46. */
  47. class IDS_Caching_Apc implements IDS_Caching_Interface
  48. {
  49. /**
  50. * Caching type
  51. *
  52. * @var string
  53. */
  54. private $type = null;
  55. /**
  56. * Cache configuration
  57. *
  58. * @var array
  59. */
  60. private $config = null;
  61. /**
  62. * Flag if the filter storage has been found in memcached
  63. *
  64. * @var boolean
  65. */
  66. private $isCached = false;
  67. /**
  68. * Holds an instance of this class
  69. *
  70. * @var object
  71. */
  72. private static $cachingInstance = null;
  73. /**
  74. * Constructor
  75. *
  76. * @param string $type caching type
  77. * @param array $init the IDS_Init object
  78. *
  79. * @return void
  80. */
  81. public function __construct($type, $init)
  82. {
  83. $this->type = $type;
  84. $this->config = $init->config['Caching'];
  85. }
  86. /**
  87. * Returns an instance of this class
  88. *
  89. * @param string $type caching type
  90. * @param object $init the IDS_Init object
  91. *
  92. * @return object $this
  93. */
  94. public static function getInstance($type, $init)
  95. {
  96. if (!self::$cachingInstance) {
  97. self::$cachingInstance = new IDS_Caching_Apc($type, $init);
  98. }
  99. return self::$cachingInstance;
  100. }
  101. /**
  102. * Writes cache data
  103. *
  104. * @param array $data the caching data
  105. *
  106. * @return object $this
  107. */
  108. public function setCache(array $data)
  109. {
  110. if(!$this->isCached)
  111. apc_store($this->config['key_prefix'] . '.storage',
  112. $data, $this->config['expiration_time']);
  113. return $this;
  114. }
  115. /**
  116. * Returns the cached data
  117. *
  118. * Note that this method returns false if either type or file cache is
  119. * not set
  120. *
  121. * @return mixed cache data or false
  122. */
  123. public function getCache()
  124. {
  125. $data = apc_fetch($this->config['key_prefix'] . '.storage');
  126. $this->isCached = !empty($data);
  127. return $data;
  128. }
  129. }
  130. /**
  131. * Local variables:
  132. * tab-width: 4
  133. * c-basic-offset: 4
  134. * End:
  135. * vim600: sw=4 ts=4 expandtab
  136. */