@@ -13,7 +13,7 @@ class CacheTokenRepository extends TokenRepository
13
13
/**
14
14
* @var string
15
15
*/
16
- protected $ cacheKey ;
16
+ protected $ cacheKeyPrefix ;
17
17
18
18
/**
19
19
* @var int
@@ -31,13 +31,14 @@ class CacheTokenRepository extends TokenRepository
31
31
protected $ cacheStore ;
32
32
33
33
/**
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
37
38
*/
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 )
39
40
{
40
- $ this ->cacheKey = $ cacheKey ?? 'passport_token_ ' ;
41
+ $ this ->cacheKeyPrefix = sprintf ( ' %s_token_ ' , $ cacheKeyPrefix ?? 'passport ' ) ;
41
42
$ this ->expiresInSeconds = $ expiresInSeconds ?? 5 * 60 ;
42
43
$ this ->cacheTags = $ tags ;
43
44
$ this ->cacheStore = $ store ?? \config ('cache.default ' );
@@ -52,9 +53,13 @@ public function __construct(string $cacheKey = null, int $expiresInSeconds = nul
52
53
*/
53
54
public function find ($ id )
54
55
{
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
+ );
58
63
}
59
64
60
65
/**
@@ -67,9 +72,13 @@ public function find($id)
67
72
*/
68
73
public function findForUser ($ id , $ userId )
69
74
{
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
+ );
73
82
}
74
83
75
84
/**
@@ -81,9 +90,13 @@ public function findForUser($id, $userId)
81
90
*/
82
91
public function forUser ($ userId ): Collection
83
92
{
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
+ );
87
100
}
88
101
89
102
/**
@@ -96,12 +109,21 @@ public function forUser($userId): Collection
96
109
*/
97
110
public function getValidToken ($ user , $ client )
98
111
{
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 ;
106
128
}
107
129
}
0 commit comments