merged skunkworks into develop.

This commit is contained in:
Taylor Otwell
2012-01-16 13:59:24 -06:00
parent 610d8827c4
commit b5442c67fc
117 changed files with 7268 additions and 3999 deletions

113
laravel/cache/drivers/database.php vendored Normal file
View File

@@ -0,0 +1,113 @@
<?php namespace Laravel\Cache\Drivers;
use Laravel\Config;
use Laravel\Database as DB;
use Laravel\Database\Connection;
class Database extends Driver {
/**
* The cache key from the cache configuration file.
*
* @var string
*/
protected $key;
/**
* Create a new database cache driver instance.
*
* @param string $key
* @return void
*/
public function __construct($key)
{
$this->key = $key;
}
/**
* 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)
{
$cache = $this->table()->where('key', '=', $this->key.$key)->first();
if ( ! is_null($cache))
{
if (time() >= $cache->expiration) return $this->forget($key);
return unserialize($cache->value);
}
}
/**
* Write an item to the cache for a given number of minutes.
*
* <code>
* // Put an item in the cache for 15 minutes
* Cache::put('name', 'Taylor', 15);
* </code>
*
* @param string $key
* @param mixed $value
* @param int $minutes
* @return void
*/
public function put($key, $value, $minutes)
{
$key = $this->key.$key;
$value = serialize($value);
$expiration = $this->expiration($minutes);
// To update the value, we'll first attempt an insert against the
// database and if we catch an exception, we'll assume that the
// primary key already exists in the table and update.
try
{
$this->table()->insert(compact('key', 'value', 'expiration'));
}
catch (\Exception $e)
{
$this->table()->where('key', '=', $key)->update(compact('value', 'expiration'));
}
}
/**
* Delete an item from the cache.
*
* @param string $key
* @return void
*/
public function forget($key)
{
$this->table()->where('key', '=', $this->key.$key)->delete();
}
/**
* Get a query builder for the database table.
*
* @return Query
*/
protected function table()
{
$connection = DB::connection(Config::get('cache.database.connection'));
return $connection->table(Config::get('cache.database.table'));
}
}

View File

@@ -28,9 +28,7 @@ abstract class Driver {
*/
public function get($key, $default = null)
{
if ( ! is_null($item = $this->retrieve($key))) return $item;
return ($default instanceof Closure) ? call_user_func($default) : $default;
return ( ! is_null($item = $this->retrieve($key))) ? $item : value($default);
}
/**
@@ -57,8 +55,7 @@ abstract class Driver {
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.
* Get an item from the cache, or cache and return the default value.
*
* <code>
* // Get an item from the cache, or cache a value for 15 minutes
@@ -77,9 +74,7 @@ abstract class Driver {
{
if ( ! is_null($item = $this->get($key, null))) return $item;
$default = ($default instanceof Closure) ? call_user_func($default) : $default;
$this->put($key, $default, $minutes);
$this->put($key, value($default), $minutes);
return $default;
}
@@ -92,4 +87,15 @@ abstract class Driver {
*/
abstract public function forget($key);
/**
* Get the expiration time as a UNIX timestamp.
*
* @param int $minutes
* @return int
*/
protected function expiration($minutes)
{
return time() + ($minutes * 60);
}
}

View File

@@ -68,7 +68,7 @@ class File extends Driver {
*/
public function put($key, $value, $minutes)
{
$value = (time() + ($minutes * 60)).serialize($value);
$value = $this->expiration($minutes).serialize($value);
file_put_contents($this->path.$key, $value, LOCK_EX);
}
@@ -81,10 +81,7 @@ class File extends Driver {
*/
public function forget($key)
{
if (file_exists($this->path.$key))
{
@unlink($this->path.$key);
}
if (file_exists($this->path.$key)) @unlink($this->path.$key);
}
}

View File

@@ -1,91 +0,0 @@
<?php namespace Laravel\Cache;
use Laravel\Redis;
use Laravel\Config;
use Laravel\Memcached;
class Manager {
/**
* All of the active cache drivers.
*
* @var array
*/
protected static $drivers = array();
/**
* Get a cache driver instance.
*
* If no driver name is specified, the default cache driver will
* be returned as defined in the cache configuration file.
*
* <code>
* // Get the default cache driver instance
* $driver = Cache::driver();
*
* // Get a specific cache driver instance by name
* $driver = Cache::driver('memcached');
* </code>
*
* @param string $driver
* @return Cache\Driver
*/
public static function driver($driver = null)
{
if (is_null($driver)) $driver = Config::get('cache.driver');
if ( ! array_key_exists($driver, static::$drivers))
{
return static::$drivers[$driver] = static::factory($driver);
}
return static::$drivers[$driver];
}
/**
* Create a new cache driver instance.
*
* @param string $driver
* @return Driver
*/
protected static function factory($driver)
{
switch ($driver)
{
case 'apc':
return new Drivers\APC(Config::get('cache.key'));
case 'file':
return new Drivers\File(CACHE_PATH);
case 'memcached':
return new Drivers\Memcached(Memcached::instance(), Config::get('cache.key'));
case 'redis':
return new Drivers\Redis(Redis::db());
default:
throw new \DomainException("Cache driver {$driver} is not supported.");
}
}
/**
* Pass all other methods to the default cache driver.
*
* Passing method calls to the driver instance provides a convenient API
* for the developer when always using the default cache driver.
*
* <code>
* // Call the "get" method on the default driver
* $name = Cache::get('name');
*
* // Call the "put" method on the default driver
* Cache::put('name', 'Taylor', 15);
* </code>
*/
public static function __callStatic($method, $parameters)
{
return call_user_func_array(array(static::driver(), $method), $parameters);
}
}