1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | <?php /** * Class to store arbitrary data objects in smarty cache * * @author Christopher Han <xiphux@gmail.com> * @copyright Copyright (c) 2010 Christopher Han * @package GitPHP * @subpackage Cache */ class GitPHP_Cache { /** * Cache strategy * * @var GitPHP_CacheStrategy_Interface */ protected $strategy; /** * Cache lifetime in seconds * * @var int */ protected $lifetime = 86400; /** * Constructor * * @param GitPHP_CacheStrategy_Interface $strategy cache strategy */ public function __construct(GitPHP_CacheStrategy_Interface $strategy) { if (!$strategy) throw new Exception('Cache strategy is required'); $this->SetStrategy($strategy); } /** * Set the cache strategy * * @param GitPHP_CacheStrategy_Interface $strategy cache strategy */ public function SetStrategy(GitPHP_CacheStrategy_Interface $strategy) { if (!$strategy) return; $this->strategy = $strategy; } /** * Gets the cache lifetime * * @return int cache lifetime in seconds */ public function GetLifetime() { return $this->lifetime; } /** * Sets the cache lifetime * * @param int $lifetime cache lifetime in seconds */ public function SetLifetime($lifetime) { if (!is_int($lifetime)) return; $this->lifetime = $lifetime; } /** * Get an item from the cache * * @param string $key cache key * @return mixed the cached object, or false */ public function Get($key) { if (empty($key)) return false; return $this->strategy->Get($key); } /** * Set an item in the cache * * @param string $key cache key * @param mixed $value value * @param int $lifetime override the lifetime for this data */ public function Set($key, $value, $lifetime = null) { if (empty($key) || empty($value)) return; if ($lifetime === null) $lifetime = $this->lifetime; $this->strategy->Set($key, $value, $lifetime); } /** * Tests if a key is cached * * @param string $key cache key * @return boolean true if cached, false otherwise */ public function Exists($key) { if (empty($key)) return false; return $this->strategy->Exists($key); } /** * Delete an item from the cache * * @param string $key cache key */ public function Delete($key) { if (empty($key)) return; $this->strategy->Delete($key); } /** * Clear the cache */ public function Clear() { $this->strategy->Clear(); } } |