Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/Illuminate/Cache/MemcachedStore.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?php namespace Illuminate\Cache;

use Illuminate\Contracts\Cache\Store;
use Illuminate\Contracts\Cache\ThreadSafeStore;

class MemcachedStore extends TaggableStore implements Store {
class MemcachedStore extends TaggableStore implements ThreadSafeStore {

/**
* The Memcached instance.
Expand Down Expand Up @@ -60,6 +61,19 @@ public function put($key, $value, $minutes)
$this->memcached->set($this->prefix.$key, $value, $minutes * 60);
}

/**
* Store an item in the cache if the key does not exist for a given number of minutes.
*
* @param string $key
* @param mixed $value
* @param int $minutes
* @return bool
*/
public function add($key, $value, $minutes)
{
return $this->memcached->add($this->prefix.$key, $value, $minutes * 60);
}

/**
* Increment the value of an item in the cache.
*
Expand Down
20 changes: 19 additions & 1 deletion src/Illuminate/Cache/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use ArrayAccess;
use Carbon\Carbon;
use Illuminate\Contracts\Cache\Store;
use Illuminate\Contracts\Cache\ThreadSafeStore;
use Illuminate\Support\Traits\Macroable;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Cache\Repository as CacheContract;
Expand All @@ -18,7 +19,7 @@ class Repository implements CacheContract, ArrayAccess {
/**
* The cache store implementation.
*
* @var \Illuminate\Contracts\Cache\Store
* @var \Illuminate\Contracts\Cache\Store|\Illuminate\Contracts\Cache\ThreadSafeStore
*/
protected $store;

Expand All @@ -36,6 +37,13 @@ class Repository implements CacheContract, ArrayAccess {
*/
protected $default = 60;

/**
* Whether or not the cache store implements the \Illuminate\Contracts\Cache\ThreadSafeStore interface
*
* @var bool
*/
protected $isStoreThreadSafe = false;

/**
* Create a new cache repository instance.
*
Expand All @@ -44,6 +52,11 @@ class Repository implements CacheContract, ArrayAccess {
public function __construct(Store $store)
{
$this->store = $store;
if ($store instanceof ThreadSafeStore)
{
$this->isStoreThreadSafe = true;
}

}

/**
Expand Down Expand Up @@ -154,6 +167,11 @@ public function put($key, $value, $minutes)
*/
public function add($key, $value, $minutes)
{
if ($this->isStoreThreadSafe)
{
return $this->store->add($key, $value, $minutes);
}

if (is_null($this->get($key)))
{
$this->put($key, $value, $minutes); return true;
Expand Down
15 changes: 15 additions & 0 deletions src/Illuminate/Contracts/Cache/ThreadSafeStore.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php namespace Illuminate\Contracts\Cache;

interface ThreadSafeStore extends Store {

/**
* Store an item in the cache if the key does not exist for a given number of minutes.
*
* @param string $key
* @param mixed $value
* @param int $minutes
* @return bool
*/
public function add($key, $value, $minutes);

}