Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Changed
- **Behaviour change**: `StorageRedisDsnModule` compresses stored values with deflate when `$marshallingOptions` is empty; pass `['enabled' => false]` to store them uncompressed. The package now requires `ext-zlib`.
- **Behaviour change**: `StorageRedisDsnModule` connects to Redis on first use, so building the application, as a compile does, needs no Redis, and a store that is down is logged as `pool_error` per request instead of failing the boot. Pass `['lazy' => false]` to connect when the pool is built. The package now requires `symfony/cache` 6.4.12 or later: earlier lazy connections fail to load with ext-redis 6.3.

## [Unreleased]

### Added
- `#[Link(rel: 'collection', href: ...)]` on a row's `onGet()` names the list it belongs to: a write to the row purges the list's URI and the variants that differ only in default-valued parameters, so the list needs no `#[DependsOn]` for its rows. A path-only href (`/posts`) takes the context scheme (`#[ContextScheme]`).
- The `purge_skipped` log entry (fields `uri`, the write's URI without its query, and `reason`, `unmatched_query`), recorded when a write on a resource without a cache attribute cannot address its own key because its query lacks a required `onGet()` parameter.
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
],
"require": {
"php": "^8.2",
"ext-zlib": "*",
"bear/resource": "^1.36",
"bear/sunday": "^1.5",
"koriym/semantic-logger": "^0.11.1",
Expand All @@ -24,7 +25,7 @@
"ray/aop": "^2.19.1",
"ray/di": "^2.23",
"ray/psr-cache-module": "^1.6",
"symfony/cache": "^6.0 || ^7.3 || ^8.0",
"symfony/cache": "^6.4.12 || ^7.3 || ^8.0",
"symfony/polyfill-php83": "^v1.32.0"
},
"require-dev": {
Expand Down Expand Up @@ -83,7 +84,7 @@
"clean": ["./vendor/bin/phpstan clear-result-cache", "./vendor/bin/psalm --clear-cache", "rm -rf tests/tmp/*.php"],
"sa": ["./vendor/bin/psalm --show-info=true", "./vendor/bin/phpstan analyse -c phpstan.neon"],
"metrics": ["./vendor/bin/phpmetrics --report-html=build/metrics --exclude=Exception --junit=build/junit.xml src"],
"phpmd": ["./vendor/bin/phpmd --exclude src/Annotation src text ./phpmd.xml"],
"phpmd": ["./vendor/bin/phpmd analyze src --format text --ruleset ./phpmd.xml"],
"demo": ["php demo/update-snapshots.php"],
"build": ["@cs", "@sa", "@pcov", "@metrics"]
},
Expand Down
3 changes: 2 additions & 1 deletion docs/llms-full.txt
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,8 @@ Configures Redis-backed storage.
use BEAR\QueryRepository\StorageRedisDsnModule;

