uxmansarwar / cache
Laravel-like Cache Wrapper for Core PHP using Symfony Cache
Requires
- php: >=8.0
- symfony/cache: ^7.0
Requires (Dev)
- pestphp/pest: ^2.34
- phpstan/phpstan: ^1.10
This package is auto-updated.
Last update: 2025-04-18 18:59:25 UTC
README
A fully-featured Laravel-style Cache Wrapper built in Core PHP using the power of Symfony's Cache component. This package provides an elegant and fluent caching API that mirrors Laravel's Cache
facade, making it easy for developers to manage caching in procedural or object-oriented PHP applications outside of Laravel.
๐ Why This Package?
Laravel provides a very expressive and flexible API for caching, but it's deeply coupled with the Laravel framework. This package allows you to bring that elegant Laravel cache experience into any Core PHP project without requiring Laravel at all.
Built for:
- Developers transitioning from Laravel to raw PHP
- Lightweight or micro PHP apps that need efficient caching
- Custom frameworks and standalone apps
๐ง Why a Wrapper?
Wrapping an existing library like Symfony's Cache component helps abstract complexity and create a clean, readable, Laravel-style API without reinventing the wheel. Symfony's cache is PSR-6/16 compliant and battle-tested in production, making it an excellent backend for this wrapper.
โ๏ธ Requirements
- PHP 8.0 or later
- Composer
- Symfony Cache Component
Install Symfony Cache:
composer require symfony/cache
๐ฆ Installation
1. Clone or Require
If you're using Composer for your project:
composer require uxmansarwar/cache
If you're manually integrating:
git clone https://github.com/uxmansarwar/cache.git
Place src/Cache.php
in your preferred directory and include it with Composer autoload.
๐งช Initialization
Before using the cache, initialize it:
use UxmanSarwar\Cache; Cache::init( namespace: 'my_app', defaultLifetime: 3600, directory: __DIR__ . '/storage/cache' );
๐ Full API Usage Examples
๐ Get Value from Cache
$value = Cache::get('user_1');
๐ Put Value into Cache
Cache::put('user_1', ['name' => 'John'], 600); // 10 mins
โพ๏ธ Store Permanently
Cache::forever('site_name', 'My Awesome App');
โ Check if Key Exists
if (Cache::has('user_1')) { echo "User found!"; }
โ Forget a Cache Key
Cache::forget('user_1');
๐งน Flush Entire Cache
Cache::flush();
๐ฆ Remember (Lazy Load + Cache)
$data = Cache::remember('config_data', 3600, function () { return expensiveFetch(); });
๐ฆ Remember Forever
$data = Cache::rememberForever('constants', function () { return loadConstants(); });
๐งช Pull and Delete
$data = Cache::pull('temp_code'); // returns and deletes it
๐ชฎ Add if Not Exists
$wasAdded = Cache::add('otp_123', 456789, 120);
๐พ๏ธ Increment a Value
$newValue = Cache::increment('views', 1);
๐พ Decrement a Value
$newValue = Cache::decrement('downloads', 2);
๐งฑ Tests & Development Setup
Install Dev Dependencies
composer require --dev pestphp/pest phpstan/phpstan
PestPHP Setup
./vendor/bin/pest --init
This will create the tests/
directory and Pest.php
bootstrap file.
PHPStan Setup
Create phpstan.neon
:
includes: - vendor/phpstan/phpstan/conf/bleedingEdge.neon parameters: level: 8 paths: - src - tests
Add Test Scripts to composer.json
"scripts": { "test": "pest", "stan": "phpstan analyse" }
๐ Example Pest Tests
File: tests/CacheTest.php
<?php declare(strict_types=1); use UxmanSarwar\Cache; beforeAll(function () { Cache::init( namespace: 'testing', defaultLifetime: 3600, directory: __DIR__ . '/../storage/cache' ); }); test('put and get works', function () { Cache::put('test_key', 'Hello', 10); expect(Cache::get('test_key'))->toBe('Hello'); }); test('forget removes a key', function () { Cache::put('temp_key', 'Temp', 10); Cache::forget('temp_key'); expect(Cache::has('temp_key'))->toBeFalse(); }); test('remember lazily loads and stores value', function () { $result = Cache::remember('lazy_key', 60, fn () => 'LazyValue'); expect($result)->toBe('LazyValue'); }); test('pull fetches and deletes value', function () { Cache::put('pull_key', 'Once', 60); $value = Cache::pull('pull_key'); expect($value)->toBe('Once'); expect(Cache::has('pull_key'))->toBeFalse(); });
๐งฑ Directory Structure
cache/
โโโ src/
โ โโโ Cache.php
โโโ tests/
โ โโโ CacheTest.php
โโโ storage/
โ โโโ cache/ (default)
โโโ composer.json
โโโ phpstan.neon
โโโ README.md
๐ฏ Planned Features
- File-based tagging (like Laravel's tagged cache)
- Redis/Memcached drivers
- PSR-16 compatibility layer
- Cache events and logging
๐งโ๐ป Contribution
Pull requests and issues are welcome! Feel free to fork and improve. Please make sure to write tests and maintain existing code style.
git clone https://github.com/uxmansarwar/cache.git
cd cache
composer install
๐ License
This project is open-sourced under the MIT License.
โค๏ธ Credits
This wrapper is built using the power of Symfony Cache and inspired by Laravel's elegant Cache
facade.
๐ Support
If this package helped you, consider โญ starring the repo and sharing it with your fellow PHP developers!