more refactoring on the framework.

This commit is contained in:
Taylor Otwell
2011-09-14 20:16:13 -05:00
parent c576c388b9
commit 26a66027f8
14 changed files with 143 additions and 193 deletions

80
laravel/cache/drivers/apc.php vendored Normal file
View File

@@ -0,0 +1,80 @@
<?php namespace Laravel\Cache\Drivers;
use Laravel\Proxy;
class APC extends Driver {
/**
* The proxy class instance.
*
* @var Proxy
*/
private $apc;
/**
* The cache key from the cache configuration file.
*
* @var string
*/
private $key;
/**
* Create a new APC cache driver instance.
*
* @param Proxy $proxy
* @param string $key
* @return void
*/
public function __construct(Proxy $apc, $key)
{
$this->key = $key;
$this->proxy = $proxy;
}
/**
* Determine if an item exists in the cache.
*
* @param string $key
* @return bool
*/
public function has($key)
{
return ( ! is_null($this->get($key)));
}
/**
* Retrieve an item from the cache driver.
*
* @param string $key
* @return mixed
*/
protected function retrieve($key)
{
if ( ! is_null($cache = $this->proxy->apc_fetch($this->key.$key))) return $cache;
}
/**
* Write an item to the cache for a given number of minutes.
*
* @param string $key
* @param mixed $value
* @param int $minutes
* @return void
*/
public function put($key, $value, $minutes)
{
$this->proxy->apc_store($this->key.$key, $value, $minutes * 60);
}
/**
* Delete an item from the cache.
*
* @param string $key
* @return void
*/
public function forget($key)
{
$this->proxy->apc_delete($this->key.$key);
}
}

79
laravel/cache/drivers/driver.php vendored Normal file
View File

@@ -0,0 +1,79 @@
<?php namespace Laravel\Cache\Drivers;
use Closure;
abstract class Driver {
/**
* Determine if an item exists in the cache.
*
* @param string $key
* @return bool
*/
abstract public function has($key);
/**
* Get an item from the cache.
*
* A default value may also be specified, and will be returned in the requested
* item does not exist in the cache.
*
* @param string $key
* @param mixed $default
* @param string $driver
* @return mixed
*/
public function get($key, $default = null)
{
if ( ! is_null($item = $this->retrieve($key))) return $item;
return ($default instanceof Closure) ? call_user_func($default) : $default;
}
/**
* Retrieve an item from the cache driver.
*
* @param string $key
* @return mixed
*/
abstract protected function retrieve($key);
/**
* Write an item to the cache for a given number of minutes.
*
* @param string $key
* @param mixed $value
* @param int $minutes
* @return void
*/
abstract public function put($key, $value, $minutes);
/**
* Get an item from the cache. If the item doesn't exist in the cache, store
* the default value in the cache and return it.
*
* @param string $key
* @param mixed $default
* @param int $minutes
* @return mixed
*/
public function remember($key, $value, $minutes)
{
if ( ! is_null($item = $this->get($key, null))) return $item;
$default = ($default instanceof Closure) ? call_user_func($default) : $default;
$this->put($key, $default, $minutes);
return $default;
}
/**
* Delete an item from the cache.
*
* @param string $key
* @return void
*/
abstract public function forget($key);
}

85
laravel/cache/drivers/file.php vendored Normal file
View File

@@ -0,0 +1,85 @@
<?php namespace Laravel\Cache\Drivers;
class File extends Driver {
/**
* The file engine instance.
*
* @var Laravel\File
*/
private $file;
/**
* The path to which the cache files should be written.
*
* @var string
*/
private $path;
/**
* Create a new File cache driver instance.
*
* @param Laravel\File $file
* @param string $path
* @return void
*/
public function __construct(\Laravel\File $file, $path)
{
$this->file = $file;
$this->path = $path;
}
/**
* Determine if an item exists in the cache.
*
* @param string $key
* @return bool
*/
public function has($key)
{
return ( ! is_null($this->get($key)));
}
/**
* Retrieve an item from the cache driver.
*
* @param string $key
* @return mixed
*/
protected function retrieve($key)
{
if ( ! $this->file->exists($this->path.$key)) return null;
if (time() >= substr($cache = $this->file->get($this->path.$key), 0, 10))
{
return $this->forget($key);
}
return unserialize(substr($cache, 10));
}
/**
* Write an item to the cache for a given number of minutes.
*
* @param string $key
* @param mixed $value
* @param int $minutes
* @return void
*/
public function put($key, $value, $minutes)
{
$this->file->put($this->path.$key, (time() + ($minutes * 60)).serialize($value));
}
/**
* Delete an item from the cache.
*
* @param string $key
* @return void
*/
public function forget($key)
{
$this->file->delete($this->path.$key);
}
}

80
laravel/cache/drivers/memcached.php vendored Normal file
View File

@@ -0,0 +1,80 @@
<?php namespace Laravel\Cache\Drivers;
use Memcache;
use Laravel\Config;
class Memcached extends Driver {
/**
* The Memcache instance.
*
* @var Memcache
*/
private $memcache;
/**
* The cache key from the cache configuration file.
*
* @var string
*/
private $key;
/**
* Create a new Memcached cache driver instance.
*
* @param Memcache $memcache
* @return void
*/
public function __construct(Memcache $memcache, $key)
{
$this->key = $key;
$this->memcache = $memcache;
}
/**
* Determine if an item exists in the cache.
*
* @param string $key
* @return bool
*/
public function has($key)
{
return ( ! is_null($this->get($key)));
}
/**
* Retrieve an item from the cache driver.
*
* @param string $key
* @return mixed
*/
protected function retrieve($key)
{
if (($cache = $this->memcache->get($this->key.$key)) !== false) return $cache;
}
/**
* Write an item to the cache for a given number of minutes.
*
* @param string $key
* @param mixed $value
* @param int $minutes
* @return void
*/
public function put($key, $value, $minutes)
{
$this->memcache->set($this->key.$key, $value, 0, $minutes * 60);
}
/**
* Delete an item from the cache.
*
* @param string $key
* @return void
*/
public function forget($key)
{
$this->memcache->delete($this->key.$key);
}
}