Download the PHP package tweekersnut/cache without Composer
On this page you can find all versions of the php package tweekersnut/cache. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download tweekersnut/cache
More information about tweekersnut/cache
Files in tweekersnut/cache
Package cache
Short Description A high-performance, pluggable PHP caching library with Redis, file, session, and memory drivers.
License MIT
Informations about the package cache
TweekersNut Cache
A high-performance, pluggable PHP caching library with Redis, file, session, and memory drivers.
TweekersNut Cache is a professional, production-ready caching solution designed to speed up backend processing, data fetching, and manipulation. It provides a unified interface for multiple cache drivers, making it easy to switch between different caching strategies without changing your code.
๐ Features
- Multiple Cache Drivers: Redis, File, Session, and Array (in-memory)
- Unified Interface: Consistent API across all drivers
- PSR-4 Autoloading: Modern PHP standards
- Type-Safe: Full PHP 8.0+ type hints
- Zero Dependencies: No framework required (works standalone)
- High Performance: Optimized for production environments
- TTL Support: Time-to-live for automatic expiration
- Prefix/Namespace Support: Avoid key collisions
- Remember Pattern: Cache-or-execute in one call
- Increment/Decrement: Atomic counter operations
- Exception-Safe: Never breaks application flow
๐ฆ Installation
Install via Composer:
Requirements
- PHP >= 8.0
- (Optional) Redis extension for Redis driver
- (Optional) Predis library as alternative to Redis extension
๐ฏ Quick Start
Basic Usage
Static Facade Usage
๐ง Configuration
Redis Driver
Requirements: PHP Redis extension (ext-redis) or Predis library
Best For: High-traffic applications, distributed systems, shared cache across servers
File Cache Driver
Best For: Single-server applications, development, when Redis is unavailable
Session Cache Driver
Best For: Per-user caching, temporary user-specific data
Array (Memory) Cache Driver
Best For: Testing, single-request caching, temporary runtime data
๐ API Reference
Core Methods
get(string $key, mixed $default = null): mixed
Retrieve a value from cache.
set(string $key, mixed $value, ?int $ttl = null): bool
Store a value in cache.
has(string $key): bool
Check if a key exists in cache.
delete(string $key): bool
Remove a key from cache.
clear(): bool
Clear all cache entries (respects prefix).
remember(string $key, int $ttl, callable $callback): mixed
Get from cache or execute callback and store result.
increment(string $key, int $value = 1): int
Increment a numeric value.
decrement(string $key, int $value = 1): int
Decrement a numeric value.
๐ก Usage Examples
Caching Database Queries
Caching API Responses
Rate Limiting
Caching Heavy Computations
Session-Based User Preferences
๐ Driver Comparison
| Feature | Redis | File | Session | Array |
|---|---|---|---|---|
| Persistence | โ Yes | โ Yes | โ ๏ธ Session only | โ No |
| Multi-Server | โ Yes | โ No | โ No | โ No |
| Performance | โก Excellent | โ ๏ธ Good | โ ๏ธ Good | โก Excellent |
| Setup Required | โ Redis server | โ None | โ None | โ None |
| Memory Usage | โ ๏ธ External | โ ๏ธ Disk | โ ๏ธ Session | โ ๏ธ PHP memory |
| Best For | Production | Development | User data | Testing |
๐จ Advanced Features
Prefix/Namespacing
Avoid key collisions by using prefixes:
Multiple Cache Instances
Graceful Fallback
๐ Best Practices
- Use appropriate TTL: Set reasonable expiration times to balance freshness and performance
- Prefix your keys: Avoid collisions, especially in shared environments
- Cache expensive operations: Database queries, API calls, complex calculations
- Use
remember()pattern: Simplifies cache-or-execute logic - Handle cache failures gracefully: Never let cache issues break your app
- Monitor cache hit rates: Optimize your caching strategy based on metrics
- Clear cache strategically: Invalidate when data changes, not on every request
๐งช Testing
The library includes a test suite. Run tests with PHPUnit:
๐ฅ Watch This Video
Click the video below to watch it directly on YouTube:
๐ Click the image to open the video
๐ License
This library is open-source software licensed under the MIT License.
๐ฅ Author
TweekersNut Network
- Website: https://tweekersnut.com
- Email: [email protected]
๐ค Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
๐ Support
If you encounter any issues or have questions:
- Open an issue on GitHub
- Email: [email protected]
๐ฏ Roadmap
- [ ] PSR-6 and PSR-16 compliance
- [ ] Memcached driver
- [ ] Tag-based cache invalidation
- [ ] Cache warming utilities
- [ ] Performance benchmarking tools
- [ ] Laravel service provider
- [ ] Symfony bundle
Made with โค๏ธ by TweekersNut Network
