Skip to content

Commit eafd102

Browse files
committed
cache tags.
1 parent 0a8de27 commit eafd102

File tree

5 files changed

+55
-31
lines changed

5 files changed

+55
-31
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ sftp-config.json
77
composer.lock
88
.subsplit
99
.php_cs.cache
10+
.php-cs-fixer.cache
11+
.phpunit.result.cache

.php_cs renamed to .php-cs-fixer.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
<?php
22

3-
return PhpCsFixer\Config::create()
3+
return (new PhpCsFixer\Config())
44
->setRules([
5-
'@PSR2' => true,
5+
'@PSR12' => true,
66
'binary_operator_spaces' => true,
77
'blank_line_after_opening_tag' => true,
88
'compact_nullable_typehint' => true,
99
'declare_equal_normalize' => true,
1010
'lowercase_cast' => true,
1111
'lowercase_static_reference' => true,
1212
'new_with_braces' => true,
13-
'no_unused_imports' => true,
1413
'no_blank_lines_after_class_opening' => true,
1514
'no_leading_import_slash' => true,
1615
'no_whitespace_in_blank_line' => true,
16+
'no_unused_imports' => true,
1717
'ordered_class_elements' => [
1818
'order' => [
1919
'use_trait',

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ return [
2121
//...
2222
'cache' => [
2323
// Cache key prefix
24-
'prefix' => 'passport_token_',
24+
'prefix' => 'passport_',
2525

2626
// The lifetime of token cache,
2727
// Unit: second

composer.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
},
1515
"require-dev": {
1616
"brainmaestro/composer-git-hooks": "^2.7",
17-
"friendsofphp/php-cs-fixer": "^2.15",
18-
"orchestra/testbench": "^4.16|^5.3",
17+
"friendsofphp/php-cs-fixer": "^3.0",
18+
"orchestra/testbench": "^5.3",
1919
"mockery/mockery": "^1.0",
20-
"phpunit/phpunit": "^8.0|^9.0"
20+
"phpunit/phpunit": "^9.0"
2121
},
2222
"autoload": {
2323
"psr-4": {
@@ -56,8 +56,8 @@
5656
"cghooks update"
5757
],
5858
"cghooks": "vendor/bin/cghooks",
59-
"check-style": "php-cs-fixer fix --using-cache=no --diff --config=.php_cs --dry-run --ansi",
60-
"fix-style": "php-cs-fixer fix --using-cache=no --config=.php_cs --ansi",
59+
"check-style": "php-cs-fixer fix --using-cache=no --diff --dry-run --ansi",
60+
"fix-style": "php-cs-fixer fix --using-cache=no --ansi",
6161
"test": "vendor/bin/phpunit"
6262
},
6363
"scripts-descriptions": {

src/CacheTokenRepository.php

+44-22
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class CacheTokenRepository extends TokenRepository
1313
/**
1414
* @var string
1515
*/
16-
protected $cacheKey;
16+
protected $cacheKeyPrefix;
1717

1818
/**
1919
* @var int
@@ -31,13 +31,14 @@ class CacheTokenRepository extends TokenRepository
3131
protected $cacheStore;
3232

3333
/**
34-
* @param string $cacheKey
35-
* @param int $expiresInSeconds
36-
* @param array $tags
34+
* @param string|null $cacheKeyPrefix
35+
* @param int|null $expiresInSeconds
36+
* @param array $tags
37+
* @param string|null $store
3738
*/
38-
public function __construct(string $cacheKey = null, int $expiresInSeconds = null, array $tags = [], ?string $store = null)
39+
public function __construct(string $cacheKeyPrefix = null, int $expiresInSeconds = null, array $tags = [], ?string $store = null)
3940
{
40-
$this->cacheKey = $cacheKey ?? 'passport_token_';
41+
$this->cacheKeyPrefix = sprintf('%s_token_', $cacheKeyPrefix ?? 'passport');
4142
$this->expiresInSeconds = $expiresInSeconds ?? 5 * 60;
4243
$this->cacheTags = $tags;
4344
$this->cacheStore = $store ?? \config('cache.default');
@@ -52,9 +53,13 @@ public function __construct(string $cacheKey = null, int $expiresInSeconds = nul
5253
*/
5354
public function find($id)
5455
{
55-
return Cache::store($this->cacheStore)->remember($this->cacheKey . $id, \now()->addSeconds($this->expiresInSeconds), function () use ($id) {
56-
return Passport::token()->where('id', $id)->first();
57-
});
56+
return Cache::store($this->cacheStore)->tags($this->cacheTags)->remember(
57+
$this->itemKey($id),
58+
\now()->addSeconds($this->expiresInSeconds),
59+
function () use ($id) {
60+
return Passport::token()->where('id', $id)->first();
61+
}
62+
);
5863
}
5964

6065
/**
@@ -67,9 +72,13 @@ public function find($id)
6772
*/
6873
public function findForUser($id, $userId)
6974
{
70-
return Cache::store($this->cacheStore)->remember($this->cacheKey . $id, \now()->addSeconds($this->expiresInSeconds), function () use ($id, $userId) {
71-
return Passport::token()->where('id', $id)->where('user_id', $userId)->first();
72-
});
75+
return Cache::store($this->cacheStore)->tags($this->cacheTags)->remember(
76+
$this->itemKey($id),
77+
\now()->addSeconds($this->expiresInSeconds),
78+
function () use ($id, $userId) {
79+
return Passport::token()->where('id', $id)->where('user_id', $userId)->first();
80+
}
81+
);
7382
}
7483

7584
/**
@@ -81,9 +90,13 @@ public function findForUser($id, $userId)
8190
*/
8291
public function forUser($userId): Collection
8392
{
84-
return Cache::store($this->cacheStore)->remember($this->cacheKey . $userId, \now()->addSeconds($this->expiresInSeconds), function () use ($userId) {
85-
return Passport::token()->where('user_id', $userId)->get();
86-
});
93+
return Cache::store($this->cacheStore)->tags($this->cacheTags)->remember(
94+
$this->itemKey($userId),
95+
\now()->addSeconds($this->expiresInSeconds),
96+
function () use ($userId) {
97+
return Passport::token()->where('user_id', $userId)->get();
98+
}
99+
);
87100
}
88101

89102
/**
@@ -96,12 +109,21 @@ public function forUser($userId): Collection
96109
*/
97110
public function getValidToken($user, $client)
98111
{
99-
return Cache::store($this->cacheStore)->remember($this->cacheKey . $user->getKey(), \now()->addSeconds($this->expiresInSeconds), function () use ($client, $user) {
100-
return $client->tokens()
101-
->whereUserId($user->getKey())
102-
->where('revoked', 0)
103-
->where('expires_at', '>', \now())
104-
->first();
105-
});
112+
return Cache::store($this->cacheStore)->tags($this->cacheTags)->remember(
113+
$this->itemKey($user->getKey()),
114+
\now()->addSeconds($this->expiresInSeconds),
115+
function () use ($client, $user) {
116+
return $client->tokens()
117+
->whereUserId($user->getKey())
118+
->where('revoked', 0)
119+
->where('expires_at', '>', \now())
120+
->first();
121+
}
122+
);
123+
}
124+
125+
public function itemKey(string $key)
126+
{
127+
return $this->cacheKeyPrefix . $key;
106128
}
107129
}

0 commit comments

Comments
 (0)