Skip to content

Commit ecbd66c

Browse files
committed
Reworked the whole thing so that it uses the factory pattern.
1 parent 8e95af5 commit ecbd66c

19 files changed

+268
-335
lines changed

extrasetup.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
}
2020

2121
if (0 === strpos($filename, 'php/')) {
22-
// $newFileName = 'src/' . substr($filename, strlen('php/'));
23-
// $info['name'] = $newFileName;
24-
// $pkg->files[$newFileName] = $info;
25-
// unset($pkg->files[$filename]);
22+
//$newFileName = 'src/' . substr($filename, strlen('php/'));
23+
//$info['name'] = $newFileName;
24+
//$pkg->files[$newFileName] = $info;
25+
//unset($pkg->files[$filename]);
2626
}
2727
}
2828
$extrafiles[] = $pkg;

package.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,21 @@
112112
<min>1.4.0</min>
113113
</pearinstaller>
114114
</required>
115+
<optional>
116+
<package>
117+
<name>PEAR2_Autoload</name>
118+
<channel>pear2.php.net</channel>
119+
<min>0.2.4</min>
120+
</package>
121+
<extension>
122+
<name>apc</name>
123+
<min>3.0.13</min>
124+
</extension>
125+
<extension>
126+
<name>wincache</name>
127+
<min>1.1.0</min>
128+
</extension>
129+
</optional>
115130
</dependencies>
116131
<phprelease>
117132
<filelist>

src/PEAR2/Cache/SHM.php

