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
9 changes: 8 additions & 1 deletion docs/Error-Handling.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,17 @@ try {
$cache = Cache::create(Redis::class, ['host' => 'redis.internal']);
$cache->has('ping'); // force the lazy connection now, so we fail fast
} catch (CacheException $e) {
$cache = Cache::create(File::class, ['path' => sys_get_temp_dir() . '/cache']);
// A directory that belongs to the application, not a shared one such as
// sys_get_temp_dir(): the File handler unserialize()s what it finds there.
$cache = Cache::create(File::class, ['path' => __DIR__ . '/var/cache']);
}
```

Do not point the `File` fallback at `/tmp` or any other directory other users can
write to. Another local user could pre-create it and plant a file in it, and
the handler would deserialize that file. Keep the directory owned by the
application user (mode `0700`); see [File Handler](File-Handler.md#usage).

## Next steps

- [Keys, TTL & Values](Keys-TTL-and-Values.md) — what makes a key invalid.
Expand Down
8 changes: 7 additions & 1 deletion docs/File-Handler.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,18 @@ A quick bootstrap:
```php
$dir = __DIR__ . '/var/cache';
if (!is_dir($dir)) {
mkdir($dir, 0775, true);
mkdir($dir, 0700, true);
}

$cache = Cache::create(File::class, ['path' => $dir]);
```

> **Security:** use a directory that belongs to the application (owned by the
> web/CLI user, mode `0700`), never `/tmp` or another location other users can
> write to. Cache files are PHP-`serialize()`d and read back with `unserialize()`,
> so a file planted by someone else becomes a deserialization input. Keep the
> directory outside the web root as well.

If `path` is missing or empty, any operation that touches the filesystem throws a
[`CacheException`](Error-Handling.md).

Expand Down
2 changes: 1 addition & 1 deletion docs/Installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ use InitPHP\Cache\Cache;
use InitPHP\Cache\Handler\File;

$dir = __DIR__ . '/var/cache';
@mkdir($dir, 0775, true);
@mkdir($dir, 0700, true); // private to the application: cache files are unserialized

$cache = Cache::create(File::class, ['path' => $dir]);

Expand Down
15 changes: 12 additions & 3 deletions docs/Recipes.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,24 @@ function fetchJson(CacheInterface $cache, string $url, int $ttl = 600): array

## Fragment caching (rendered HTML)

A fragment that depends on the current user must be cached **per user**: put the
user's id in the key. A fixed key such as `frag_nav` would serve the first user's
menu, name and any tokens in it to everyone until the entry expires.

```php
function renderNav(CacheInterface $cache, callable $render): string
function renderNav(CacheInterface $cache, ?int $userId, callable $render): string
{
return remember($cache, 'frag_nav', 600, $render);
// $userId is null for a guest, who gets one shared "guest" fragment: the guest
// fragment must not contain a CSRF token or other per-session data.
return remember($cache, 'frag_nav_' . ($userId ?? 'guest'), 600, $render);
}

echo renderNav($cache, fn () => view('partials/nav', ['user' => $user]));
echo renderNav($cache, $user?->id, fn () => view('partials/nav', ['user' => $user]));
```

Only fragments that are identical for every visitor (a footer, a public
category list) may use a fixed key.

## Pick a backend per environment

Develop on the filesystem, run Redis in production — one place decides:
Expand Down
2 changes: 1 addition & 1 deletion docs/Testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ final class PriceServiceFileTest extends TestCase
protected function setUp(): void
{
$this->dir = sys_get_temp_dir() . '/cache-test-' . uniqid();
mkdir($this->dir);
mkdir($this->dir, 0700);
$this->cache = Cache::create(File::class, ['path' => $this->dir]);
}

Expand Down
6 changes: 3 additions & 3 deletions docs/The-Cache-Factory.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ config) and when you want a single place that validates the class.
Option keys are normalised to lower case, so these are all the same option:

```php
Cache::create(File::class, ['PATH' => '/tmp/cache']);
Cache::create(File::class, ['Path' => '/tmp/cache']);
Cache::create(File::class, ['path' => '/tmp/cache']);
Cache::create(File::class, ['PATH' => __DIR__ . '/var/cache']);
Cache::create(File::class, ['Path' => __DIR__ . '/var/cache']);
Cache::create(File::class, ['path' => __DIR__ . '/var/cache']);
```

Options you don't set fall back to the handler's defaults (and the shared
Expand Down
2 changes: 1 addition & 1 deletion docs/Troubleshooting.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ an empty one).

```php
$dir = __DIR__ . '/var/cache';
is_dir($dir) || mkdir($dir, 0775, true);
is_dir($dir) || mkdir($dir, 0700, true); // private to the application
$cache = Cache::create(File::class, ['path' => $dir]);
```

Expand Down
Loading