PluginProbe
Gianism / 1.0
Gianism v1.0
4.3.0 4.3.1 4.3.2 4.3.3 4.3.4 4.4.0 5.0.0 5.0.1 5.0.2 5.1.0 5.2.1 5.2.2 5.3.0 6.0.0 6.0.1 trunk 1.0 1.1 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 All 65 releases
gianism / sdks / google / cache / apiApcCache.php

apiApcCache.php in Gianism 1.0, at sdks/google/cache/apiApcCache.php

98 lines 2.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 apiApcCache extends apiCache {
27
28 public function __construct() {
29 if (! function_exists('apc_add')) {
30 throw new apiCacheException("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 apiCacheException("Couldn't store data");
88 }
89 }
90
91 /**
92 * @inheritDoc
93 */
94 public function delete($key) {
95 @apc_delete($key);
96 }
97 }
98