Skip to content

Commit

Permalink
find or create using cache
Browse files Browse the repository at this point in the history
  • Loading branch information
nekufa committed Jul 10, 2024
1 parent 07e517f commit 6943ade
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 13 deletions.
51 changes: 38 additions & 13 deletions src/Space.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,18 +153,7 @@ public function drop()

public function find(Criteria|array|null $criteria = null, ?int $limit = null): array
{
if (!$criteria) {
$criteria = Criteria::allIterator();
} elseif (is_array($criteria)) {
$index = $this->castIndex(array_keys($criteria));
$criteria = Criteria::eqIterator()
->andIndex($index['iid'])
->andKey($this->getKey($criteria, $index));
}

if ($limit) {
$criteria = $criteria->andLimit($limit);
}
$criteria = $this->getCriteria($criteria, $limit);

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

public function findOrCreate(array $query, ?array $data = null)
{
if ($this->cache) {
$instance = $this->findOne($query);
if ($instance) {
return $instance;
}
$criteria = $this->getCriteria($query, 1);
$this->cache->deleteItem(md5(serialize($criteria)));
}

if ($data == null) {
$data = $query;
} else {
Expand Down Expand Up @@ -230,14 +228,23 @@ public function findOrCreate(array $query, ?array $data = null)
'id_key' => array_search('id', $this->fields) + 1
]
);

if (!$present) {
$this->mapper->middleware->register(
new InsertRequest($this->id, $tuple),
new Response([], [Keys::DATA => [$tuple]]),
);
}

return $this->getInstance($tuple);
$instance = $this->getInstance($tuple);
if ($this->cache) {
$criteria = $this->getCriteria($query, 1);
$item = $this->cache->getItem(md5(serialize($criteria)));
$item->set([$instance]);
$this->cache->save($item);
}

return $instance;
}

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

public function getCriteria(Criteria|array|null $criteria = null, ?int $limit = null): Criteria
{
if (!$criteria) {
$criteria = Criteria::allIterator();
} elseif (is_array($criteria)) {
$index = $this->castIndex(array_keys($criteria));
$criteria = Criteria::eqIterator()
->andIndex($index['iid'])
->andKey($this->getKey($criteria, $index));
}

if ($limit) {
$criteria = $criteria->andLimit($limit);
}

return $criteria;
}

public function getFieldFormat(string $name): array
{
foreach ($this->format as $field) {
Expand Down
11 changes: 11 additions & 0 deletions tests/MapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@ public function testCache()
$mapper->find('_vspace');
$this->assertCount(1, $cache->getValues());
$this->assertCount($queries, $this->middleware->data);

$tester = $mapper->createSpace('tester');
$tester->addProperty('id', 'unsigned');
$tester->cache = new ArrayAdapter();
$mapper->findOrCreate('tester', []); // created
$queries = count($this->middleware->data);
$mapper->findOrCreate('tester', []);
$mapper->findOrCreate('tester', []);
$mapper->findOrCreate('tester', []);
$mapper->findOrCreate('tester', []);
$this->assertCount($queries, $this->middleware->data);
}

public function testDifferentIndexPartConfiguration()
Expand Down

0 comments on commit 6943ade

Please sign in to comment.