CacheManager bean, and the attributes remain identical regardless of the backend.
Enabling Caching
Before any caching attributes take effect, annotate your main application class with#[EnableCaching]. This tells Winter Boot to scan all beans for caching attributes and create the necessary AOP proxies.
MyApplication.php
#[Cacheable]
#[Cacheable] is a method-level attribute. The first time a method is called for a given cache key, Winter Boot executes the method and stores the return value in the cache. On subsequent calls with the same key the cached value is returned directly and the method body is not executed.
Options
string|string[]
default:"\"default\""
One or more cache container names where the result will be stored. Accepts a single string or an array of strings.
string
default:"Method name + arguments"
The key under which the value is cached. Supports
#{param} expression interpolation.string
default:"Framework managed"
Bean name of a class implementing
KeyGenerator for custom key generation logic.string
default:"Framework managed"
Bean name of a class implementing
CacheManager. Use when you have multiple managers registered.string
default:"Framework managed"
Bean name of a class implementing
CacheResolver for dynamic cache resolution at runtime.string
default:"\"\""
SpEL-style expression. When non-empty, caching only applies if the expression evaluates to
true.string
default:"\"\""
SpEL-style expression. When non-empty, the result is not cached if the expression evaluates to
true.Examples
#[CachePut]
#[CachePut] is a method-level attribute that always executes the underlying method and then writes the fresh return value to the cache. Use it when you need to keep the cache up to date after a write operation. #[CachePut] accepts the same options as #[Cacheable].
#[CacheEvict]
#[CacheEvict] is a method-level attribute that removes one or more entries from the cache when the annotated method is called. You can target a specific entry using the key option or flush an entire cache container by setting allEntries: true.
Additional Options
bool
default:"false"
When
true, all entries in the specified cache(s) are removed rather than just the entry matching key.bool
default:"false"
When
true, the cache is cleared before the method executes. The default clears the cache after successful execution.Example — Clear All Entries
Cache Backends
Winter Boot ships with multiple cache backend options. Choose the one that fits your deployment topology.In-Memory
Zero-config default. Fast and simple, but not shared across processes or nodes.
SharedKvCache
Shared across all processes on the same node. Ideal for single-node deployments.
RedisCache
Distributed cache shared across the entire cluster. Requires
winter-data-redis.Default In-Memory Cache
Out of the box, Winter Boot registers aSimpleCacheManager backed by a fast PHP in-memory store. No additional configuration is required — add #[Cacheable] to your methods and caching works immediately.
SharedKvCache — Local Node KV Store
SharedKvCache is backed by a local Key-Value store that is shared across all processes on the same node. It is a good fit for single-node deployments or for data that does not need to be distributed across a cluster.
CacheConfiguration controls the eviction and expiry policy per cache container:
int
default:"PHP_INT_MAX - 1"
Maximum number of entries before LRU eviction kicks in.
int
default:"-1"
Milliseconds after which a written entry expires.
-1 means entries never expire.int
default:"-1"
Milliseconds after the last access after which an entry expires.
CacheConfig.php
RedisCache — Distributed Caching
For multi-node deployments where all nodes must share the same cached data,RedisCache provides a distributed cache backed by Redis. It is available in the winter-data-redis module.
When you define a custom
CacheManager bean with a specific name (e.g. "redisCacheManager"), you must pass that name via the cacheManager option on your caching attributes.1
Define the Redis CacheManager bean
CacheConfig.php
2
Reference the bean name in your caching attributes
StockService.php