Skip to content

Commit 3dddcec

Browse files
Adding doc for caching credentials
1 parent b6ee083 commit 3dddcec

File tree

5 files changed

+44
-4
lines changed

5 files changed

+44
-4
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"guzzle/http" : "3.8.*@dev"
2626
},
2727
"require-dev" : {
28-
"guzzle/guzzle": "3.8.*@dev",
28+
"guzzle/guzzle": "dev-master",
2929
"psr/log": "1.0.*",
3030
"satooshi/php-coveralls": "0.6.*@dev"
3131
}

docs/userguide/caching-credentials.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Caching credentials
2+
3+
You can speed up your API operations by caching your credentials in a (semi-)permanent location, such as your
4+
DB or local filesystem. This enable subsequent requests to access a shared resource, instead of repetitively having to
5+
re-authenticate on every thread of execution.
6+
7+
Tokens are valid for 24 hours, so you can effectively re-use the same cached value for that period. If you try to use
8+
a cached version that has expired, an authentication request will be made.
9+
10+
## Filesystem example
11+
12+
In this example, credentials will be saved to a file in the local filesystem. Be sure to exclude it from your VCS.
13+
14+
```php
15+
use OpenCloud\Rackspace;
16+
17+
$client = new Rackspace(Rackspace::US_IDENTITY_ENDPOINT, array(
18+
'username' => 'foo',
19+
'apiKey' => 'bar'
20+
));
21+
22+
$cacheFile = __DIR__ . '/.opencloud_token';
23+
24+
// If the cache file exists, try importing it into the client
25+
if (file_exists($cacheFile)) {
26+
$data = unserialize(file_get_contents($cacheFile));
27+
$client->importCredentials($data);
28+
}
29+
30+
$token = $client->getTokenObject();
31+
32+
// If no token exists, or the current token is expired, re-authenticate and save the new token to disk
33+
if (!$token || ($token && $token->hasExpired())) {
34+
$client->authenticate();
35+
file_put_contents($cacheFile, serialize($client->exportCredentials()));
36+
}
37+
```
38+
39+
In tests, the above code shaved about 1-2s off the execution time.

lib/OpenCloud/Common/PersistentObject.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ public function getUrl($path = null, array $query = array())
334334
$url = Url::factory($url);
335335
}
336336

337-
return $url->addPath($path)->setQuery($query);
337+
return $url->addPath((string) $path)->setQuery($query);
338338
}
339339

340340
/**

lib/OpenCloud/Identity/Resource/Token.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function getExpires()
6666
*/
6767
public function hasExpired()
6868
{
69-
return time() >= $this->expires;
69+
return time() >= strtotime($this->expires);
7070
}
7171

7272
}

lib/OpenCloud/ObjectStore/Resource/AbstractContainer.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ public function getUrl($path = null, array $params = array())
7070
}
7171

7272
$url = $this->getService()->getUrl();
73-
return $url->addPath((string) $this->getName())->addPath($path)->setQuery($params);
73+
74+
return $url->addPath((string) $this->getName())->addPath((string) $path)->setQuery($params);
7475
}
7576

7677
protected function createRefreshRequest()

0 commit comments

Comments
 (0)