22
33namespace Composite \DB ;
44
5- use Composite \DB \Exceptions \DbException ;
6- use Composite \DB \Tests \TestStand \Entities \TestAutoincrementEntity ;
75use Composite \Entity \AbstractEntity ;
86use Psr \SimpleCache \CacheInterface ;
97use Ramsey \Uuid \UuidInterface ;
108
119abstract class AbstractCachedTable extends AbstractTable
1210{
11+ use SelectRawTrait;
12+
1313 protected const CACHE_VERSION = 1 ;
1414
1515 public function __construct (
@@ -94,44 +94,47 @@ private function collectCacheKeysByEntity(AbstractEntity $entity): array
9494 }
9595
9696 /**
97- * @return array<string, mixed> |null
97+ * @return AbstractEntity |null
9898 */
99- protected function _findByPkCached (mixed $ pk , null |int |\DateInterval $ ttl = null ): ? array
99+ protected function _findByPkCached (mixed $ pk , null |int |\DateInterval $ ttl = null ): mixed
100100 {
101101 return $ this ->_findOneCached ($ this ->getPkCondition ($ pk ), $ ttl );
102102 }
103103
104104 /**
105- * @param array<string, mixed> $condition
105+ * @param array<string, mixed> $where
106106 * @param int|\DateInterval|null $ttl
107- * @return array<string, mixed> |null
107+ * @return AbstractEntity |null
108108 */
109- protected function _findOneCached (array $ condition , null |int |\DateInterval $ ttl = null ): ? array
109+ protected function _findOneCached (array $ where , null |int |\DateInterval $ ttl = null ): mixed
110110 {
111- return $ this ->getCached (
112- $ this ->getOneCacheKey ($ condition ),
113- fn () => $ this ->_findOne ( $ condition ),
111+ $ row = $ this ->getCached (
112+ $ this ->getOneCacheKey ($ where ),
113+ fn () => $ this ->_findOneRaw ( $ where ),
114114 $ ttl ,
115- ) ?: null ;
115+ );
116+ return $ this ->createEntity ($ row );
116117 }
117118
118119 /**
119120 * @param array<string, mixed>|Where $where
120121 * @param array<string, string>|string $orderBy
121- * @return array<string, mixed>[]
122+ * @return array<AbstractEntity>|array<array-key, AbstractEntity>
122123 */
123124 protected function _findAllCached (
124125 array |Where $ where = [],
125126 array |string $ orderBy = [],
126127 ?int $ limit = null ,
127128 null |int |\DateInterval $ ttl = null ,
129+ ?string $ keyColumnName = null ,
128130 ): array
129131 {
130- return $ this ->getCached (
132+ $ rows = $ this ->getCached (
131133 $ this ->getListCacheKey ($ where , $ orderBy , $ limit ),
132- fn () => $ this ->_findAll (where: $ where , orderBy: $ orderBy , limit: $ limit ),
134+ fn () => $ this ->_findAllRaw (where: $ where , orderBy: $ orderBy , limit: $ limit ),
133135 $ ttl ,
134136 );
137+ return $ this ->createEntities ($ rows , $ keyColumnName );
135138 }
136139
137140 /**
@@ -165,10 +168,14 @@ protected function getCached(string $cacheKey, callable $dataCallback, null|int|
165168 /**
166169 * @param mixed[] $ids
167170 * @param int|\DateInterval|null $ttl
168- * @return array<array<string, mixed> >
171+ * @return array<AbstractEntity>| array<array-key, AbstractEntity >
169172 * @throws \Psr\SimpleCache\InvalidArgumentException
170173 */
171- protected function _findMultiCached (array $ ids , null |int |\DateInterval $ ttl = null ): array
174+ protected function _findMultiCached (
175+ array $ ids ,
176+ null |int |\DateInterval $ ttl = null ,
177+ ?string $ keyColumnName = null ,
178+ ): array
172179 {
173180 $ result = $ cacheKeys = $ foundIds = [];
174181 foreach ($ ids as $ id ) {
@@ -191,7 +198,7 @@ protected function _findMultiCached(array $ids, null|int|\DateInterval $ttl = nu
191198 $ result [] = $ row ;
192199 }
193200 }
194- return $ result ;
201+ return $ this -> createEntities ( $ result, $ keyColumnName ) ;
195202 }
196203
197204 /**
@@ -271,7 +278,7 @@ protected function buildCacheKey(mixed ...$parts): string
271278
272279 private function formatStringForCacheKey (string $ string ): string
273280 {
274- $ string = mb_strtolower ($ string );
281+ $ string = strtolower ($ string );
275282 $ string = str_replace (['!= ' , '<> ' , '> ' , '< ' , '= ' ], ['_not_ ' , '_not_ ' , '_gt_ ' , '_lt_ ' , '_eq_ ' ], $ string );
276283 $ string = (string )preg_replace ('/\W/ ' , '_ ' , $ string );
277284 return trim ((string )preg_replace ('/_+/ ' , '_ ' , $ string ), '_ ' );
0 commit comments