Download the PHP package tedon/kachet without Composer
On this page you can find all versions of the php package tedon/kachet. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Package kachet
Short Description Elegant method-level caching for PHP classes
License MIT
Informations about the package kachet
Kachet - Elegant Method-Level Caching for PHP
A Laravel package that provides elegant, method-level caching through a simple proxy pattern. Cache your class methods with zero boilerplate using either PHP attributes or programmatic configuration.
Features
- Zero boilerplate caching - Add caching with a single method call
- Dual configuration - Use PHP attributes or programmatic configuration
- Cache proxy pattern -
$obj->method()calls directly,$obj->cached()->method()uses cache - Flexible cache keys - Support for dynamic cache keys with sprintf-style placeholders
- Redis integration - Built for Redis but works with any Laravel cache driver
- Laravel integration - Service provider and facade included
- PHP 8.2+ - Built with modern PHP features
What is Kachet?
Kachet provides a clean, intuitive API for caching method results in your PHP classes. Instead of manually wrapping your methods with cache logic, you can:
- Add the
Kachetabletrait to your class - Configure cacheable methods using attributes or a configuration method
- Call
cached()when you want to use the cache
Installation
Install via Composer:
For Laravel applications, the service provider will be automatically registered.
Basic Usage
Using PHP Attributes (Recommended)
The simplest way to add caching is using PHP attributes:
Using Programmatic Configuration
If you prefer to configure cache settings in a method, use the cachedMethods() approach:
Advanced Usage
Dynamic Cache Keys with sprintf
Cache keys support sprintf-style placeholders that automatically map to method arguments:
Supported sprintf formats:
%d- Integer%s- String%f- Float- And all other standard sprintf formats
Forever Cache (No TTL)
Omit the TTL to cache indefinitely:
Custom Cache Drivers
Specify a custom cache driver (configured in config/cache.php):
Cache Tags
Use tags for easier cache invalidation:
Cache Patterns
Kachet supports different serialization patterns for cached data:
Available patterns:
CachePattern::BASE- No serialization (default)CachePattern::JSON- JSON serializationCachePattern::TOON- TOON format (requires tedon/tooner)
Caching Null Values
Control whether null results should be cached:
Custom Cache Prefix
Change the default cache key prefix using a class-level attribute:
Configuration Options
Attribute Configuration
When using the #[UseKachet] attribute:
Programmatic Configuration
When using cachedMethods():
Complete Example
Here's a comprehensive example showing multiple caching strategies:
Laravel Facade Usage
Kachet provides a facade for direct cache operations:
How It Works
Kachet uses PHP's magic __call method to intercept method calls on the proxy object:
- When you call
$obj->cached(), it returns aKachetProxyinstance - The proxy holds a reference to your original object
- When you call a method on the proxy (e.g.,
->findById(1)), the proxy:- Looks up the cache configuration for that method
- Generates a cache key using the method arguments
- Checks if the result exists in cache
- If cached: returns the cached value
- If not cached: calls the original method, caches the result, and returns it
Requirements
- PHP 8.2 or higher
- Laravel 10.x, 11.x, or 12.x
- Redis (recommended) or any Laravel-supported cache driver
Testing
IDE Support & Autocomplete
Kachet fully supports IDE autocomplete for cached methods using PHPDoc annotations. To enable autocomplete in your IDE (PhpStorm, VSCode, etc.), add the following annotation to your class:
With this annotation:
- Your IDE will autocomplete
$repo->cached()->findById() - Type hints and parameter suggestions will work correctly
- You'll get proper code navigation and refactoring support
The @method KachetProxy<static> cached() annotation tells the IDE that:
- The
cached()method returns aKachetProxyinstance - The proxy is generic over
static(your class type) - Through the
@mixinannotation inKachetProxy, the IDE knows the proxy has all your class methods
Note: This annotation is optional - your code will work without it, but adding it significantly improves the development experience.
Best Practices
- Use attributes for simple cases - They're cleaner and easier to read
- Use programmatic config for complex cases - When you need tags, custom drivers, or patterns
- Choose appropriate TTLs - Shorter for frequently changing data, longer for stable data
- Use cache tags - Makes cache invalidation easier
- Set custom prefixes - Include version numbers for easier cache busting
- Cache expensive operations - Database queries, API calls, complex computations
- Don't cache everything - Simple getters/setters don't need caching
- Add IDE autocomplete annotations - Include
@method KachetProxy<static> cached()for better IDE support
Cache Invalidation
Performance Tips
- Use Redis for better performance with high-traffic applications
- Set appropriate TTLs to balance freshness and performance
- Use cache tags for efficient bulk invalidation
- Monitor cache hit rates using Laravel Telescope or similar tools
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
MIT License - see the LICENSE file for details.
Author
Pouya Zouravand
- Email: [email protected]
Credits
Built with inspiration from Laravel's elegant API design principles and modern PHP best practices.