3 * Copyright 2010 Google Inc.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
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.
24 * @author Chris Chabot <chabotc@google.com>
26 class googleApcCache
extends Google_Cache
{
28 public function __construct() {
29 if (! function_exists('apc_add')) {
30 throw new Google_CacheException("Apc functions not available");
34 private function isLocked($key) {
35 if ((@apc_fetch
($key . '.lock')) === false) {
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);
47 private function removeLock($key) {
48 // suppress all warnings, if some other process removed it that's ok too
49 @apc_delete
($key . '.lock');
52 private function waitForLock($key) {
53 // 20 x 250 = 5 seconds
57 // 250 ms is a long time to sleep, but it does stop the server from burning all resources on polling locks..
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);
70 public function get($key, $expiration = false) {
72 if (($ret = @apc_fetch
($key)) === false) {
75 if (!$expiration ||
(time() - $ret['time'] > $expiration)) {
79 return unserialize($ret['data']);
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");
95 public function delete($key) {