Skip to content

Commit

Permalink
Merge pull request shlinkio#95 from acelaya-forks/feature/cache-defau…
Browse files Browse the repository at this point in the history
…lt-lifetime

Feature/cache default lifetime
  • Loading branch information
acelaya authored Jan 7, 2022
2 parents 0d476fd + 75e69ae commit b967e9a
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 25 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com), and this project adheres to [Semantic Versioning](https://semver.org).

## [Unreleased]
## [4.3.0] - 2022-01-07
### Added
* Added support in paginator utils to provide a custom data prop name
* Added support in paginator utils to provide a custom data prop name.
* Added support to define a default lifetime in cache adapters.

### Changed
* *Nothing*
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM composer:2

ENV APCU_VERSION 5.1.20
ENV APCU_VERSION 5.1.21

RUN apk add --no-cache libpng-dev libpng libjpeg-turbo-dev libwebp-dev zlib-dev libxpm-dev
RUN docker-php-ext-install gd
Expand Down
19 changes: 11 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ Install this library using composer:
A [symfony/cache](https://symfony.com/doc/current/components/cache.html) adapter is registered, under the `Psr\Cache\CacheItemPoolInterface` service key (as all adapters implement it).

The concrete implementation it returns is different depending on your configuration:

* An `ArrayAdapter` instance when the `debug` config is set to true or when the APUc extension is not installed and the `cache.redis` config is not defined.
* An `ApcuAdapter`instance when no `cache.redis` is defined and the APCu extension is installed.
* A `RedisAdapter` instance when the `cache.redis` config is defined.

The last two adapters will use the namespace defined in `cache.namespace` config entry.


* An `ArrayAdapter` instance when the `debug` config is set to true or when the APUc extension is not installed and the `cache.redis` config is not defined.
* An `ApcuAdapter`instance when no `cache.redis` is defined and the APCu extension is installed.
* A `RedisAdapter` instance when the `cache.redis` config is defined.

The last two adapters will use the namespace defined in `cache.namespace` config entry.

The three of them will allow setting a default lifetime for those entries which do not explicitly define one, picking it up from `cache.default_lifetime`.

```php
<?php

Expand All @@ -42,7 +44,8 @@ return [

'cache' => [
'namespace' => 'my_namespace',

'default_lifetime' => 86400, // Optional. Defaults to "never expire"

'redis' => [
'servers' => [
'tcp://1.1.1.1:6379',
Expand Down
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@
"infect:test": "<fg=blue;options=bold>Alias for \"test:ci\" and \"infect:show:ci\"</>"
},
"config": {
"sort-packages": true
"sort-packages": true,
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true,
"infection/extension-installer": true
}
}
}
9 changes: 5 additions & 4 deletions src/Cache/CacheFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,22 @@ public function __construct(?callable $apcuEnabled = null)

public function __invoke(ContainerInterface $container): CacheItemPoolInterface
{
$apcuEnabled = ($this->apcuEnabled)();
$config = $container->get('config');
$isDebug = (bool) ($config['debug'] ?? false);
$redisConfig = $config['cache']['redis'] ?? null;
$apcuEnabled = ($this->apcuEnabled)();
$lifetime = (int) ($config['cache']['default_lifetime'] ?? 0);

if ($isDebug || (! $apcuEnabled && $redisConfig === null)) {
return new Adapter\ArrayAdapter();
return new Adapter\ArrayAdapter($lifetime);
}

$namespace = $config['cache']['namespace'] ?? '';
if ($redisConfig === null) {
return new Adapter\ApcuAdapter($namespace);
return new Adapter\ApcuAdapter($namespace, $lifetime);
}

$predis = $container->get(PredisClient::class);
return new Adapter\RedisAdapter($predis, $namespace);
return new Adapter\RedisAdapter($predis, $namespace, $lifetime);
}
}
9 changes: 0 additions & 9 deletions src/Doctrine/EntityManagerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,14 @@
namespace Shlinkio\Shlink\Common\Doctrine;

use Doctrine\Common\Cache\Cache;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Types\Type;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\ORMException;
use Doctrine\ORM\Tools\Setup;
use Psr\Container\ContainerInterface;
use Shlinkio\Shlink\Common\Doctrine\Mapping\EnhancedPHPDriver;

class EntityManagerFactory
{
/**
* @throws ORMException
* @throws Exception
*/
public function __invoke(ContainerInterface $container): EntityManager
{
$globalConfig = $container->get('config');
Expand Down Expand Up @@ -48,9 +42,6 @@ public function __invoke(ContainerInterface $container): EntityManager
return $em;
}

/**
* @throws Exception
*/
private function registerTypes(array $ormConfig): void
{
$types = $ormConfig['types'] ?? [];
Expand Down

0 comments on commit b967e9a

Please sign in to comment.