Permalink
Browse files

Add Carbon/Datetime argument to cache

  • Loading branch information...
lucasmichot committed Oct 24, 2013
1 parent 70b063b commit 977f803030b84ce28f034bcf925f93bfb029a0d4
Showing with 48 additions and 7 deletions.
  1. +46 −6 src/Illuminate/Cache/Repository.php
  2. +2 −1 src/Illuminate/Cache/composer.json
@@ -2,6 +2,8 @@
use Closure;
use ArrayAccess;
use DateTime;
use Carbon\Carbon;
class Repository implements ArrayAccess {
@@ -54,12 +56,27 @@ public function get($key, $default = null)
return ! is_null($value) ? $value : value($default);
}
/**
* Store an item in the cache.
*
* @param string $key
* @param mixed $value
* @param Carbon|Datetime|int $minutes
* @return void
*/
public function put($key, $value, $minutes)
{
$minutes = $this->getMinutes($minutes);
$this->store->put($key, $value, $minutes);
}
/**
* Store an item in the cache if the key does not exist.
*
* @param string $key
* @param mixed $value
* @param int $minutes
* @param string $key
* @param mixed $value
* @param Carbon|Datetime|int $minutes
* @return void
*/
public function add($key, $value, $minutes)
@@ -70,9 +87,9 @@ public function add($key, $value, $minutes)
/**
* Get an item from the cache, or store the default value.
*
* @param string $key
* @param int $minutes
* @param Closure $callback
* @param string $key
* @param Carbon|Datetime|int $minutes
* @param Closure $callback
* @return mixed
*/
public function remember($key, $minutes, Closure $callback)
@@ -200,6 +217,29 @@ public function offsetUnset($key)
return $this->forget($key);
}
/**
* Calculate the number of minutes with the given duration.
*
* @param Carbon|DateTime|int $duration
* @return int
*/
protected function getMinutes($duration)
{
if ($duration instanceof DateTime)
{
$duration = Carbon::instance($duration);
}
if ($duration instanceof Carbon)
{
return max(0, Carbon::now()->diffInMinutes(false));
}
else
{
return intval($duration);
}
}
/**
* Dynamically pass missing methods to the store.
*
@@ -9,7 +9,8 @@
],
"require": {
"php": ">=5.3.0",
"illuminate/support": "4.1.x"
"illuminate/support": "4.1.x",
"nesbot/carbon": "1.*"
},
"require-dev": {
"phpunit/phpunit": "3.7.*",

0 comments on commit 977f803

Please sign in to comment.