MDL-51531 libraries: Upgrade google libraries to 1.1.5
[moodle.git] / lib / google / src / Google / Cache / File.php
blob30cbeab83733ec8e111fea4ebc4d6e75af77d621
1 <?php
2 /*
3 * Copyright 2008 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 if (!class_exists('Google_Client')) {
19 require_once dirname(__FILE__) . '/../autoload.php';
23 * This class implements a basic on disk storage. While that does
24 * work quite well it's not the most elegant and scalable solution.
25 * It will also get you into a heap of trouble when you try to run
26 * this in a clustered environment.
28 * @author Chris Chabot <chabotc@google.com>
30 class Google_Cache_File extends Google_Cache_Abstract
32 const MAX_LOCK_RETRIES = 10;
33 private $path;
34 private $fh;
36 /**
37 * @var Google_Client the current client
39 private $client;
41 public function __construct(Google_Client $client)
43 $this->client = $client;
44 $this->path = $this->client->getClassConfig($this, 'directory');
47 public function get($key, $expiration = false)
49 $storageFile = $this->getCacheFile($key);
50 $data = false;
52 if (!file_exists($storageFile)) {
53 $this->client->getLogger()->debug(
54 'File cache miss',
55 array('key' => $key, 'file' => $storageFile)
57 return false;
60 if ($expiration) {
61 $mtime = filemtime($storageFile);
62 if ((time() - $mtime) >= $expiration) {
63 $this->client->getLogger()->debug(
64 'File cache miss (expired)',
65 array('key' => $key, 'file' => $storageFile)
67 $this->delete($key);
68 return false;
72 if ($this->acquireReadLock($storageFile)) {
73 if (filesize($storageFile) > 0) {
74 $data = fread($this->fh, filesize($storageFile));
75 $data = unserialize($data);
76 } else {
77 $this->client->getLogger()->debug(
78 'Cache file was empty',
79 array('file' => $storageFile)
82 $this->unlock($storageFile);
85 $this->client->getLogger()->debug(
86 'File cache hit',
87 array('key' => $key, 'file' => $storageFile, 'var' => $data)
90 return $data;
93 public function set($key, $value)
95 $storageFile = $this->getWriteableCacheFile($key);
96 if ($this->acquireWriteLock($storageFile)) {
97 // We serialize the whole request object, since we don't only want the
98 // responseContent but also the postBody used, headers, size, etc.
99 $data = serialize($value);
100 $result = fwrite($this->fh, $data);
101 $this->unlock($storageFile);
103 $this->client->getLogger()->debug(
104 'File cache set',
105 array('key' => $key, 'file' => $storageFile, 'var' => $value)
107 } else {
108 $this->client->getLogger()->notice(
109 'File cache set failed',
110 array('key' => $key, 'file' => $storageFile)
115 public function delete($key)
117 $file = $this->getCacheFile($key);
118 if (file_exists($file) && !unlink($file)) {
119 $this->client->getLogger()->error(
120 'File cache delete failed',
121 array('key' => $key, 'file' => $file)
123 throw new Google_Cache_Exception("Cache file could not be deleted");
126 $this->client->getLogger()->debug(
127 'File cache delete',
128 array('key' => $key, 'file' => $file)
132 private function getWriteableCacheFile($file)
134 return $this->getCacheFile($file, true);
137 private function getCacheFile($file, $forWrite = false)
139 return $this->getCacheDir($file, $forWrite) . '/' . md5($file);
142 private function getCacheDir($file, $forWrite)
144 // use the first 2 characters of the hash as a directory prefix
145 // this should prevent slowdowns due to huge directory listings
146 // and thus give some basic amount of scalability
147 $storageDir = $this->path . '/' . substr(md5($file), 0, 2);
148 if ($forWrite && ! is_dir($storageDir)) {
149 if (! mkdir($storageDir, 0755, true)) {
150 $this->client->getLogger()->error(
151 'File cache creation failed',
152 array('dir' => $storageDir)
154 throw new Google_Cache_Exception("Could not create storage directory: $storageDir");
157 return $storageDir;
160 private function acquireReadLock($storageFile)
162 return $this->acquireLock(LOCK_SH, $storageFile);
165 private function acquireWriteLock($storageFile)
167 $rc = $this->acquireLock(LOCK_EX, $storageFile);
168 if (!$rc) {
169 $this->client->getLogger()->notice(
170 'File cache write lock failed',
171 array('file' => $storageFile)
173 $this->delete($storageFile);
175 return $rc;
178 private function acquireLock($type, $storageFile)
180 $mode = $type == LOCK_EX ? "w" : "r";
181 $this->fh = fopen($storageFile, $mode);
182 if (!$this->fh) {
183 $this->client->getLogger()->error(
184 'Failed to open file during lock acquisition',
185 array('file' => $storageFile)
187 return false;
189 $count = 0;
190 while (!flock($this->fh, $type | LOCK_NB)) {
191 // Sleep for 10ms.
192 usleep(10000);
193 if (++$count < self::MAX_LOCK_RETRIES) {
194 return false;
197 return true;
200 public function unlock($storageFile)
202 if ($this->fh) {
203 flock($this->fh, LOCK_UN);