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;
37 * @var Google_Client the current 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);
52 if (!file_exists($storageFile)) {
53 $this->client
->getLogger()->debug(
55 array('key' => $key, 'file' => $storageFile)
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)
72 if ($this->acquireReadLock($storageFile)) {
73 if (filesize($storageFile) > 0) {
74 $data = fread($this->fh
, filesize($storageFile));
75 $data = unserialize($data);
77 $this->client
->getLogger()->debug(
78 'Cache file was empty',
79 array('file' => $storageFile)
82 $this->unlock($storageFile);
85 $this->client
->getLogger()->debug(
87 array('key' => $key, 'file' => $storageFile, 'var' => $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(
105 array('key' => $key, 'file' => $storageFile, 'var' => $value)
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(
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");
160 private function acquireReadLock($storageFile)
162 return $this->acquireLock(LOCK_SH
, $storageFile);
165 private function acquireWriteLock($storageFile)
167 $rc = $this->acquireLock(LOCK_EX
, $storageFile);
169 $this->client
->getLogger()->notice(
170 'File cache write lock failed',
171 array('file' => $storageFile)
173 $this->delete($storageFile);
178 private function acquireLock($type, $storageFile)
180 $mode = $type == LOCK_EX ?
"w" : "r";
181 $this->fh
= fopen($storageFile, $mode);
183 $this->client
->getLogger()->error(
184 'Failed to open file during lock acquisition',
185 array('file' => $storageFile)
190 while (!flock($this->fh
, $type | LOCK_NB
)) {
193 if (++
$count < self
::MAX_LOCK_RETRIES
) {
200 public function unlock($storageFile)
203 flock($this->fh
, LOCK_UN
);