Merge pull request #21073 from owncloud/memcache-lock-ttl

Add ttl for redis based locking
This commit is contained in:
Thomas Müller 2015-12-10 13:51:01 +01:00
commit 744ef6141b
5 changed files with 55 additions and 4 deletions

View File

@ -28,6 +28,8 @@ use OCP\Lock\ILockingProvider;
* to release any left over locks at the end of the request
*/
abstract class AbstractLockingProvider implements ILockingProvider {
const TTL = 3600; // how long until we clear stray locks in seconds
protected $acquiredLocks = [
'shared' => [],
'exclusive' => []

View File

@ -51,8 +51,6 @@ class DBLockingProvider extends AbstractLockingProvider {
private $sharedLocks = [];
const TTL = 3600; // how long until we clear stray locks in seconds
/**
* Check if we have an open shared lock for a path
*

View File

@ -21,6 +21,7 @@
namespace OC\Lock;
use OCP\IMemcacheTTL;
use OCP\Lock\LockedException;
use OCP\IMemcache;
@ -37,6 +38,12 @@ class MemcacheLockingProvider extends AbstractLockingProvider {
$this->memcache = $memcache;
}
private function setTTL($path) {
if ($this->memcache instanceof IMemcacheTTL) {
$this->memcache->setTTL($path, self::TTL);
}
}
/**
* @param string $path
* @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE
@ -69,6 +76,7 @@ class MemcacheLockingProvider extends AbstractLockingProvider {
throw new LockedException($path);
}
}
$this->setTTL($path);
$this->markAcquire($path, $type);
}
@ -106,6 +114,7 @@ class MemcacheLockingProvider extends AbstractLockingProvider {
throw new LockedException($path);
}
}
$this->setTTL($path);
$this->markChange($path, $targetType);
}
}

View File

@ -25,9 +25,9 @@
namespace OC\Memcache;
use OCP\IMemcache;
use OCP\IMemcacheTTL;
class Redis extends Cache implements IMemcache {
class Redis extends Cache implements IMemcacheTTL {
/**
* @var \Redis $cache
*/
@ -195,6 +195,10 @@ class Redis extends Cache implements IMemcache {
return false;
}
public function setTTL($key, $ttl) {
self::$cache->expire($this->getNamespace() . $key, $ttl);
}
static public function isAvailable() {
return extension_loaded('redis')
&& version_compare(phpversion('redis'), '2.2.5', '>=');

View File

@ -0,0 +1,38 @@
<?php
/**
* @author Robin Appelman <icewind@owncloud.com>
*
* @copyright Copyright (c) 2015, ownCloud, Inc.
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OCP;
/**
* Interface for memcache backends that support setting ttl after the value is set
*
* @since 9.0.0
*/
interface IMemcacheTTL extends IMemcache {
/**
* Set the ttl for an existing value
*
* @param string $key
* @param int $ttl time to live in seconds
* @since 9.0.0
*/
public function setTTL($key, $ttl);
}