Skip to content

Added probabilistic cleaning to SQLiteStorage - addresses Issue #83 #84

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

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion .github/workflows/static-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
- uses: actions/checkout@v4
- uses: shivammathur/setup-php@v2
with:
php-version: 8.0
php-version: 8.1
coverage: none

- run: composer install --no-progress --prefer-dist
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
php: ['8.0', '8.1', '8.2', '8.3', '8.4']
php: ['8.1', '8.2', '8.3', '8.4']

fail-fast: false

Expand Down Expand Up @@ -37,7 +37,7 @@ jobs:
- uses: actions/checkout@v4
- uses: shivammathur/setup-php@v2
with:
php-version: 8.0
php-version: 8.1
coverage: none

- run: composer update --no-progress --prefer-dist --prefer-lowest --prefer-stable
Expand All @@ -52,7 +52,7 @@ jobs:
- uses: actions/checkout@v4
- uses: shivammathur/setup-php@v2
with:
php-version: 8.0
php-version: 8.1
coverage: none

- run: composer install --no-progress --prefer-dist
Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@
}
],
"require": {
"php": "8.0 - 8.4",
"php": "8.1 - 8.4",
"nette/utils": "^4.0"
},
"require-dev": {
"nette/tester": "^2.4",
"nette/di": "^3.1 || ^4.0",
"latte/latte": "^2.11 || ^3.0.12",
"latte/latte": "^3.0.12",
"tracy/tracy": "^2.9",
"psr/simple-cache": "^2.0 || ^3.0",
"phpstan/phpstan-nette": "^2.0@stable"
},
"conflict": {
"latte/latte": ">=3.0.0 <3.0.12"
"latte/latte": "<3.0.12"
},
"suggest": {
"ext-pdo_sqlite": "to use SQLiteStorage or SQLiteJournal"
Expand All @@ -45,7 +45,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "3.3-dev"
"dev-master": "4.0-dev"
}
}
}
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Installation
composer require nette/caching
```

It requires PHP version 8.0 and supports PHP up to 8.4.
It requires PHP version 8.1 and supports PHP up to 8.4.


Basic Usage
Expand Down
8 changes: 0 additions & 8 deletions src/Bridges/CacheDI/CacheExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,5 @@ public function loadConfiguration(): void
$builder->addDefinition($this->prefix('storage'))
->setType(Nette\Caching\Storage::class)
->setFactory(Nette\Caching\Storages\FileStorage::class, [$this->tempDir]);

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

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

This file was deleted.

7 changes: 3 additions & 4 deletions src/Caching/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,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 @@ -386,6 +384,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 All @@ -395,7 +394,7 @@ public function start($key): ?OutputHelper
*/
protected function generateKey($key): string
{
return $this->namespace . md5(is_scalar($key) ? (string) $key : serialize($key));
return $this->namespace . hash('xxh128', is_scalar($key) ? (string) $key : serialize($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
8 changes: 8 additions & 0 deletions src/Caching/Storages/SQLiteStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class SQLiteStorage implements Nette\Caching\Storage, Nette\Caching\BulkReader
{
private \PDO $pdo;

/** probability that the clean() routine is started */
public static float $gcProbability = 0.001;

public function __construct(string $path)
{
Expand All @@ -46,6 +48,12 @@ public function __construct(string $path)
CREATE INDEX IF NOT EXISTS tags_tag ON tags(tag);
PRAGMA synchronous = OFF;
');

// should we run the clean function to remove expired cached items?
if (mt_rand() / mt_getrandmax() < static::$gcProbability) {
$this->clean([]);
}

}


Expand Down
4 changes: 0 additions & 4 deletions tests/Bridges.DI/CacheExtension.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,4 @@ test('', function () {

$storage = $container->getService('cache.storage');
Assert::type(Nette\Caching\Storages\FileStorage::class, $storage);

// aliases
Assert::same($journal, $container->getService('nette.cacheJournal'));
Assert::same($storage, $container->getService('cacheStorage'));
});
48 changes: 0 additions & 48 deletions tests/Bridges.Latte2/CacheMacro.cache.phpt

This file was deleted.

Loading