@@ -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