diff --git a/src/Illuminate/Cache/MemcachedStore.php b/src/Illuminate/Cache/MemcachedStore.php index c1e7cefb4551..23291099c795 100755 --- a/src/Illuminate/Cache/MemcachedStore.php +++ b/src/Illuminate/Cache/MemcachedStore.php @@ -3,6 +3,7 @@ namespace Illuminate\Cache; use Memcached; +use Carbon\Carbon; use Illuminate\Contracts\Cache\Store; class MemcachedStore extends TaggableStore implements Store @@ -82,7 +83,7 @@ public function many(array $keys) */ public function put($key, $value, $minutes) { - $this->memcached->set($this->prefix.$key, $value, (int) ($minutes * 60)); + $this->memcached->set($this->prefix.$key, $value, $this->toTimestamp($minutes)); } /** @@ -100,7 +101,7 @@ public function putMany(array $values, $minutes) $prefixedValues[$this->prefix.$key] = $value; } - $this->memcached->setMulti($prefixedValues, (int) ($minutes * 60)); + $this->memcached->setMulti($prefixedValues, $this->toTimestamp($minutes)); } /** @@ -113,7 +114,7 @@ public function putMany(array $values, $minutes) */ public function add($key, $value, $minutes) { - return $this->memcached->add($this->prefix.$key, $value, (int) ($minutes * 60)); + return $this->memcached->add($this->prefix.$key, $value, $this->toTimestamp($minutes)); } /** @@ -173,6 +174,17 @@ public function flush() $this->memcached->flush(); } + /** + * Get the UNIX timestamp for the given number of minutes. + * + * @parma int $minutes + * @return int + */ + protected function toTimestamp($minutes) + { + return $minutes > 0 ? Carbon::now()->timestamp + ((int) $minutes * 60) : 0; + } + /** * Get the underlying Memcached connection. * diff --git a/tests/Cache/CacheMemcachedStoreTest.php b/tests/Cache/CacheMemcachedStoreTest.php index 92404b0395dc..944a11140d8f 100755 --- a/tests/Cache/CacheMemcachedStoreTest.php +++ b/tests/Cache/CacheMemcachedStoreTest.php @@ -49,10 +49,12 @@ public function testSetMethodProperlyCallsMemcache() $this->markTestSkipped('Memcached module not installed'); } + Carbon\Carbon::setTestNow($now = Carbon\Carbon::now()); $memcache = $this->getMockBuilder('Memcached')->setMethods(['set'])->getMock(); - $memcache->expects($this->once())->method('set')->with($this->equalTo('foo'), $this->equalTo('bar'), $this->equalTo(60)); + $memcache->expects($this->once())->method('set')->with($this->equalTo('foo'), $this->equalTo('bar'), $this->equalTo($now->timestamp + 60)); $store = new Illuminate\Cache\MemcachedStore($memcache); $store->put('foo', 'bar', 1); + Carbon\Carbon::setTestNow(); } public function testIncrementMethodProperlyCallsMemcache()