https://github.com/becklyn/cache-bundle
A simple high performance cache.
https://github.com/becklyn/cache-bundle
Last synced: 3 months ago
JSON representation
A simple high performance cache.
- Host: GitHub
- URL: https://github.com/becklyn/cache-bundle
- Owner: Becklyn
- License: bsd-3-clause
- Created: 2020-01-31T21:24:14.000Z (over 5 years ago)
- Default Branch: 1.x
- Last Pushed: 2022-02-17T14:23:36.000Z (over 3 years ago)
- Last Synced: 2025-02-14T09:43:16.200Z (3 months ago)
- Language: PHP
- Size: 30.3 KB
- Stars: 1
- Watchers: 5
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
Becklyn Cache Bundle
====================Provides a simple cache, that should be easy to use and high performance. Warning: for highly concurrent usage you should
either prepare the cache in advance or use symfony's own cache, as it has proper cache stampede protection.Usage
-----Fetch the simple cache factory and get the cache item:
```php
use Becklyn\Cache\Cache\SimpleCacheFactory;class MyService
{
private SimpleCacheItemInterface $cache;/**
*/
public function __construct (SimpleCacheFactory $cacheFactory)
{
$this->cache = $cacheFactory->getItem(
"my.cache.key",
fn () => $this->loadItems()
);
}/**
* Returns the cached or fresh items, depending on several conditions.
*/
public function getItems () : array
{
return $this->cache->get();
}
/**
* Loads the items from the database
*/
public function loadItems () : array
{
// ...
}
}
```Caching Based on Symfony Config
-------------------------------You can either pass the cache key and generator to the `getItem()` method (like in the example above),
or you can additionally pass in resources that should be tracked and can be used for cache invalidation.If you pass resources, this will be a two-level cache:
1. first level will be the plain symfony/cache, which is really fast but has no proper way of cache invalidation.
2. second level will be the `ConfigCache` which will automatically be invalidated if some of the tracked resources change.```php
$cacheFactory->getItem(
"my.cache.key",
fn () => $this->loadItems(),
// eg. if your cache status depends on routing resources
$this->router->getRouteCollection()->getResources()
);
```