merged skunkworks into develop.
This commit is contained in:
113
laravel/cache/drivers/database.php
vendored
Normal file
113
laravel/cache/drivers/database.php
vendored
Normal 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'));
|
||||
}
|
||||
|
||||
}
|
||||
22
laravel/cache/drivers/driver.php
vendored
22
laravel/cache/drivers/driver.php
vendored
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
7
laravel/cache/drivers/file.php
vendored
7
laravel/cache/drivers/file.php
vendored
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user