Files
spa-api/laravel/cache/driver.php

100 lines
2.3 KiB
PHP
Raw Normal View History

2011-08-18 19:56:29 -05:00
<?php namespace Laravel\Cache;
2011-09-08 17:49:16 -05:00
use Closure;
2011-08-18 19:56:29 -05:00
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>
* // Retrieve an item from the cache
* $name = Cache::get('name');
*
* // Retrieve an item from the cache and return a default value if it doesn't exist
* $name = Cache::get('name', 'Fred');
* </code>
*
2011-08-18 19:56:29 -05:00
* @param string $key
* @param mixed $default
* @param string $driver
* @return mixed
*/
2011-08-19 20:12:39 -05:00
public function get($key, $default = null)
{
if ( ! is_null($item = $this->retrieve($key))) return $item;
2011-09-08 17:49:16 -05:00
return ($default instanceof Closure) ? call_user_func($default) : $default;
2011-08-19 20:12:39 -05:00
}
2011-08-18 19:56:29 -05:00
/**
2011-08-19 20:12:39 -05:00
* Retrieve an item from the cache driver.
2011-08-18 19:56:29 -05:00
*
2011-08-19 20:12:39 -05:00
* @param string $key
2011-08-18 19:56:29 -05:00
* @return mixed
*/
2011-08-19 20:12:39 -05:00
abstract protected function retrieve($key);
2011-08-18 19:56:29 -05:00
/**
2011-08-19 20:12:39 -05:00
* Write an item to the cache for a given number of minutes.
*
* <code>
* // Store an item in the cache for 5 minutes
* Cache::put('name', 'Fred', 5);
* </code>
*
2011-08-18 19:56:29 -05:00
* @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 an item from the cache and store the default value if it doesn't exist
* Cache::remember('name', 'Fred', 5);
*
* // Closures may also be used to defer retrieval of the default value
* Cache::remember('users', function() {return DB::table('users')->get();}, 5);
* </code>
*
2011-08-18 19:56:29 -05:00
* @param string $key
* @param mixed $default
* @param int $minutes
* @return mixed
*/
public function remember($key, $value, $minutes)
{
2011-08-19 20:12:39 -05:00
if ( ! is_null($item = $this->get($key, null))) return $item;
2011-08-18 19:56:29 -05:00
2011-09-08 17:49:16 -05:00
$default = ($default instanceof Closure) ? call_user_func($default) : $default;
2011-08-18 19:56:29 -05:00
$this->put($key, $default, $minutes);
return $default;
}
/**
* Delete an item from the cache.
*
* @param string $key
* @return void
*/
abstract public function forget($key);
}