cache swift tokens in memcache
Signed-off-by: Robin Appelman <robin@icewind.nl>
This commit is contained in:
parent
de83bda777
commit
205d5586e8
1 changed files with 36 additions and 10 deletions
|
@ -31,6 +31,7 @@ use OCP\Files\StorageNotAvailableException;
|
||||||
use OpenCloud\Common\Exceptions\EndpointError;
|
use OpenCloud\Common\Exceptions\EndpointError;
|
||||||
use OpenCloud\Common\Service\Catalog;
|
use OpenCloud\Common\Service\Catalog;
|
||||||
use OpenCloud\Common\Service\CatalogItem;
|
use OpenCloud\Common\Service\CatalogItem;
|
||||||
|
use OpenCloud\Identity\Resource\Token;
|
||||||
use OpenCloud\ObjectStore\Service;
|
use OpenCloud\ObjectStore\Service;
|
||||||
use OpenCloud\OpenStack;
|
use OpenCloud\OpenStack;
|
||||||
use OpenCloud\Rackspace;
|
use OpenCloud\Rackspace;
|
||||||
|
@ -57,6 +58,8 @@ class Swift implements IObjectStore {
|
||||||
*/
|
*/
|
||||||
private $container;
|
private $container;
|
||||||
|
|
||||||
|
private $memcache;
|
||||||
|
|
||||||
public function __construct($params) {
|
public function __construct($params) {
|
||||||
if (isset($params['bucket'])) {
|
if (isset($params['bucket'])) {
|
||||||
$params['container'] = $params['bucket'];
|
$params['container'] = $params['bucket'];
|
||||||
|
@ -71,9 +74,15 @@ class Swift implements IObjectStore {
|
||||||
|
|
||||||
if (isset($params['apiKey'])) {
|
if (isset($params['apiKey'])) {
|
||||||
$this->client = new Rackspace($params['url'], $params);
|
$this->client = new Rackspace($params['url'], $params);
|
||||||
|
$cacheKey = $this->params['username'] . '@' . $this->params['url'] . '/' . $this->params['bucket'];
|
||||||
} else {
|
} else {
|
||||||
$this->client = new OpenStack($params['url'], $params);
|
$this->client = new OpenStack($params['url'], $params);
|
||||||
|
$cacheKey = $this->params['username'] . '@' . $this->params['url'] . '/' . $this->params['bucket'];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$cacheFactory = \OC::$server->getMemCacheFactory();
|
||||||
|
$this->memcache = $cacheFactory->create('swift::' . $cacheKey);
|
||||||
|
|
||||||
$this->params = $params;
|
$this->params = $params;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -82,19 +91,36 @@ class Swift implements IObjectStore {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
$cachedTokenString = $this->memcache->get('token');
|
||||||
$this->client->authenticate();
|
if ($cachedTokenString) {
|
||||||
} catch (ClientErrorResponseException $e) {
|
$cachedToken = unserialize($cachedTokenString);
|
||||||
$statusCode = $e->getResponse()->getStatusCode();
|
try {
|
||||||
if ($statusCode == 412) {
|
$this->client->importCredentials($cachedToken);
|
||||||
throw new StorageAuthException('Precondition failed, verify the keystone url', $e);
|
} catch (\Exception $e) {
|
||||||
} else if ($statusCode === 401) {
|
$this->client->setTokenObject(new Token());
|
||||||
throw new StorageAuthException('Authentication failed, verify the username, password and possibly tenant', $e);
|
|
||||||
} else {
|
|
||||||
throw new StorageAuthException('Unknown error', $e);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @var Token $token */
|
||||||
|
$token = $this->client->getTokenObject();
|
||||||
|
|
||||||
|
if (!$token || $token->hasExpired()) {
|
||||||
|
try {
|
||||||
|
$this->client->authenticate();
|
||||||
|
$this->memcache->set('token', serialize($this->client->exportCredentials()));
|
||||||
|
} catch (ClientErrorResponseException $e) {
|
||||||
|
$statusCode = $e->getResponse()->getStatusCode();
|
||||||
|
if ($statusCode == 412) {
|
||||||
|
throw new StorageAuthException('Precondition failed, verify the keystone url', $e);
|
||||||
|
} else if ($statusCode === 401) {
|
||||||
|
throw new StorageAuthException('Authentication failed, verify the username, password and possibly tenant', $e);
|
||||||
|
} else {
|
||||||
|
throw new StorageAuthException('Unknown error', $e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/** @var Catalog $catalog */
|
/** @var Catalog $catalog */
|
||||||
$catalog = $this->client->getCatalog();
|
$catalog = $this->client->getCatalog();
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue