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