Skip to content

Commit 0704c34

Browse files
committed
query cache
1 parent aa9d725 commit 0704c34

File tree

3 files changed

+47
-8
lines changed

3 files changed

+47
-8
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,20 @@ $mapper->cache = $cache;
172172
$mapper->getSpace('_vspace'); // no new requests are made
173173
```
174174

175+
## Query Cache
176+
If you don't want to perform select queries you can inject cache interface to space instance/\
177+
For example, we use apcu adapter from `symfony/cache` package.
178+
```php
179+
use Symfony\Component\Cache\Adapter\ArrayAdapter;
180+
181+
$mapper = new Mapper(Client::fromDefaults());
182+
$mapper->getSpace('_vspace')->cache = new ArrayAdapter(); // feel free to set default ttl
183+
184+
$mapper->find('_vspace'); // query is executed
185+
$mapper->find('_vspace'); // results are fetched from cache
186+
$mapper->find('_vspace'); // results are fetched from cache
187+
```
188+
175189
## Changes registration
176190
In some cases you want to get all changes that were made during current session.\
177191
By default spy configuration is set to false, this improves performance a bit.

src/Space.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Tarantool\Mapper;
66

77
use Exception;
8+
use Psr\Cache\CacheItemPoolInterface;
89
use ReflectionClass;
910
use ReflectionMethod;
1011
use Tarantool\Client\Exception\RequestFailed;
@@ -17,6 +18,8 @@
1718

1819
class Space
1920
{
21+
public ?CacheItemPoolInterface $cache = null;
22+
2023
private readonly int $id;
2124
private readonly string $name;
2225

@@ -163,8 +166,22 @@ public function find(Criteria|array|null $criteria = null, ?int $limit = null):
163166
$criteria = $criteria->andLimit($limit);
164167
}
165168

169+
$item = null;
170+
if ($this->cache) {
171+
$item = $this->cache->getItem(md5(serialize($criteria)));
172+
if ($item->isHit()) {
173+
return $item->get();
174+
}
175+
}
176+
166177
$tuples = $this->mapper->client->getSpaceById($this->id)->select($criteria);
167-
return array_map($this->getInstance(...), $tuples);
178+
$result = array_map($this->getInstance(...), $tuples);
179+
if ($item) {
180+
$item->set($result);
181+
$this->cache->save($item);
182+
}
183+
184+
return $result;
168185
}
169186

170187
public function findOne(Criteria|array|null $criteria = null)

tests/MapperTest.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,23 +40,31 @@ public function testCache()
4040
$mapper = $this->createMapper(dropUserSpaces: false);
4141
$cache = new ArrayAdapter();
4242
$mapper->cache = $cache;
43-
$this->assertCount(0, $cache->getvalues());
4443
$mapper->dropUserSpaces();
4544
$mapper->find('_vspace');
4645

47-
$this->assertNotCount(0, $cache->getvalues());
46+
$this->assertNotCount(0, $cache->getValues());
4847

4948
$freshCounter = count($this->middleware->data);
50-
5149
$mapper = $this->createMapper(dropUserSpaces: false);
5250
$mapper->cache = $cache;
5351
$mapper->dropUserSpaces();
5452
$mapper->find('_vspace');
5553

56-
// 4 requests:
57-
// - schema id 0 space + index
58-
// - schema id N space + index
59-
$this->assertCount($freshCounter - 4, $this->middleware->data);
54+
$this->assertLessThan($freshCounter, count($this->middleware->data));
55+
56+
$cache = new ArrayAdapter();
57+
$mapper->getSpace('_vspace')->cache = $cache;
58+
59+
$mapper->find('_vspace');
60+
$this->assertCount(1, $cache->getValues());
61+
$queries = count($this->middleware->data);
62+
63+
$mapper->find('_vspace');
64+
$mapper->find('_vspace');
65+
$mapper->find('_vspace');
66+
$this->assertCount(1, $cache->getValues());
67+
$this->assertCount($queries, $this->middleware->data);
6068
}
6169

6270
public function testDifferentIndexPartConfiguration()

0 commit comments

Comments
 (0)