@@ -98,7 +98,7 @@ public function getNewToken(
9898 */
9999 public function persistNewAccessToken (OAuth2AccessTokenEntityInterface $ accessTokenEntity ): void
100100 {
101- if (!$ accessTokenEntity instanceof AccessTokenEntity) {
101+ if (!( $ accessTokenEntity instanceof AccessTokenEntity) ) {
102102 throw new Error ('Invalid AccessTokenEntity ' );
103103 }
104104
@@ -112,6 +112,14 @@ public function persistNewAccessToken(OAuth2AccessTokenEntityInterface $accessTo
112112 $ stmt ,
113113 $ accessTokenEntity ->getState (),
114114 );
115+
116+ $ this ->protocolCache ?->set(
117+ $ accessTokenEntity ->getState (),
118+ $ this ->helpers ->dateTime ()->getSecondsToExpirationTime (
119+ $ accessTokenEntity ->getExpiryDateTime ()->getTimestamp (),
120+ ),
121+ $ this ->getCacheKey ((string )$ accessTokenEntity ->getIdentifier ()),
122+ );
115123 }
116124
117125 /**
@@ -121,6 +129,13 @@ public function persistNewAccessToken(OAuth2AccessTokenEntityInterface $accessTo
121129 */
122130 public function findById (string $ tokenId ): ?AccessTokenEntity
123131 {
132+ /** @var ?array $cachedState */
133+ $ cachedState = $ this ->protocolCache ?->get(null , $ this ->getCacheKey ($ tokenId ));
134+
135+ if (is_array ($ cachedState )) {
136+ return $ this ->accessTokenEntityFactory ->fromState ($ cachedState );
137+ }
138+
124139 $ stmt = $ this ->database ->read (
125140 "SELECT * FROM {$ this ->getTableName ()} WHERE id = :id " ,
126141 [
@@ -136,7 +151,17 @@ public function findById(string $tokenId): ?AccessTokenEntity
136151 $ data = current ($ rows );
137152 $ data ['client ' ] = $ this ->clientRepository ->findById ((string )$ data ['client_id ' ]);
138153
139- return $ this ->accessTokenEntityFactory ->fromState ($ data );
154+ $ accessTokenEntity = $ this ->accessTokenEntityFactory ->fromState ($ data );
155+
156+ $ this ->protocolCache ?->set(
157+ $ accessTokenEntity ->getState (),
158+ $ this ->helpers ->dateTime ()->getSecondsToExpirationTime (
159+ $ accessTokenEntity ->getExpiryDateTime ()->getTimestamp (),
160+ ),
161+ $ this ->getCacheKey ((string )$ accessTokenEntity ->getIdentifier ()),
162+ );
163+
164+ return $ accessTokenEntity ;
140165 }
141166
142167 /**
@@ -209,5 +234,13 @@ private function update(AccessTokenEntity $accessTokenEntity): void
209234 $ stmt ,
210235 $ accessTokenEntity ->getState (),
211236 );
237+
238+ $ this ->protocolCache ?->set(
239+ $ accessTokenEntity ->getState (),
240+ $ this ->helpers ->dateTime ()->getSecondsToExpirationTime (
241+ $ accessTokenEntity ->getExpiryDateTime ()->getTimestamp (),
242+ ),
243+ $ this ->getCacheKey ((string )$ accessTokenEntity ->getIdentifier ()),
244+ );
212245 }
213246}
0 commit comments