123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- <?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();
- }
- }
|