commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-new / civicrm / packages / IDS / Caching / Memcached.php
1 <?php
2
3 /**
4 * PHPIDS
5 *
6 * Requirements: PHP5, SimpleXML
7 *
8 * Copyright (c) 2008 PHPIDS group (https://phpids.org)
9 *
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.
14 *
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.
19 *
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/>.
22 *
23 * PHP version 5.1.6+
24 *
25 * @category Security
26 * @package PHPIDS
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/
32 */
33
34 require_once 'IDS/Caching/Interface.php';
35
36 /**
37 * File caching wrapper
38 *
39 * This class inhabits functionality to get and set cache via memcached.
40 *
41 * @category Security
42 * @package PHPIDS
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/
50 * @since Version 0.4
51 */
52 class IDS_Caching_Memcached implements IDS_Caching_Interface
53 {
54
55 /**
56 * Caching type
57 *
58 * @var string
59 */
60 private $type = null;
61
62 /**
63 * Cache configuration
64 *
65 * @var array
66 */
67 private $config = null;
68
69 /**
70 * Flag if the filter storage has been found in memcached
71 *
72 * @var boolean
73 */
74 private $isCached = false;
75
76 /**
77 * Memcache object
78 *
79 * @var object
80 */
81 private $memcache = null;
82
83 /**
84 * Holds an instance of this class
85 *
86 * @var object
87 */
88 private static $cachingInstance = null;
89
90
91 /**
92 * Constructor
93 *
94 * @param string $type caching type
95 * @param array $init the IDS_Init object
96 *
97 * @return void
98 */
99 public function __construct($type, $init)
100 {
101
102 $this->type = $type;
103 $this->config = $init->config['Caching'];
104
105 $this->_connect();
106 }
107
108 /**
109 * Returns an instance of this class
110 *
111 * @param string $type caching type
112 * @param object $init the IDS_Init object
113 *
114 * @return object $this
115 */
116 public static function getInstance($type, $init)
117 {
118
119 if (!self::$cachingInstance) {
120 self::$cachingInstance = new IDS_Caching_Memcached($type, $init);
121 }
122
123 return self::$cachingInstance;
124 }
125
126 /**
127 * Writes cache data
128 *
129 * @param array $data the caching data
130 *
131 * @return object $this
132 */
133 public function setCache(array $data)
134 {
135
136 if(!$this->isCached) {
137 $this->memcache->set(
138 $this->config['key_prefix'] . '.storage',
139 $data, false, $this->config['expiration_time']
140 );
141 }
142
143 return $this;
144 }
145
146 /**
147 * Returns the cached data
148 *
149 * Note that this method returns false if either type or file cache is
150 * not set
151 *
152 * @return mixed cache data or false
153 */
154 public function getCache()
155 {
156
157 $data = $this->memcache->get(
158 $this->config['key_prefix'] .
159 '.storage'
160 );
161 $this->isCached = !empty($data);
162
163 return $data;
164 }
165
166 /**
167 * Connect to the memcached server
168 *
169 * @throws Exception if connection parameters are insufficient
170 * @return void
171 */
172 private function _connect()
173 {
174
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']
181 );
182
183 } else {
184 throw new Exception('Insufficient connection parameters');
185 }
186 }
187 }
188
189 /**
190 * Local variables:
191 * tab-width: 4
192 * c-basic-offset: 4
193 * End:
194 * vim600: sw=4 ts=4 expandtab
195 */