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

Cache ttl prolongation #161

Merged
merged 1 commit into from
Aug 26, 2024
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* __BREAKING__: The `HttpLoader::retryCachedErrorResponses()` method now returns an instance of the new `Crwlr\Crawler\Loader\Http\Cache\RetryManager` class. This class provides the methods `only()` and `except()` to restrict retries to specific HTTP response status codes. Previously, this method returned the `HttpLoader` itself (`$this`), so if you're using it in a chain and calling other loader methods after it, you will need to refactor your code.
* __BREAKING__: Removed the `Microseconds` class from this package. It has been moved to the `crwlr/utils` package, which you can use instead.

### Added
* New methods `FileCache::prolong()` and `FileCache::prolongAll()` to allow prolonging the time to live for cached responses.

## [1.10.0] - 2024-08-05
### Added
* URL refiners: `UrlRefiner::withScheme()`, `UrlRefiner::withHost()`, `UrlRefiner::withPort()`, `UrlRefiner::withoutPort()`, `UrlRefiner::withPath()`, `UrlRefiner::withQuery()`, `UrlRefiner::withoutQuery()`, `UrlRefiner::withFragment()` and `UrlRefiner::withoutFragment()`.
Expand Down
8 changes: 8 additions & 0 deletions src/Cache/CacheItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ public function isExpired(): bool
return time() > $this->createdAt->add($ttl)->getTimestamp();
}

/**
* Get a new instance with same data but a different time to live.
*/
public function withTtl(DateInterval|int $ttl): CacheItem
{
return new CacheItem($this->value, $this->key, $ttl, $this->createdAt);
}

/**
* @return mixed[]
*/
Expand Down
48 changes: 41 additions & 7 deletions src/Cache/FileCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,20 +82,25 @@ public function set(string $key, mixed $value, DateInterval|int|null $ttl = null
$value = new CacheItem($value->value(), $key, $ttl ?? $value->ttl);
}

$content = serialize($value);

if ($this->useCompression) {
$content = $this->encode($content);
}

return file_put_contents($this->basePath . '/' . $key, $content) !== false;
return $this->saveCacheItem($value);
}

public function delete(string $key): bool
{
return unlink($this->basePath . '/' . $key);
}

public function prolong(string $key, DateInterval|int $ttl): bool
{
try {
$item = $this->getCacheItem($key);

return $this->saveCacheItem($item->withTtl($ttl));
} catch (Throwable) {
return false;
}
}

/**
* @throws InvalidArgumentException
*/
Expand All @@ -114,6 +119,21 @@ public function clear(): bool
return true;
}

public function prolongAll(DateInterval|int $ttl): bool
{
$allFiles = scandir($this->basePath);

if (is_array($allFiles)) {
foreach ($allFiles as $file) {
if ($file !== '.' && $file !== '..' && $file !== '.gitkeep' && !$this->prolong($file, $ttl)) {
return false;
}
}
}

return true;
}

/**
* @return iterable<mixed>
* @throws MissingZlibExtensionException|ReadingCacheFailedException|InvalidArgumentException
Expand Down Expand Up @@ -176,6 +196,20 @@ protected function getCacheItem(string $key): CacheItem
return $unserialized;
}

/**
* @throws MissingZlibExtensionException
*/
protected function saveCacheItem(CacheItem $item): bool
{
$content = serialize($item);

if ($this->useCompression) {
$content = $this->encode($content);
}

return file_put_contents($this->basePath . '/' . $item->key(), $content) !== false;
}

protected function unserialize(string $content): mixed
{
// Temporarily set a new error handler, so unserializing a compressed string does not result in a PHP warning.
Expand Down
4 changes: 4 additions & 0 deletions src/Logger/CliLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ public function debug(string|Stringable $message, array $context = []): void
$this->log('debug', $message, $context);
}

/**
* @param string $level
* @param mixed[] $context
*/
public function log($level, string|Stringable $message, array $context = []): void
{
if (!is_string($level)) {
Expand Down
Loading