Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Implement ability to drop cache keys using combination of OR an… #75

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
10 changes: 6 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@
],
"require": {
"php": "8.0 - 8.3",
"nette/finder": "^2.4 || ^3.0",
"nette/utils": "^3.2 || ~4.0.0"
"nette/utils": "^4.0"
},
"require-dev": {
"nette/tester": "^2.4",
"nette/di": "^3.1 || ^4.0",
"latte/latte": "^2.11 || ^3.0",
"latte/latte": "^3.0.12",
"tracy/tracy": "^2.9",
"phpstan/phpstan": "^1.0"
},
"conflict": {
"latte/latte": "<3.0.12"
},
"suggest": {
"ext-pdo_sqlite": "to use SQLiteStorage or SQLiteJournal"
},
Expand All @@ -39,7 +41,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "3.2-dev"
"dev-master": "4.0-dev"
}
}
}
25 changes: 9 additions & 16 deletions src/Bridges/CacheDI/CacheExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,27 @@
namespace Nette\Bridges\CacheDI;

use Nette;
use Nette\Utils\FileSystem;


/**
* Cache extension for Nette DI.
*/
final class CacheExtension extends Nette\DI\CompilerExtension
{
private string $tempDir;


public function __construct(string $tempDir)
{
$this->tempDir = $tempDir;
public function __construct(
private string $tempDir,
) {
}


public function loadConfiguration()
public function loadConfiguration(): void
{
if (!FileSystem::isAbsolute($this->tempDir)) {
throw new Nette\InvalidArgumentException("Cache directory must be absolute, '$this->tempDir' given.");
}
$dir = $this->tempDir . '/cache';
Nette\Utils\FileSystem::createDir($dir);
FileSystem::createDir($dir);
if (!is_writable($dir)) {
throw new Nette\InvalidStateException("Make directory '$dir' writable.");
}
Expand All @@ -45,13 +46,5 @@ public function loadConfiguration()
$builder->addDefinition($this->prefix('storage'))
->setType(Nette\Caching\Storage::class)
->setFactory(Nette\Caching\Storages\FileStorage::class, [$dir]);

if ($this->name === 'cache') {
if (extension_loaded('pdo_sqlite')) {
$builder->addAlias('nette.cacheJournal', $this->prefix('journal'));
}

$builder->addAlias('cacheStorage', $this->prefix('storage'));
}
}
}
168 changes: 0 additions & 168 deletions src/Bridges/CacheLatte/CacheMacro.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Bridges/CacheLatte/Nodes/CacheNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class CacheNode extends StatementNode
/** @return \Generator<int, ?array, array{AreaNode, ?Tag}, static> */
public static function create(Tag $tag): \Generator
{
$node = new static;
$node = $tag->node = new static;
$node->args = $tag->parser->parseArguments();
[$node->content, $endTag] = yield;
$node->endLine = $endTag?->position;
Expand Down
13 changes: 5 additions & 8 deletions src/Caching/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
*/
class Cache
{
use Nette\SmartObject;

/** dependency */
public const
Priority = 'priority',
Expand Down Expand Up @@ -66,9 +64,7 @@ class Cache
public const ALL = self::All;

/** @internal */
public const
NamespaceSeparator = "\x00",
NAMESPACE_SEPARATOR = self::NamespaceSeparator;
public const NamespaceSeparator = "\x00";

private Storage $storage;
private string $namespace;
Expand Down Expand Up @@ -181,7 +177,7 @@ public function bulkLoad(array $keys, ?callable $generator = null): array
* Writes item into the cache.
* Dependencies are:
* - Cache::Priority => (int) priority
* - Cache::Expire => (timestamp) expiration
* - Cache::Expire => (timestamp) expiration, infinite if null
* - Cache::Sliding => (bool) use sliding expiration?
* - Cache::Tags => (array) tags
* - Cache::Files => (array|string) file names
Expand Down Expand Up @@ -281,13 +277,13 @@ public function remove(mixed $key): void
* Removes items from the cache by conditions.
* Conditions are:
* - Cache::Priority => (int) priority
* - Cache::Tags => (array) tags
* - Cache::Tags => (array) tags | CacheSelector
* - Cache::All => true
*/
public function clean(?array $conditions = null): void
{
$conditions = (array) $conditions;
if (isset($conditions[self::Tags])) {
if (isset($conditions[self::Tags]) && !$conditions[self::Tags] instanceof CacheSelector) {
$conditions[self::Tags] = array_values((array) $conditions[self::Tags]);
}

Expand Down Expand Up @@ -348,6 +344,7 @@ public function capture(mixed $key): ?OutputHelper
*/
public function start($key): ?OutputHelper
{
trigger_error(__METHOD__ . '() was renamed to capture()', E_USER_DEPRECATED);
return $this->capture($key);
}

Expand Down
34 changes: 34 additions & 0 deletions src/Caching/CacheSelector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Nette\Caching;

class CacheSelector
{
private array $conditions;


/**
* Adds where condition, more calls appends with AND.
* Pass tags as array to append with OR.
*
* Example:
* (new CacheSelector())->where("animal")->where("dog")->where(["brown", "white"])
* Creates condition looking for entities having tags animal and dog and (brown / white). Will not match entity, tagged animal, dog, black.
*
* @param string|array $tags tag names to select
*/
public function where(string|array $tags): static
{
$this->conditions[] = $tags;

return $this;
}


public function getConditions(): array
{
return $this->conditions;
}
}
2 changes: 0 additions & 2 deletions src/Caching/OutputHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
*/
class OutputHelper
{
use Nette\SmartObject;

public array $dependencies = [];
private ?Cache $cache;
private mixed $key;
Expand Down
3 changes: 1 addition & 2 deletions src/Caching/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ interface Storage
{
/**
* Read from cache.
* @return mixed
*/
function read(string $key);
function read(string $key): mixed;

/**
* Prevents item reading and writing. Lock is released by write() or remove().
Expand Down
2 changes: 0 additions & 2 deletions src/Caching/Storages/DevNullStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
*/
class DevNullStorage implements Nette\Caching\Storage
{
use Nette\SmartObject;

public function read(string $key): mixed
{
return null;
Expand Down
6 changes: 2 additions & 4 deletions src/Caching/Storages/FileStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
*/
class FileStorage implements Nette\Caching\Storage
{
use Nette\SmartObject;

/**
* Atomic thread safe logic:
*
Expand Down Expand Up @@ -57,8 +55,8 @@ class FileStorage implements Nette\Caching\Storage

public function __construct(string $dir, ?Journal $journal = null)
{
if (!is_dir($dir)) {
throw new Nette\DirectoryNotFoundException("Directory '$dir' not found.");
if (!is_dir($dir) || !Nette\Utils\FileSystem::isAbsolute($dir)) {
throw new Nette\DirectoryNotFoundException("Directory '$dir' not found or is not absolute.");
}

$this->dir = $dir;
Expand Down
Loading