Lines changed: 158 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -36,71 +36,34 @@
3636
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
3737
* @link http://pear2.php.net/PEAR2_Cache_SHM
3838
*/
39-
class SHM implements \IteratorAggregate
39+
abstract class SHM implements \IteratorAggregate
4040
{
41-
/**
42-
* @var Adapter The adapter of this instance.
43-
*/
44-
protected $adapter;
4541

4642
/**
4743
* Creates a new shared memory storage.
4844
*
49-
* Estabilishes a separate persistent storage.
45+
* Estabilishes a separate persistent storage. Adapter is automatically
46+
* chosen based on the available extensions.
47+
*
48+
* @param string $persistentId The ID for the storage.
5049
*
51-
* @param string|Adapter $persistentId The ID for the storage or an
52-
* already instanciated storage adapter. If an ID is specified, an adapter
53-
* will automatically be chosen based on the available extensions.
50+
* @return self|SHM A new instance of an SHM adapter (child of this class).
5451
*/
55-
public function __construct($persistentId)
52+
public static function factory($persistentId)
5653
{
57-
if ($persistentId instanceof Adapter) {
58-
$this->adapter = $persistentId;
54+
if ('cli' === PHP_SAPI) {
55+
return new Adapter\Placebo($persistentId);
56+
} elseif (version_compare(phpversion('wincache'), '1.1.0', '>=')) {
57+
return new Adapter\Wincache($persistentId);
58+
} elseif (version_compare(phpversion('apc'), '3.0.13', '>=')) {
59+
return new Adapter\APC($persistentId);
5960
} else {
60-
if ('cli' === PHP_SAPI) {
61-
$this->adapter = new Adapter\Placebo($persistentId);
62-
} elseif (version_compare(phpversion('wincache'), '1.1.0', '>=')) {
63-
$this->adapter = new Adapter\Wincache($persistentId);
64-
} elseif (version_compare(phpversion('apc'), '3.0.13', '>=')) {
65-
$this->adapter = new Adapter\APC($persistentId);
66-
} else {
67-
throw new SHM\InvalidArgumentException(
68-
'No appropriate adapter available', 1
69-
);
70-
}
61+
throw new SHM\InvalidArgumentException(
62+
'No appropriate adapter available', 1
63+
);
7164
}
7265
}
7366

74-
/**
75-
* Get the currently set SHM adapter.
76-
*
77-
* @return Adapter The currently set adapter
78-
*/
79-
public function getAdapter()
80-
{
81-
return $this->adapter;
82-
}
83-
84-
/**
85-
* Calls a method from the adapter.
86-
*
87-
* This is a magic method, thanks to which any method you call will be
88-
* redirected to the adapter. Every adapter implements at minimum the
89-
* {@link Adapter} interface, so check it out for what you can expect as
90-
* common functionality.
91-
*
92-
* @param string $method The adapter method to call.
93-
* @param array $args The arguments to the method.
94-
*
95-
* @return mixed Whatever the adapter method returns.
96-
*/
97-
public function __call($method, $args)
98-
{
99-
return call_user_func_array(
100-
array($this->adapter, $method), $args
101-
);
102-
}
103-
10467
/**
10568
* Gets a value from the shared memory storage.
10669
*
@@ -114,7 +77,7 @@ public function __call($method, $args)
11477
*/
11578
public function __get($key)
11679
{
117-
return $this->adapter->get($key);
80+
return $this->get($key);
11881
}
11982

12083
/**
@@ -131,7 +94,7 @@ public function __get($key)
13194
*/
13295
public function __set($key, $value)
13396
{
134-
return $this->adapter->set($key, $value);
97+
return $this->set($key, $value);
13598
}
13699

137100
/**
@@ -147,7 +110,7 @@ public function __set($key, $value)
147110
*/
148111
public function __isset($key)
149112
{
150-
return $this->adapter->exists($key);
113+
return $this->exists($key);
151114
}
152115

153116
/**
@@ -163,25 +126,150 @@ public function __isset($key)
163126
*/
164127
public function __unset($key)
165128
{
166-
return $this->adapter->delete($key);
129+
return $this->delete($key);
167130
}
168131

169132
/**
170-
* Retrieve an external iterator
133+
* Creates a new shared memory storage.
134+
*
135+
* Estabilishes a separate persistent storage.
136+
*
137+
* @param string $persistentId The ID for the storage. The storage will be
138+
* reused if it exists, or created if it doesn't exist. Data and locks are
139+
* namespaced by this ID.
140+
*/
141+
abstract public function __construct($persistentId);
142+
143+
/**
144+
* Obtains a named lock.
171145
*
172-
* Returns an external iterator.
146+
* @param string $key Name of the key to obtain. Note that $key may
147+
* repeat for each distinct $persistentId.
148+
* @param double $timeout If the lock can't be immediatly obtained, the
149+
* script will block for at most the specified amount of seconds. Setting
150+
* this to 0 makes lock obtaining non blocking, and setting it to NULL makes
151+
* it block without a time limit.
173152
*
174-
* @param string $filter A PCRE regular expression. Only matching keys
175-
* will be iterated over. Setting this to NULL matches all keys of this
176-
* instance.
177-
* @param bool $keysOnly Whether to return only the keys, or return both
178-
* the keys and values.
153+
* @return bool TRUE on success, FALSE on failure.
154+
*/
155+
abstract public function lock($key, $timeout = null);
156+
157+
/**
158+
* Releases a named lock.
179159
*
180-
* @return An array or instance of an object implementing {@link \Iterator}
181-
* or {@link \Traversable}.
160+
* @param string $key Name of the key to release. Note that $key may
161+
* repeat for each distinct $persistentId.
162+
*
163+
* @return bool TRUE on success, FALSE on failure.
182164
*/
183-
public function getIterator($filter = null, $keysOnly = false)
184-
{
185-
return $this->adapter->getIterator($filter, $keysOnly);
186-
}
165+
abstract public function unlock($key);
166+
167+
/**
168+
* Checks if a specified key is in the storage.
169+
*
170+
* @param string $key Name of key to check.
171+
*
172+
* @return bool TRUE if the key is in the storage, FALSE otherwise.
173+
*/
174+
abstract public function exists($key);
175+
176+
/**
177+
* Adds a value to the shared memory storage.
178+
*
179+
* Adds a value to the storage if it doesn't exist, or fails if it does.
180+
*
181+
* @param string $key Name of key to associate the value with.
182+
* @param mixed $value Value for the specified key.
183+
* @param int $ttl Seconds to store the value. If set to 0 indicates no
184+
* time limit.
185+
*
186+
* @return bool TRUE on success, FALSE on failure.
187+
*/
188+
abstract public function add($key, $value, $ttl = 0);
189+
190+
/**
191+
* Sets a value in the shared memory storage.
192+
*
193+
* Adds a value to the storage if it doesn't exist, overwrites it otherwise.
194+
*
195+
* @param string $key Name of key to associate the value with.
196+
* @param mixed $value Value for the specified key.
197+
* @param int $ttl Seconds to store the value. If set to 0 indicates no
198+
* time limit.
199+
*
200+
* @return bool TRUE on success, FALSE on failure.
201+
*/
202+
abstract public function set($key, $value, $ttl = 0);
203+
204+
/**
205+
* Gets a value from the shared memory storage.
206+
*
207+
* Gets the current value, or throws an exception if it's not stored.
208+
*
209+
* @param string $key Name of key to get the value of.
210+
*
211+
* @return mixed The current value of the specified key.
212+
*/
213+
abstract public function get($key);
214+
215+
/**
216+
* Deletes a value from the shared memory storage.
217+
*
218+
* @param string $key Name of key to delete.
219+
*
220+
* @return bool TRUE on success, FALSE on failure.
221+
*/
222+
abstract public function delete($key);
223+
224+
/**
225+
* Increases a value from the shared memory storage.
226+
*
227+
* Increases a value from the shared memory storage. Unlike a plain
228+
* set($key, get($key)+$step) combination, this function also implicitly
229+
* performs locking.
230+
*
231+
* @param string $key Name of key to increase.
232+
* @param int $step Value to increase the key by.
233+
*
234+
* @return int The new value.
235+
*/
236+
abstract public function inc($key, $step = 1);
237+
238+
/**
239+
* Decreases a value from the shared memory storage.
240+
*
241+
* Decreases a value from the shared memory storage. Unlike a plain
242+
* set($key, get($key)-$step) combination, this function also implicitly
243+
* performs locking.
244+
*
245+
* @param string $key Name of key to decrease.
246+
* @param int $step Value to decrease the key by.
247+
*
248+
* @return int The new value.
249+
*/
250+
abstract public function dec($key, $step = 1);
251+
252+
/**
253+
* Sets a new value if a key has a certain value.
254+
*
255+
* Sets a new value if a key has a certain value. This function only works
256+
* when $old and $new are longs.
257+
*
258+
* @param string $key Key of the value to compare and set.
259+
* @param int $old The value to compare the key against.
260+
* @param int $new The value to set the key to.
261+
*
262+
* @return bool TRUE on success, FALSE on failure.
263+
*/
264+
abstract public function cas($key, $old, $new);
265+
266+
/**
267+
* Clears the persistent storage.
268+
*
269+
* Clears the persistent storage, i.e. removes all keys. Locks are left
270+
* intact.
271+
*
272+
* @return void
273+
*/
274+
abstract public function clear();
187275
}

0 commit comments

Comments
 (0)