-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from byjg/5.0
Enable Cache Query results
- Loading branch information
Showing
9 changed files
with
161 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Caching the Results | ||
|
||
The `AnyDatasetDb` library has a built-in cache mechanism that can be used to cache the results of a query. | ||
This is useful when you have a query that is expensive to run and you want to cache the results for a | ||
certain amount of time. | ||
|
||
To enable caching, you need to pass a `CacheQueryResult` object when you´ll query the database. | ||
|
||
Here is an example of how to use the cache mechanism: | ||
|
||
```php | ||
<?php | ||
|
||
$query = Query::getInstance() | ||
->where('id = :id1', ['id1'=>3]); | ||
|
||
$cacheEngine = /* any SimpleCache implementation */; | ||
|
||
// Get the result and save to cache | ||
$result = $repository->getByQuery($query, cache: new CacheQueryResult($cacheEngine, 120)); | ||
``` | ||
|
||
In this example, the result of the query will be saved in the cache for 120 seconds. | ||
|
||
The `CacheQueryResult` object has the following parameters: | ||
|
||
- `cacheEngine`: The cache engine to use. It must implement the `Psr\SimpleCache\CacheInterface` interface. | ||
- `ttl`: The time-to-live of the cache in seconds. If the cache is older than this value, it will be considered expired. | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace ByJG\MicroOrm; | ||
|
||
use DateInterval; | ||
use Psr\SimpleCache\CacheInterface; | ||
|
||
class CacheQueryResult | ||
{ | ||
protected CacheInterface $cache; | ||
protected string $cacheKey; | ||
protected int|DateInterval $ttl; | ||
|
||
public function __construct(CacheInterface $cache, string $cacheKey, int|DateInterval $ttl) | ||
{ | ||
$this->cache = $cache; | ||
$this->cacheKey = $cacheKey; | ||
$this->ttl = $ttl; | ||
} | ||
|
||
public function getCache(): CacheInterface | ||
{ | ||
return $this->cache; | ||
} | ||
|
||
public function getCacheKey(): string | ||
{ | ||
return $this->cacheKey; | ||
} | ||
|
||
public function getTtl(): DateInterval|int | ||
{ | ||
return $this->ttl; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters