aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorRobin Appelman <robin@icewind.nl>2017-03-28 13:06:09 +0200
committerMorris Jobke <hey@morrisjobke.de>2017-03-28 12:37:41 -0600
commit205d5586e84b5ea2b71d582e7bd731ed02583088 (patch)
treec9a5b0e8d5c6ed7184e9c1f836a67e5936b89739 /lib
parentde83bda7778e3b581bf4ffd5ecc1ba0dfcf70f8e (diff)
downloadnextcloud-server-205d5586e84b5ea2b71d582e7bd731ed02583088.tar.gz
nextcloud-server-205d5586e84b5ea2b71d582e7bd731ed02583088.zip
cache swift tokens in memcache
Signed-off-by: Robin Appelman <robin@icewind.nl>
Diffstat (limited to 'lib')
-rw-r--r--lib/private/Files/ObjectStore/Swift.php46
1 files changed, 36 insertions, 10 deletions
diff --git a/lib/private/Files/ObjectStore/Swift.php b/lib/private/Files/ObjectStore/Swift.php
index 43ce32af941..cfb5a654e68 100644
--- a/lib/private/Files/ObjectStore/Swift.php
+++ b/lib/private/Files/ObjectStore/Swift.php
@@ -31,6 +31,7 @@ use OCP\Files\StorageNotAvailableException;
use OpenCloud\Common\Exceptions\EndpointError;
use OpenCloud\Common\Service\Catalog;
use OpenCloud\Common\Service\CatalogItem;
+use OpenCloud\Identity\Resource\Token;
use OpenCloud\ObjectStore\Service;
use OpenCloud\OpenStack;
use OpenCloud\Rackspace;
@@ -57,6 +58,8 @@ class Swift implements IObjectStore {
*/
private $container;
+ private $memcache;
+
public function __construct($params) {
if (isset($params['bucket'])) {
$params['container'] = $params['bucket'];
@@ -71,9 +74,15 @@ class Swift implements IObjectStore {
if (isset($params['apiKey'])) {
$this->client = new Rackspace($params['url'], $params);
+ $cacheKey = $this->params['username'] . '@' . $this->params['url'] . '/' . $this->params['bucket'];
} else {
$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;
}
@@ -82,19 +91,36 @@ class Swift implements IObjectStore {
return;
}
- try {
- $this->client->authenticate();
- } 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);
+ $cachedTokenString = $this->memcache->get('token');
+ if ($cachedTokenString) {
+ $cachedToken = unserialize($cachedTokenString);
+ try {
+ $this->client->importCredentials($cachedToken);
+ } catch (\Exception $e) {
+ $this->client->setTokenObject(new Token());
}
}
+ /** @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 */
$catalog = $this->client->getCatalog();