6 * Requirements: PHP5, SimpleXML
8 * Copyright (c) 2008 PHPIDS group (https://phpids.org)
10 * PHPIDS is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation, version 3 of the License, or
13 * (at your option) any later version.
15 * PHPIDS is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with PHPIDS. If not, see <http://www.gnu.org/licenses/>.
27 * @author Mario Heiderich <mario.heiderich@gmail.com>
28 * @author Christian Matthies <ch0012@gmail.com>
29 * @author Lars Strojny <lars@strojny.net>
30 * @license http://www.gnu.org/licenses/lgpl.html LGPL
31 * @link http://php-ids.org/
34 require_once 'IDS/Caching/Interface.php';
37 * File caching wrapper
39 * This class inhabits functionality to get and set cache via memcached.
43 * @author Christian Matthies <ch0012@gmail.com>
44 * @author Mario Heiderich <mario.heiderich@gmail.com>
45 * @author Lars Strojny <lars@strojny.net>
46 * @copyright 2007-2009 The PHPIDS Groupoup
47 * @license http://www.gnu.org/licenses/lgpl.html LGPL
48 * @version Release: $Id:Memcached.php 517 2007-09-15 15:04:13Z mario $
49 * @link http://php-ids.org/
52 class IDS_Caching_Memcached
implements IDS_Caching_Interface
67 private $config = null;
70 * Flag if the filter storage has been found in memcached
74 private $isCached = false;
81 private $memcache = null;
84 * Holds an instance of this class
88 private static $cachingInstance = null;
94 * @param string $type caching type
95 * @param array $init the IDS_Init object
99 public function __construct($type, $init)
103 $this->config
= $init->config
['Caching'];
109 * Returns an instance of this class
111 * @param string $type caching type
112 * @param object $init the IDS_Init object
114 * @return object $this
116 public static function getInstance($type, $init)
119 if (!self
::$cachingInstance) {
120 self
::$cachingInstance = new IDS_Caching_Memcached($type, $init);
123 return self
::$cachingInstance;
129 * @param array $data the caching data
131 * @return object $this
133 public function setCache(array $data)
136 if(!$this->isCached
) {
137 $this->memcache
->set(
138 $this->config
['key_prefix'] . '.storage',
139 $data, false, $this->config
['expiration_time']
147 * Returns the cached data
149 * Note that this method returns false if either type or file cache is
152 * @return mixed cache data or false
154 public function getCache()
157 $data = $this->memcache
->get(
158 $this->config
['key_prefix'] .
161 $this->isCached
= !empty($data);
167 * Connect to the memcached server
169 * @throws Exception if connection parameters are insufficient
172 private function _connect()
175 if ($this->config
['host'] && $this->config
['port']) {
176 // establish the memcache connection
177 $this->memcache
= new Memcache
;
178 $this->memcache
->pconnect(
179 $this->config
['host'],
180 $this->config
['port']
184 throw new Exception('Insufficient connection parameters');
194 * vim600: sw=4 ts=4 expandtab