25 |
- |
1 |
<?php
|
|
|
2 |
/*
|
|
|
3 |
* Copyright 2010 Google Inc.
|
|
|
4 |
*
|
|
|
5 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
6 |
* you may not use this file except in compliance with the License.
|
|
|
7 |
* You may obtain a copy of the License at
|
|
|
8 |
*
|
|
|
9 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
10 |
*
|
|
|
11 |
* Unless required by applicable law or agreed to in writing, software
|
|
|
12 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
14 |
* See the License for the specific language governing permissions and
|
|
|
15 |
* limitations under the License.
|
|
|
16 |
*/
|
|
|
17 |
|
|
|
18 |
/**
|
|
|
19 |
* A persistent storage class based on the APC cache, which is not
|
|
|
20 |
* really very persistent, as soon as you restart your web server
|
|
|
21 |
* the storage will be wiped, however for debugging and/or speed
|
|
|
22 |
* it can be useful, kinda, and cache is a lot cheaper then storage.
|
|
|
23 |
*
|
|
|
24 |
* @author Chris Chabot <chabotc@google.com>
|
|
|
25 |
*/
|
|
|
26 |
class googleApcCache extends Google_Cache {
|
|
|
27 |
|
|
|
28 |
public function __construct() {
|
|
|
29 |
if (! function_exists('apc_add')) {
|
|
|
30 |
throw new Google_CacheException("Apc functions not available");
|
|
|
31 |
}
|
|
|
32 |
}
|
|
|
33 |
|
|
|
34 |
private function isLocked($key) {
|
|
|
35 |
if ((@apc_fetch($key . '.lock')) === false) {
|
|
|
36 |
return false;
|
|
|
37 |
}
|
|
|
38 |
return true;
|
|
|
39 |
}
|
|
|
40 |
|
|
|
41 |
private function createLock($key) {
|
|
|
42 |
// the interesting thing is that this could fail if the lock was created in the meantime..
|
|
|
43 |
// but we'll ignore that out of convenience
|
|
|
44 |
@apc_add($key . '.lock', '', 5);
|
|
|
45 |
}
|
|
|
46 |
|
|
|
47 |
private function removeLock($key) {
|
|
|
48 |
// suppress all warnings, if some other process removed it that's ok too
|
|
|
49 |
@apc_delete($key . '.lock');
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
private function waitForLock($key) {
|
|
|
53 |
// 20 x 250 = 5 seconds
|
|
|
54 |
$tries = 20;
|
|
|
55 |
$cnt = 0;
|
|
|
56 |
do {
|
|
|
57 |
// 250 ms is a long time to sleep, but it does stop the server from burning all resources on polling locks..
|
|
|
58 |
usleep(250);
|
|
|
59 |
$cnt ++;
|
|
|
60 |
} while ($cnt <= $tries && $this->isLocked($key));
|
|
|
61 |
if ($this->isLocked($key)) {
|
|
|
62 |
// 5 seconds passed, assume the owning process died off and remove it
|
|
|
63 |
$this->removeLock($key);
|
|
|
64 |
}
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
/**
|
|
|
68 |
* @inheritDoc
|
|
|
69 |
*/
|
|
|
70 |
public function get($key, $expiration = false) {
|
|
|
71 |
|
|
|
72 |
if (($ret = @apc_fetch($key)) === false) {
|
|
|
73 |
return false;
|
|
|
74 |
}
|
|
|
75 |
if (!$expiration || (time() - $ret['time'] > $expiration)) {
|
|
|
76 |
$this->delete($key);
|
|
|
77 |
return false;
|
|
|
78 |
}
|
|
|
79 |
return unserialize($ret['data']);
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
/**
|
|
|
83 |
* @inheritDoc
|
|
|
84 |
*/
|
|
|
85 |
public function set($key, $value) {
|
|
|
86 |
if (@apc_store($key, array('time' => time(), 'data' => serialize($value))) == false) {
|
|
|
87 |
throw new Google_CacheException("Couldn't store data");
|
|
|
88 |
}
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
/**
|
|
|
92 |
* @inheritDoc
|
|
|
93 |
* @param String $key
|
|
|
94 |
*/
|
|
|
95 |
public function delete($key) {
|
|
|
96 |
@apc_delete($key);
|
|
|
97 |
}
|
|
|
98 |
}
|