Skip to content

Commit 6943ade

Browse files
committed
find or create using cache
1 parent 07e517f commit 6943ade

File tree

2 files changed

+49
-13
lines changed

2 files changed

+49
-13
lines changed

src/Space.php

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -153,18 +153,7 @@ public function drop()
153153

154154
public function find(Criteria|array|null $criteria = null, ?int $limit = null): array
155155
{
156-
if (!$criteria) {
157-
$criteria = Criteria::allIterator();
158-
} elseif (is_array($criteria)) {
159-
$index = $this->castIndex(array_keys($criteria));
160-
$criteria = Criteria::eqIterator()
161-
->andIndex($index['iid'])
162-
->andKey($this->getKey($criteria, $index));
163-
}
164-
165-
if ($limit) {
166-
$criteria = $criteria->andLimit($limit);
167-
}
156+
$criteria = $this->getCriteria($criteria, $limit);
168157

169158
$item = null;
170159
if ($this->cache) {
@@ -195,6 +184,15 @@ public function findOne(Criteria|array|null $criteria = null)
195184

196185
public function findOrCreate(array $query, ?array $data = null)
197186
{
187+
if ($this->cache) {
188+
$instance = $this->findOne($query);
189+
if ($instance) {
190+
return $instance;
191+
}
192+
$criteria = $this->getCriteria($query, 1);
193+
$this->cache->deleteItem(md5(serialize($criteria)));
194+
}
195+
198196
if ($data == null) {
199197
$data = $query;
200198
} else {
@@ -230,14 +228,23 @@ public function findOrCreate(array $query, ?array $data = null)
230228
'id_key' => array_search('id', $this->fields) + 1
231229
]
232230
);
231+
233232
if (!$present) {
234233
$this->mapper->middleware->register(
235234
new InsertRequest($this->id, $tuple),
236235
new Response([], [Keys::DATA => [$tuple]]),
237236
);
238237
}
239238

240-
return $this->getInstance($tuple);
239+
$instance = $this->getInstance($tuple);
240+
if ($this->cache) {
241+
$criteria = $this->getCriteria($query, 1);
242+
$item = $this->cache->getItem(md5(serialize($criteria)));
243+
$item->set([$instance]);
244+
$this->cache->save($item);
245+
}
246+
247+
return $instance;
241248
}
242249

243250
public function findOrFail(Criteria|array|null $criteria = null)
@@ -250,6 +257,24 @@ public function findOrFail(Criteria|array|null $criteria = null)
250257
throw new Exception("Not found");
251258
}
252259

260+
public function getCriteria(Criteria|array|null $criteria = null, ?int $limit = null): Criteria
261+
{
262+
if (!$criteria) {
263+
$criteria = Criteria::allIterator();
264+
} elseif (is_array($criteria)) {
265+
$index = $this->castIndex(array_keys($criteria));
266+
$criteria = Criteria::eqIterator()
267+
->andIndex($index['iid'])
268+
->andKey($this->getKey($criteria, $index));
269+
}
270+
271+
if ($limit) {
272+
$criteria = $criteria->andLimit($limit);
273+
}
274+
275+
return $criteria;
276+
}
277+
253278
public function getFieldFormat(string $name): array
254279
{
255280
foreach ($this->format as $field) {

tests/MapperTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,17 @@ public function testCache()
6565
$mapper->find('_vspace');
6666
$this->assertCount(1, $cache->getValues());
6767
$this->assertCount($queries, $this->middleware->data);
68+
69+
$tester = $mapper->createSpace('tester');
70+
$tester->addProperty('id', 'unsigned');
71+
$tester->cache = new ArrayAdapter();
72+
$mapper->findOrCreate('tester', []); // created
73+
$queries = count($this->middleware->data);
74+
$mapper->findOrCreate('tester', []);
75+
$mapper->findOrCreate('tester', []);
76+
$mapper->findOrCreate('tester', []);
77+
$mapper->findOrCreate('tester', []);
78+
$this->assertCount($queries, $this->middleware->data);
6879
}
6980

7081
public function testDifferentIndexPartConfiguration()

0 commit comments

Comments
 (0)