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
- Time-based caching with configurable TTL (Time-to-Live)
- Built-in encryption for sensitive data protection
- Multiple storage backends (File-based included, extensible via Storage interface)
- Static API for backward compatibility and simple usage
- Key prefixing for namespace isolation
- Comprehensive error handling with custom exceptions
Table of Contents
- Installation
- Supported PHP Versions
- Quick Start
- Usage
- Basic Operations
- Advanced Usage
- Dependency Injection
- Security Configuration
- Custom Storage Drivers
- API Reference
- Configuration
- Security
- License
Installation
Using Composer
Manual Installation
- Download the latest release from GitHub releases
- Extract the files to your project directory
- Include the library in your project:
Note: Manual installation is not recommended. Use Composer for automatic dependency management.
Supported PHP Versions
This library requires PHP 8.1 or higher.
Build Status |
---|
Quick Start
Usage
Basic Operations
Creating Cache Items
Retrieving Items
Retrieve Only:
Retrieve or Create:
Other Operations
Check Item Existence:
Remove Single Item:
Clear All Cache:
Update TTL:
Enable/Disable Caching:
Advanced Usage
Working with Cache Items
Key Prefixing
Dependency Injection
For modern applications, use dependency injection instead of static methods:
Security Configuration
Environment Variables
Set these environment variables for security configuration:
Programmatic Configuration
Custom Storage Drivers
Implement the Storage
interface to create custom storage backends:
API Reference
Cache Class (Static Methods)
Method | Description | Parameters | Returns |
---|---|---|---|
get($key, $generator, $ttl, $params) |
Retrieve or create cache item | string $key , callable $generator , int $ttl = 60 , array $params = [] |
mixed |
set($key, $data, $ttl, $override) |
Store cache item | string $key , mixed $data , int $ttl = 60 , bool $override = false |
bool |
has($key) |
Check if item exists | string $key |
bool |
delete($key) |
Remove cache item | string $key |
void |
flush() |
Clear all cache | - | void |
getItem($key) |
Get detailed item info | string $key |
Item\|null |
setTTL($key, $ttl) |
Update item TTL | string $key , int $ttl |
bool |
isEnabled() |
Check if caching is enabled | - | bool |
setEnabled($enable) |
Enable/disable caching | bool $enable |
void |
setDriver($driver) |
Set storage driver | Storage $driver |
void |
getDriver() |
Get current storage driver | - | Storage |
withPrefix($prefix) |
Set key prefix | string $prefix |
Cache |
Item Class
Method | Description | Returns |
---|---|---|
getKey() |
Get item key | string |
getData() |
Get raw data | mixed |
getDataDecrypted() |
Get decrypted data | mixed |
getDataEncrypted() |
Get encrypted data | string |
getTTL() |
Get time-to-live | int |
getCreatedAt() |
Get creation timestamp | int |
getExpiryTime() |
Get expiry timestamp | int |
setTTL($ttl) |
Set time-to-live | void |
generateKey() |
Generate encryption key (static) | string |
Storage Interface
Method | Description | Parameters |
---|---|---|
store($item) |
Store cache item | Item $item |
read($key, $prefix) |
Read item data | string $key , ?string $prefix |
readItem($key, $prefix) |
Read item object | string $key , ?string $prefix |
has($key, $prefix) |
Check item existence | string $key , ?string $prefix |
delete($key) |
Delete item | string $key |
flush($prefix) |
Clear cache | ?string $prefix |
Configuration
Environment Variables
Variable | Description | Default | Example |
---|---|---|---|
CACHE_ENCRYPTION_KEY |
64-char hex encryption key | Required | a1b2c3d4e5f6... |
CACHE_ENCRYPTION_ENABLED |
Enable/disable encryption | true |
true\|false |
CACHE_ENCRYPTION_ALGORITHM |
Encryption algorithm | aes-256-cbc |
aes-256-gcm |
CACHE_FILE_PERMISSIONS |
Cache file permissions | 600 |
644 |
CACHE_DIR_PERMISSIONS |
Cache directory permissions | 700 |
755 |
File Storage Configuration
The default FileStorage
class stores cache files in the WebFiori/Cache/cache
directory. You can customize this:
Security
Encryption
- All cached data is encrypted by default using AES-256-CBC
- Encryption keys should be 64-character hexadecimal strings
- Keys are managed through environment variables or
KeyManager
class - Each cache item can have its own encryption configuration
File Permissions
- Cache files are created with restrictive permissions (600 by default)
- Cache directories use 700 permissions by default
- Permissions are configurable via environment variables or
SecurityConfig
Best Practices
- Always set an encryption key in production environments
- Use environment variables for sensitive configuration
- Regularly rotate encryption keys for high-security applications
- Set appropriate file permissions for your environment
- Use prefixes to isolate different application components
- Implement proper error handling for cache operations
Key Generation
Error Handling
The library provides specific exceptions for different error conditions:
Performance Tips
- Use appropriate TTL values - longer for stable data, shorter for frequently changing data
- Implement cache warming for critical data
- Use key prefixes to organize and manage cache efficiently
- Monitor cache hit rates to optimize your caching strategy
- Consider cache size limits when using file storage
- Use dependency injection in modern applications for better testability
Examples
Web Application Session Caching
Database Query Caching
API Response Caching
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 inline code documentation