new StorageRedisDsnModule('redis://localhost:6379');
// optional: $options, $defaultLifetime, $marshallingOptions (e.g. ['enabled' => true, 'type' => 'deflate'])
// optional: $options, $defaultLifetime, $marshallingOptions
// empty $marshallingOptions compress values with deflate; given options are used as they are, and ['enabled' => false] stores values uncompressed
```

### StorageMemcachedModule
Expand Down
7 changes: 5 additions & 2 deletions src/MarshallerProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
*/
final readonly class MarshallerProvider implements ProviderInterface
{
/** Used when no options are given; given options replace it as a whole, so `['enabled' => false]` stores values uncompressed */
private const DEFAULT_OPTIONS = ['enabled' => true, 'type' => 'deflate'];

/** @param array{enabled?: bool, type?: string, use_igbinary?: bool} $options Marshalling options */
public function __construct(
#[MarshallerOptions]
Expand All @@ -27,13 +30,13 @@ public function __construct(
#[Override]
public function get(): MarshallerInterface|null
{
return $this->createMarshaller($this->options);
return $this->createMarshaller($this->options === [] ? self::DEFAULT_OPTIONS : $this->options);
}

/** @param array{enabled?: bool, type?: string, use_igbinary?: bool} $options */
private function createMarshaller(array $options): MarshallerInterface|null
{
if (empty($options) || ($options['enabled'] ?? false) !== true) {
if (($options['enabled'] ?? false) !== true) {
return null;
}

Expand Down
4 changes: 3 additions & 1 deletion src/RedisDsnProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ public function __construct(
#[Override]
public function get(): Redis|RedisArray|RedisCluster|ClientInterface|Relay|RelayCluster
{
// Connect on first use: the pool is built at boot, where a compile has no store to reach.
// A `lazy` in the options or the DSN query still wins.
/** @var Redis|RedisArray|RedisCluster|ClientInterface|Relay|RelayCluster $connection */
$connection = RedisAdapter::createConnection($this->dns, $this->options);
$connection = RedisAdapter::createConnection($this->dns, $this->options + ['lazy' => true]);

return $connection;
}
Expand Down
4 changes: 3 additions & 1 deletion src/StorageRedisDsnModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ final class StorageRedisDsnModule extends AbstractModule
* @param int $defaultLifetime Default lifetime for cache items in seconds
* @param array<string, mixed> $marshallingOptions Marshalling options
*
* Example with compression:
* Empty $marshallingOptions compress values with deflate. Given options are used as they are, so a marshaller is
* set only with `'enabled' => true`:
* <code>
* new StorageRedisDsnModule('redis://localhost:6379', marshallingOptions: ['enabled' => false]); // uncompressed
* new StorageRedisDsnModule(
* dsn: 'redis://localhost:6379',
* marshallingOptions: [
Expand Down
4 changes: 2 additions & 2 deletions tests/MarshallerProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ public function testGetMarshallerWithInvalidType(): void
$this->assertInstanceOf(DefaultMarshaller::class, $marshaller);
}

public function testGetMarshallerWithEmptyOptions(): void
public function testGetMarshallerWithEmptyOptionsCompressesWithDeflate(): void
{
$provider = new MarshallerProvider([]);
$marshaller = $provider->get();

$this->assertNull($marshaller);
$this->assertInstanceOf(DeflateMarshaller::class, $marshaller);
}

public function testGetMarshallerWithDeflateTypeAndIgbinary(): void
Expand Down
52 changes: 52 additions & 0 deletions tests/StorageRedisDsnModuleLazyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace BEAR\QueryRepository;

use BEAR\RepositoryModule\Annotation\ResourceObjectPool;
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
use PHPUnit\Framework\TestCase;
use Ray\Di\Injector;
use Symfony\Component\Cache\Adapter\TagAwareAdapterInterface;
use Symfony\Component\Cache\Exception\InvalidArgumentException;

/**
* The pool is part of the graph an application builds at boot, and a machine that compiles the
* application may have no store to reach.
*/
#[RequiresPhpExtension('redis')]
final class StorageRedisDsnModuleLazyTest extends TestCase
{
/** A closed port: connection refused at once, no service to install. */
private const UNREACHABLE = 'redis://127.0.0.1:1';

public function testThePoolIsBuiltWithoutReachingTheStore(): void
{
$pool = $this->pool(new StorageRedisDsnModule(self::UNREACHABLE));

$this->assertFalse($pool->getItem('key')->isHit(), 'the store is first reached on use, and its failure reads as a miss');
}

public function testLazyFalseConnectsWhenThePoolIsBuilt(): void
{
$this->expectException(InvalidArgumentException::class);

$this->pool(new StorageRedisDsnModule(self::UNREACHABLE, ['lazy' => false]));
}

public function testLazyInTheDsnQueryWinsOverTheDefault(): void
{
$this->expectException(InvalidArgumentException::class);

$this->pool(new StorageRedisDsnModule(self::UNREACHABLE . '?lazy=0'));
}

private function pool(StorageRedisDsnModule $storage): TagAwareAdapterInterface
{
$module = new QueryRepositoryModule();
$module->override($storage);

return (new Injector($module))->getInstance(TagAwareAdapterInterface::class, ResourceObjectPool::class);
}
}
2 changes: 1 addition & 1 deletion vendor-bin/tools/composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"require-dev": {
"doctrine/coding-standard": "^14.0",
"phpmd/phpmd": "^2.15",
"phpmd/phpmd": "^3.0",
"phpstan/phpstan": "^2.0",
"rector/rector": "^2.0",
"rector/swiss-knife": "^2.3",
Expand Down
Loading
Loading