SHINDIG-943 by Pan Jie - Add support invalidation for CacheApc/CacheFile/CacheMemcach...
[shindig.git] / php / src / common / sample / CacheStorageFile.php
bloba3dcfb2c12b62a8a38cd25f0b1c869222dd74339
1 <?php
2 /**
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
21 class CacheStorageFile extends CacheStorage {
22 private $prefix = null;
24 public function __construct($name) {
25 $this->prefix = $name;
28 public function store($key, $value) {
29 $cacheDir = CacheStorageFile::getCacheDir($key);
30 $cacheFile = CacheStorageFile::getCacheFile($key);
31 if (! is_dir($cacheDir)) {
32 if (! @mkdir($cacheDir, 0755, true)) {
33 throw new CacheException("Could not create cache directory");
37 return file_put_contents($cacheFile, $value);
40 public function fetch($key) {
41 $cacheFile = CacheStorageFile::getCacheFile($key);
42 if (File::exists($cacheFile) && File::readable($cacheFile)) {
43 return @file_get_contents($cacheFile);
45 return false;
48 public function delete($key) {
49 $cacheFile = CacheStorageFile::getCacheFile($key);
50 if (! @unlink($cacheFile)) {
51 throw new CacheException("Cache file could not be deleted");
55 public function isLocked($key) {
56 // our lock file convention is simple: /the/file/path.lock
57 clearstatcache();
58 return file_exists($key . '.lock');
61 public function lock($key) {
62 $cacheDir = CacheStorageFile::getCacheDir($key);
63 $cacheFile = CacheStorageFile::getCacheFile($key);
64 if (! is_dir($cacheDir)) {
65 if (! @mkdir($cacheDir, 0755, true)) {
66 // make sure the failure isn't because of a concurency issue
67 if (! is_dir($cacheDir)) {
68 throw new CacheException("Could not create cache directory");
72 @touch($cacheFile . '.lock');
75 public function unlock($key) {
76 // suppress all warnings, if some other process removed it that's ok too
77 $cacheFile = CacheStorageFile::getCacheFile($key);
78 @unlink($cacheFile . '.lock');
81 private function getCacheDir($key) {
82 // use the first 2 characters of the hash as a directory prefix
83 // this should prevent slowdowns due to huge directory listings
84 // and thus give some basic amount of scalability
85 return Config::get('cache_root') . '/' . $this->prefix . '/' .
86 substr($key, 0, 2);
89 private function getCacheFile($key) {
90 return $this->getCacheDir($key) . '/' . $key;