📦 Cache Class
A simple wrapper around modx()->cacheManager for convenient cache handling in MODX. Supports storing, retrieving, clearing, deleting cache entries, as well as working with the remember() pattern.
🔹 Cache::get(string $key, array $options = []): mixed
Get a value from cache by key.
Example:
php
$data = Cache::get('my_key');🔹 Cache::set(string $key, $value, int $ttl = 3600, array $options = []): bool
Store a value in the cache.
$ttl— time to live in seconds (0means forever).
Example:
php
Cache::set('my_key', ['foo' => 'bar'], 600);🔹 Cache::add(string $key, $value, int $ttl = 3600, array $options = []): bool
Add a value to cache only if it does not already exist.
Example:
php
Cache::add('unique_key', 'value');🔹 Cache::replace(string $key, $value, int $ttl = 3600, array $options = []): bool
Replace a cached value only if the key already exists.
Example:
php
Cache::replace('existing_key', 'new_value');🔹 Cache::delete(string $key, array $options = []): bool
Remove a key from cache.
Example:
php
Cache::delete('temp_key');🔹 Cache::clear(string $cacheKey = 'pageblocks'): bool
Clear both logical cache and physical files for the given cache key.
Example:
php
Cache::clear(); // deletes all files in core/cache/pageblocks and clears MODX cache🔹 Cache::remember(string $key, int $ttl, callable $callback, array $options = [], bool &$fromCache = false): mixed
Retrieve from cache or compute and store if not found.
Example:
php
$user = Cache::remember('user_42', 3600, function () {
return getUserFromDB(42);
}, [], $wasCached);
if ($wasCached) {
echo 'Loaded from cache';
}🔹 Cache::rememberForever(string $key, callable $callback, array $options = []): mixed
Same as remember, but stores the value forever.
Example:
php
$config = Cache::rememberForever('site_config', function () {
return loadConfig();
});🔹 Cache::generateObject(string $class, array $data): \xPDOObject
Create and return a new MODX object populated with data.
Example:
php
$object = Cache::generateObject(MyClass::class, ['name' => 'John']);