MDL-45536 atto_html: Update the textarea size when switching to HTML view
[moodle.git] / lib / google / cache / Google_ApcCache.php
blob3523c98dccab3172b57a60fd3727432711454ebd
1 <?php
2 /*
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.
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.
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) {
36 return false;
38 return true;
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
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);
67 /**
68 * @inheritDoc
70 public function get($key, $expiration = false) {
72 if (($ret = @apc_fetch($key)) === false) {
73 return false;
75 if (!$expiration || (time() - $ret['time'] > $expiration)) {
76 $this->delete($key);
77 return false;
79 return unserialize($ret['data']);
82 /**
83 * @inheritDoc
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");
91 /**
92 * @inheritDoc
93 * @param String $key
95 public function delete($key) {
96 @apc_delete($key);