Command Palette

Search for a command to run...

UnylyUnyly
Назад к скиллам

nette-caching

БесплатноБез исполняемых скриптовНе проверен

Invoke before writing or reviewing code that uses nette/caching. Covers the Cache object and namespaces, load() with a generator callback, save(), remove()…

netteот nette★ 47 звёзд на GitHub

Об этом скилле

Nette Caching

composer require nette/caching

Version 4.0 requires PHP 8.4. What differs from 3.x is in Upgrading; check the installed version before relying on Dependencies or named arguments of clean().

Dependencies

Since 4.0 dependencies are the object Nette\Caching\Dependencies with the properties expire, sliding, tags, priority, files, items, constants and callbacks. A generator whose parameter is typed Dependencies gets the object; an untyped one gets the array by reference.

use Nette\Caching\Cache;
use Nette\Caching\Dependencies;

$cache = new Cache($storage, 'articles'); // Nette\Caching\Storage is autowired
$sub = $cache->derive('previews');        // nested namespace
$html = $cache->load("article/$id", function (Dependencies $dp) use ($id) {
	$dp->expire = '20 minutes';
	$dp->tags = ["article/$id"];
	return renderArticle($id);
});
$cache->save($key, $value, new Dependencies(expire: '1 hour', tags: ['articles']));

The array form with Cache::* keys still works. Since 4.0 an unknown key or a wrong type throws, typically a priority written as the string '50'. The constants are PascalCase:

Cache::Expire      // 'expire'   – NOT Cache::Expiration (that constant does not exist)
Cache::Sliding     // 'sliding'
Cache::Tags        // 'tags'
Cache::Files       // 'files'     – resolved with filemtime(), so a relative path depends on the CWD; use absolute
Cache::Items       // 'items'     – other cache keys
Cache::Constants   // 'consts'
Cache::Callbacks   // 'callbacks' – [callable, ...args], must return true to stay valid.
                   //               The callable is SERIALIZED with the entry, so a closure is
                   //               a fatal ("Serialization of 'Closure' is not allowed") –
                   //               use a function name or [Class::class, 'method'].
Cache::Priority    // 'priority'  – int

Cache::EXPIRATION and the other UPPER_CASE names are deprecated aliases (E_USER_DEPRECATED since 4.0); Cache::Expiration does not exist and is a fatal Undefined constant. There is likewise no forget(), no delete() and no has() – removal is remove($key).

Reading and Writing

  • Prefer load() with a generator to save(): only with a generator do FileStorage and RedisStorage protect against cache stampede. If the generator throws, nothing is stored. A generator must not load() its own key: FileStorage throws InvalidStateException.
  • load($key) without a generator returns null on a miss, so null is not storable: save($key, null) removes the entry, as does an expire resolving to <= 0.
  • expire accepts seconds, a UNIX timestamp, a DateTimeInterface or any DateTime::from() string. Keys may be any serializable value, not just strings; save() returns the value.
  • save() with a closure as the data is deprecated; use load().

Cleaning

$cache->clean(tags: ["article/$id"]);  // in every namespace of the storage
$cache->clean(priority: 50);           // priority <= 50; items without priority stay
$cache->clean(namespaces: ['articles']);
$cache->clean(all: true);              // only this namespace and those derived from it

Since 4.0 clean() takes named arguments (3.x: clean([Cache::Tags => [...]]), which still works). clean() with no condition throws; expired items are removed by $storage->clean([]). all on a cache with a namespace no longer wipes the storage, only a cache without one does.

Storages and the Journal

A storage throws Nette\NotSupportedException for a dependency or condition it cannot honor (3.x silently ignored them):

Storage Tags, priority Files, constants, callbacks Items Sliding Clean namespaces Stampede
FileStorage (default) journal yes yes yes yes yes
RedisStorage yes yes no yes yes yes
MemcachedStorage journal yes no yes no no
SQLiteStorage tags only no no yes yes no
MemoryStorage yes ignored ignored yes yes no
Psr16Storage journal yes no no no no

"journal": without one, a write with tags or priority throws Nette\InvalidStateException: CacheJournal has not been provided. The default journal is SQLiteJournal, and the DI extension registers it only if pdo_sqlite is loaded – without that extension every tagged save() blows up at runtime. MemoryStorage silently ignores what it cannot do, so in tests an item that would expire in production stays.

Nette\Bridges\Psr\PsrCacheAdapter exposes a storage as PSR-16 and since 4.0 rejects keys containing {}()/\@: such as user:1; Psr16Storage is the opposite direction.

$values = $cache->bulkLoad($keys, fn($key, Dependencies $dp) => compute($key)); // scalar keys only
$cache->bulkSave([$k1 => $v1, $k2 => $v2], new
Полный SKILL.md на GitHub

Установить nette-caching в Claude Code и Claude Desktop

Зарегайся, чтобы установить скилл

Создай бесплатный аккаунт, чтобы открыть команду установки и сохранить скилл в библиотеку.

  • Открой команду установки в одну строку
  • Сохраняй скиллы в синхронизируемую библиотеку
  • Уведомления, когда скиллы обновляются
Зарегаться бесплатноУ меня уже есть аккаунт

Разрешённые инструменты

Инструменты, которые скиллу разрешено вызывать.

Без ограничений — скилл может использовать любой инструмент.

FAQ

Что делает скилл nette-caching?

Invoke before writing or reviewing code that uses nette/caching. Covers the Cache object and namespaces, load() with a generator callback, save(), remove(), clean() with named conditions, the Dependencies object and the Cache::* dependency array (Expire, Tags, Files, Items, Priority, Sliding, Callbacks), the Journal requirement for tags and priorities, bulkLoad(), output capture, storages (FileStorage, RedisStorage, MemcachedStorage, SQLiteStorage, MemoryStorage, Psr16Storage, DevNullStorage), PSR-16 adapters and the {cache} Latte tag. Also trigger for cache.storage or cache.journal in NEON, invalidation by tag, cache stampede, or disabling the cache in tests. Not for the Latte template cache or the DI container cache, which are separate mechanisms.

Как установить скилл nette-caching?

Скопируй папку скилла в ~/.claude/skills (вкладка Claude Code выше делает это одной командой), либо поставь как плагин.

Скилл nette-caching запускает скрипты?

Нет, скилл состоит только из инструкций (SKILL.md), без исполняемых скриптов.

Похожие скиллы

Сравнить nette-caching с