Nette Documentation Preview

syntax
Upgrading
*********


Upgrading to Version 4.0
========================

The minimum required PHP version is 8.4. The `{cache}` tag requires Latte 3.1.

.[tip]
A part of the work can be done for you by [DressCode |tools:coding-standard#Newer PHP and Nette] with the `dresscode/upgrade-nette` package: it rewrites renamed classes and constants and reports what it cannot rewrite. See [Upgrading Nette |https://dresscode.run/en/upgrading-nette].

The interfaces `Nette\Caching\IStorage`, `Nette\Caching\IBulkReader` and `Nette\Caching\Storages\IJournal` have been removed; use `Nette\Caching\Storage`, `Nette\Caching\BulkReader` and `Nette\Caching\Storages\Journal`. The service aliases `cacheStorage` and `nette.cacheJournal` have been removed as well; use the services `cache.storage` and `cache.journal`, or autowiring.

The uppercase constants such as `Cache::EXPIRE` or `Cache::TAGS` and the method `Cache::start()` now trigger `E_USER_DEPRECATED`. Use `Cache::Expire`, `Cache::Tags` and `capture()`. Passing a closure as the data to `Cache::save()` is deprecated, pass it as the generator of `load()`.

Dependencies can be written as the new object `Nette\Caching\Dependencies`, see [Expiration and Invalidation |expiration]. The array with `Cache::*` keys still works, but an unknown key or a value of a wrong type now throws an exception. Typically, this concerns a priority written as a string, such as `'50'`, which has to be an integer.

The method `Cache::clean()` takes the conditions as named arguments, such as `$cache->clean(tags: ['article/10'])`; the array is validated the same way as the dependencies. Several things behave differently:

- `clean()` without any condition throws an exception. To remove expired items, call `clean([])` on the storage.
- `all` on a cache with a namespace removes only that namespace and those derived from it; previously, it cleared the whole storage. To clear everything, call it on a cache without a namespace, or call `$storage->clean([Cache::All => true])`.
- Cleaning by `Cache::Namespaces` also removes the namespaces derived from the given ones by `derive()`.

A storage throws `Nette\NotSupportedException` for a dependency or a condition it cannot honor, instead of silently ignoring it. `SQLiteStorage` does not support priorities or dependencies on files, constants, callbacks and other items; `MemcachedStorage` does not support cleaning by namespaces. See the overview of [storages |storages#Choosing a Storage].

`PsrCacheAdapter` now validates keys as PSR-16 requires, so it rejects keys containing any of `{}()/\@:`, such as `user:1`.

If you have written your own storage, remove the method `lock()`, which no longer exists, and declare the return type `mixed` of the method `read()`. A storage that should let only one process generate a missing item implements the interface `Nette\Caching\LockingReader`. A subclass of `Cache` that overrides `clean()` must declare its new parameters.


Upgrading to Version 3.1
========================

- the method `Nette\Caching\Cache::start()` was renamed to `capture()`


Upgrading to Version 2.4
========================

- the class `Nette\Caching\Storages\FileJournal` is no longer available


Upgrading to Version 2.3
========================

- the ancient and deprecated ArrayAccess syntax `$val = $cache[$key]` or `$cache[$key] = $val` triggers `E_USER_DEPRECATED`; use `$cache->load($key)` and `$cache->save($key, $val)` instead

{{maintitle: Upgrading Nette Caching: What Changed in Each Version}}
{{description: What changed in Nette Caching 4.0, 3.1 and older versions, and how to update your code when moving to a newer version.}}

Upgrading

Upgrading to Version 4.0

The minimum required PHP version is 8.4. The {cache} tag requires Latte 3.1.

A part of the work can be done for you by DressCode with the dresscode/upgrade-nette package: it rewrites renamed classes and constants and reports what it cannot rewrite. See Upgrading Nette.

The interfaces Nette\Caching\IStorage, Nette\Caching\IBulkReader and Nette\Caching\Storages\IJournal have been removed; use Nette\Caching\Storage, Nette\Caching\BulkReader and Nette\Caching\Storages\Journal. The service aliases cacheStorage and nette.cacheJournal have been removed as well; use the services cache.storage and cache.journal, or autowiring.

The uppercase constants such as Cache::EXPIRE or Cache::TAGS and the method Cache::start() now trigger E_USER_DEPRECATED. Use Cache::Expire, Cache::Tags and capture(). Passing a closure as the data to Cache::save() is deprecated, pass it as the generator of load().

Dependencies can be written as the new object Nette\Caching\Dependencies, see Expiration and Invalidation. The array with Cache::* keys still works, but an unknown key or a value of a wrong type now throws an exception. Typically, this concerns a priority written as a string, such as '50', which has to be an integer.

The method Cache::clean() takes the conditions as named arguments, such as $cache->clean(tags: ['article/10']); the array is validated the same way as the dependencies. Several things behave differently:

A storage throws Nette\NotSupportedException for a dependency or a condition it cannot honor, instead of silently ignoring it. SQLiteStorage does not support priorities or dependencies on files, constants, callbacks and other items; MemcachedStorage does not support cleaning by namespaces. See the overview of storages.

PsrCacheAdapter now validates keys as PSR-16 requires, so it rejects keys containing any of {}()/\@:, such as user:1.

If you have written your own storage, remove the method lock(), which no longer exists, and declare the return type mixed of the method read(). A storage that should let only one process generate a missing item implements the interface Nette\Caching\LockingReader. A subclass of Cache that overrides clean() must declare its new parameters.

Upgrading to Version 3.1

Upgrading to Version 2.4

Upgrading to Version 2.3