]> Tony Duckles's Git Repositories (git.nynim.org) - delicious-utils.git/blob - delicious-dump/php-delicious/cache.inc.php
Initial commit
[delicious-utils.git] / delicious-dump / php-delicious / cache.inc.php
1 <?php
2 /***************************************************************/
3 /* Cache - part of the PhpDelicious library
4
5 Software License Agreement (BSD License)
6
7 Copyright (C) 2005-2008, Edward Eliot.
8 All rights reserved.
9
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions are met:
12
13 * Redistributions of source code must retain the above copyright
14 notice, this list of conditions and the following disclaimer.
15 * Redistributions in binary form must reproduce the above copyright
16 notice, this list of conditions and the following disclaimer in the
17 documentation and/or other materials provided with the distribution.
18 * Neither the name of Edward Eliot nor the names of its contributors
19 may be used to endorse or promote products derived from this software
20 without specific prior written permission of Edward Eliot.
21
22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS" AND ANY
23 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY
26 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
33 Last Updated: 7th April 2008 */
34 /***************************************************************/
35
36 class Cache {
37 protected $sFile;
38 protected $sFileLock;
39 protected $iCacheTime;
40
41 protected $sShortKey;
42 protected static $aCache = array();
43
44 public function __construct($sKey, $iCacheTime, $sPrefix='', $sCachePath = CACHE_PATH) {
45 $this->sShortKey = $sPrefix.md5($sKey);
46 $this->sFile = "$sCachePath$this->sShortKey.txt";
47 $this->sFileLock = "$this->sFile.lock";
48 $this->iCacheTime = $iCacheTime;
49 }
50
51 public function Check() {
52 if (array_key_exists($this->sShortKey, self::$aCache) || file_exists($this->sFileLock)) {
53 return true;
54 }
55 return (file_exists($this->sFile) && ($this->iCacheTime == -1 || time() - filemtime($this->sFile) <= $this->iCacheTime));
56 }
57
58 public function Exists() {
59 return (array_key_exists($this->sShortKey, self::$aCache)) || (file_exists($this->sFile) || file_exists($this->sFileLock));
60 }
61
62 public function Set($vContents) {
63 if (!file_exists($this->sFileLock)) {
64 if (file_exists($this->sFile)) {
65 copy($this->sFile, $this->sFileLock);
66 }
67 $oFile = fopen($this->sFile, 'w');
68 fwrite($oFile, serialize($vContents));
69 fclose($oFile);
70 if (file_exists($this->sFileLock)) {
71 unlink($this->sFileLock);
72 }
73 self::$aCache[$this->sShortKey] = $vContents;
74 return true;
75 }
76 return false;
77 }
78
79 public function Get() {
80 if (array_key_exists($this->sShortKey, self::$aCache)) {
81 return self::$aCache[$this->sShortKey];
82 } else if (file_exists($this->sFileLock)) {
83 self::$aCache[$this->sShortKey] = unserialize(file_get_contents($this->sFileLock));
84 return self::$aCache[$this->sShortKey];
85 } else {
86 self::$aCache[$this->sShortKey] = unserialize(file_get_contents($this->sFile));
87 return self::$aCache[$this->sShortKey];
88 }
89 }
90
91 public function ReValidate() {
92 touch($this->sFile);
93 }
94 }
95 ?>