@@ -48,6 +48,11 @@ public function getTableName(): string
4848 return $ this ->database ->applyPrefix (self ::TABLE_NAME );
4949 }
5050
51+ public function getCacheKey (string $ identifier ): string
52+ {
53+ return $ this ->getTableName () . '_ ' . $ identifier ;
54+ }
55+
5156 /**
5257 * @param string $identifier
5358 *
@@ -56,6 +61,13 @@ public function getTableName(): string
5661 */
5762 public function getUserEntityByIdentifier (string $ identifier ): ?UserEntity
5863 {
64+ /** @var ?array $cachedState */
65+ $ cachedState = $ this ->protocolCache ?->get(null , $ this ->getCacheKey ($ identifier ));
66+
67+ if (is_array ($ cachedState )) {
68+ return $ this ->userEntityFactory ->fromState ($ cachedState );
69+ }
70+
5971 $ stmt = $ this ->database ->read (
6072 "SELECT * FROM {$ this ->getTableName ()} WHERE id = :id " ,
6173 [
@@ -99,21 +111,29 @@ public function add(UserEntity $userEntity): void
99111 $ stmt ,
100112 $ userEntity ->getState (),
101113 );
114+
115+ $ this ->protocolCache ?->set(
116+ $ userEntity ->getState (),
117+ $ this ->moduleConfig ->getUserEntityCacheDuration (),
118+ $ this ->getCacheKey ($ userEntity ->getIdentifier ()),
119+ );
102120 }
103121
104- public function delete (UserEntity $ user ): void
122+ public function delete (UserEntity $ userEntity ): void
105123 {
106124 $ this ->database ->write (
107125 "DELETE FROM {$ this ->getTableName ()} WHERE id = :id " ,
108126 [
109- 'id ' => $ user ->getIdentifier (),
127+ 'id ' => $ userEntity ->getIdentifier (),
110128 ],
111129 );
130+
131+ $ this ->protocolCache ?->delete($ this ->getCacheKey ($ userEntity ->getIdentifier ()));
112132 }
113133
114- public function update (UserEntity $ user , ?DateTimeImmutable $ updatedAt = null ): void
134+ public function update (UserEntity $ userEntity , ?DateTimeImmutable $ updatedAt = null ): void
115135 {
116- $ user ->setUpdatedAt ($ updatedAt ?? $ this ->helpers ->dateTime ()->getUtc ());
136+ $ userEntity ->setUpdatedAt ($ updatedAt ?? $ this ->helpers ->dateTime ()->getUtc ());
117137
118138 $ stmt = sprintf (
119139 "UPDATE %s SET claims = :claims, updated_at = :updated_at, created_at = :created_at WHERE id = :id " ,
@@ -122,7 +142,13 @@ public function update(UserEntity $user, ?DateTimeImmutable $updatedAt = null):
122142
123143 $ this ->database ->write (
124144 $ stmt ,
125- $ user ->getState (),
145+ $ userEntity ->getState (),
146+ );
147+
148+ $ this ->protocolCache ?->set(
149+ $ userEntity ->getState (),
150+ $ this ->moduleConfig ->getUserEntityCacheDuration (),
151+ $ this ->getCacheKey ($ userEntity ->getIdentifier ()),
126152 );
127153 }
128154}
0 commit comments