Skip to content

Commit

Permalink
send unix timestamp to memcached
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Sep 21, 2016
1 parent d145a84 commit c5984af
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
18 changes: 15 additions & 3 deletions src/Illuminate/Cache/MemcachedStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Cache;

use Memcached;
use Carbon\Carbon;
use Illuminate\Contracts\Cache\Store;

class MemcachedStore extends TaggableStore implements Store
Expand Down Expand Up @@ -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));
}

/**
Expand All @@ -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));
}

/**
Expand All @@ -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));
}

/**
Expand Down Expand Up @@ -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.
*
Expand Down
4 changes: 3 additions & 1 deletion tests/Cache/CacheMemcachedStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit c5984af

Please sign in to comment.