first commit of 2.0
This commit is contained in:
29
laravel/cache/apc.php
vendored
Normal file
29
laravel/cache/apc.php
vendored
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php namespace Laravel\Cache;
|
||||
|
||||
use Laravel\Config;
|
||||
|
||||
class APC extends Driver {
|
||||
|
||||
public function has($key)
|
||||
{
|
||||
return ( ! is_null($this->get($key)));
|
||||
}
|
||||
|
||||
public function get($key, $default = null)
|
||||
{
|
||||
$item = ( ! is_null($cache = apc_fetch(Config::get('cache.key').$key))) ? $cache : null;
|
||||
|
||||
return $this->prepare($item, $default);
|
||||
}
|
||||
|
||||
public function put($key, $value, $minutes)
|
||||
{
|
||||
apc_store(Config::get('cache.key').$key, $value, $minutes * 60);
|
||||
}
|
||||
|
||||
public function forget($key)
|
||||
{
|
||||
apc_delete(Config::get('cache.key').$key);
|
||||
}
|
||||
|
||||
}
|
||||
93
laravel/cache/driver.php
vendored
Normal file
93
laravel/cache/driver.php
vendored
Normal file
@@ -0,0 +1,93 @@
|
||||
<?php namespace Laravel\Cache;
|
||||
|
||||
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.
|
||||
*
|
||||
* <code>
|
||||
* // Get the "name" item from the cache
|
||||
* $name = Cache::driver()->get('name');
|
||||
*
|
||||
* // Get the "name" item from the cache or return "Fred"
|
||||
* $name = Cache::driver()->get('name', 'Fred');
|
||||
* </code>
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed $default
|
||||
* @param string $driver
|
||||
* @return mixed
|
||||
*/
|
||||
abstract public function get($key, $default = null);
|
||||
|
||||
/**
|
||||
* Prepare the cache item for returning to the requestor.
|
||||
*
|
||||
* If the item is NULL, the default will be returned.
|
||||
*
|
||||
* @param mixed $item
|
||||
* @param mixed $default
|
||||
* @return mixed
|
||||
*/
|
||||
protected function prepare($item, $default)
|
||||
{
|
||||
if ( ! is_null($item)) return $item;
|
||||
|
||||
return (is_callable($default)) ? call_user_func($default) : $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write an item to the cache.
|
||||
*
|
||||
* @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.
|
||||
*
|
||||
* <code>
|
||||
* // Get the "name" item from the cache or store "Fred" for 30 minutes
|
||||
* $name = Cache::driver()->remember('name', 'Fred', 30);
|
||||
* </code>
|
||||
*
|
||||
* @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, $driver))) return $item;
|
||||
|
||||
$default = is_callable($default) ? 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);
|
||||
|
||||
}
|
||||
41
laravel/cache/file.php
vendored
Normal file
41
laravel/cache/file.php
vendored
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php namespace Laravel\Cache;
|
||||
|
||||
class File extends Driver {
|
||||
|
||||
public function has($key)
|
||||
{
|
||||
return ( ! is_null($this->get($key)));
|
||||
}
|
||||
|
||||
public function get($key, $default = null)
|
||||
{
|
||||
if ( ! file_exists(CACHE_PATH.$key))
|
||||
{
|
||||
return $this->prepare(null, $default);
|
||||
}
|
||||
|
||||
$cache = file_get_contents(CACHE_PATH.$key);
|
||||
|
||||
// The cache expiration date is stored as a UNIX timestamp at the beginning
|
||||
// of the cache file. We'll extract it out and check it here.
|
||||
if (time() >= substr($cache, 0, 10))
|
||||
{
|
||||
$this->forget($key);
|
||||
|
||||
return $this->prepare(null, $default);
|
||||
}
|
||||
|
||||
return $this->prepare(unserialize(substr($cache, 10)), $default);
|
||||
}
|
||||
|
||||
public function put($key, $value, $minutes)
|
||||
{
|
||||
file_put_contents(CACHE_PATH.$key, (time() + ($minutes * 60)).serialize($value), LOCK_EX);
|
||||
}
|
||||
|
||||
public function forget($key)
|
||||
{
|
||||
@unlink(CACHE_PATH.$key);
|
||||
}
|
||||
|
||||
}
|
||||
30
laravel/cache/memcached.php
vendored
Normal file
30
laravel/cache/memcached.php
vendored
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php namespace Laravel\Cache;
|
||||
|
||||
use Laravel\Config;
|
||||
use Laravel\Memcached as Mem;
|
||||
|
||||
class Memcached extends Driver {
|
||||
|
||||
public function has($key)
|
||||
{
|
||||
return ( ! is_null($this->get($key)));
|
||||
}
|
||||
|
||||
public function get($key, $default = null)
|
||||
{
|
||||
$item = (($cache = Mem::instance()->get(Config::get('cache.key').$key)) !== false) ? $cache : null;
|
||||
|
||||
return $this->prepare($item, $default);
|
||||
}
|
||||
|
||||
public function put($key, $value, $minutes)
|
||||
{
|
||||
Mem::instance()->set(Config::get('cache.key').$key, $value, 0, $minutes * 60);
|
||||
}
|
||||
|
||||
public function forget($key)
|
||||
{
|
||||
Mem::instance()->delete(Config::get('cache.key').$key);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user