Download the PHP package webfiori/cache without Composer
On this page you can find all versions of the php package webfiori/cache. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download webfiori/cache
More information about webfiori/cache
Files in webfiori/cache
Package cache
Short Description A simple caching engine which is highly customizable.
License MIT
Informations about the package cache
WebFiori Cache
A simple, secure, and highly customizable caching engine for PHP. This library provides time-based caching with built-in encryption support, making it suitable for application-level, server-level, and database-level caching scenarios.
Features
- Instance-based API — fully DI-friendly, multiple cache pools can coexist
- Static facade (
CacheFacade) for quick usage without DI - Time-based caching with configurable TTL (Time-to-Live)
- Built-in encryption for sensitive data protection
- Multiple storage backends (File-based and Redis included, extensible via
Storageinterface) - Key prefixing for namespace isolation (
withPrefix()returns a new instance — no mutation) - Expired item cleanup via
purgeExpired() - Comprehensive error handling with custom exceptions
Installation
Supported PHP Versions
This library requires PHP 8.1 or higher.
| Build Status |
|---|
Quick Start
For a complete runnable version of this, see the Set and Get example.
Usage
Basic Operations
Working examples for each operation: basic examples
Prefix Isolation
withPrefix() returns a new Cache instance — the original is never mutated.
See Prefix Flush.
Static Facade
For quick usage without dependency injection, use CacheFacade:
Security & Encryption
Cached data is encrypted using AES-256-CBC when an encryption key is available. If no key is set, data is stored unencrypted. Configure via environment variables or programmatically:
See Encrypt/Decrypt Round-Trip.
Redis Storage
A built-in Redis backend is available via RedisStorage. It requires the ext-redis PHP extension and a connected \Redis instance:
Redis handles TTL natively, so expired items are removed automatically — no need for purgeExpired().
See Redis Storage for a full example.
Custom Storage Drivers
Implement the Storage interface to create custom backends (e.g., Memcached, database):
Full working implementation: Custom Storage Driver.
API Reference
Cache Class (Instance Methods)
| Method | Description | Returns |
|---|---|---|
get($key, $generator, $ttl, $params) |
Retrieve or create cache item | mixed |
set($key, $data, $ttl, $override) |
Store cache item | bool |
has($key) |
Check if item exists and is not expired | bool |
delete($key) |
Remove cache item | void |
flush() |
Clear all cache (respects current prefix) | void |
getItem($key) |
Get Item object with full metadata |
Item\|null |
setTTL($key, $ttl) |
Update item TTL | bool |
isEnabled() / setEnabled($bool) |
Check or toggle caching | bool / void |
setDriver($driver) / getDriver() |
Set or get storage driver | void / Storage |
withPrefix($prefix) |
Returns new Cache instance with prefix |
Cache |
getPrefix() |
Get current prefix | string |
purgeExpired() |
Remove all expired items from storage | int |
CacheFacade Class (Static Methods)
Same methods as Cache, plus:
| Method | Description | Returns |
|---|---|---|
getInstance() |
Get the default Cache instance |
Cache |
setInstance($cache) |
Replace the default instance | void |
reset() |
Destroy the default instance (for testing) | void |
Item Class
| Method | Description | Returns |
|---|---|---|
getKey() |
Get item key | string |
getData() / getDataDecrypted() / getDataEncrypted() |
Get raw, decrypted, or encrypted data | mixed / mixed / string |
getTTL() / setTTL($ttl) |
Get or set time-to-live | int / void |
getCreatedAt() / getExpiryTime() |
Get creation or expiry timestamp | int |
getSecurityConfig() / setSecurityConfig($config) |
Get or set security configuration | SecurityConfig / void |
generateKey() |
Generate encryption key (static) | string |
Storage Interface
| Method | Description |
|---|---|
store(Item $item) |
Store cache item |
read(string $key, ?string $prefix) |
Read item data |
readItem(string $key, ?string $prefix) |
Read item as Item object |
has(string $key, ?string $prefix) |
Check item existence |
delete(string $key) |
Delete item |
flush(?string $prefix) |
Clear cache |
purgeExpired() |
Remove expired items, return count removed |
Error Handling
| Exception | When |
|---|---|
InvalidCacheKeyException |
Empty key, key > 250 chars, key with control characters |
CacheStorageException |
File system errors, invalid paths, permission issues |
CacheDriverException |
Invalid driver implementation |
CacheException |
Base exception, missing encryption key |
See Error Handling for all cases.
Upgrading from v2
v3 changes Cache from a static singleton to an instance class. Migration is straightforward:
If you prefer the static API, use CacheFacade — it works identically to v2's Cache:
Examples
The examples/ directory contains 23 self-contained, runnable samples organized by difficulty:
Basic — Core operations every user needs:
| # | Example | What it demonstrates |
|---|---|---|
| 01 | Set and Get | set() and get() |
| 02 | Get with Generator | Auto-populate on cache miss |
| 03 | Generator with Params | Pass arguments to the callback |
| 04 | Check Existence | has() |
| 05 | Delete Item | delete() |
| 06 | Flush Cache | flush() |
| 07 | Update TTL | setTTL() |
| 08 | Override Behavior | set() with/without override |
| 09 | TTL Expiration | Items expire after TTL |
| 10 | Enable / Disable | Toggle caching on/off |
| 11 | Item Inspection | Read item metadata |
| 12 | Data Types | Strings, arrays, objects, edge cases |
Advanced — Security, extensibility, and error handling:
| # | Example | What it demonstrates |
|---|---|---|
| 01 | Prefix Isolation | Namespace isolation |
| 02 | Prefix Flush | Selective flush by prefix |
| 03 | Encryption Key Management | KeyManager usage |
| 04 | Security Config | Algorithm and permissions |
| 05 | Encryption Disabled | Store without encryption |
| 06 | Encrypt/Decrypt Round-Trip | Full encryption cycle |
| 07 | Custom Storage Driver | Implement Storage interface |
| 08 | File Storage Custom Path | Custom cache directory |
| 09 | File Permissions | Default and custom permissions |
| 10 | Error Handling | All exception types |
| 11 | Factory and DI | Constructor-based DI |
| 12 | Redis Storage | Redis backend driver |
Run any example:
License
This library is licensed under MIT License. See LICENSE file for more details.
Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Run the test suite:
composer test - Submit a pull request
Support
- Issues: GitHub Issues
- Documentation: This README and examples/