Laravel-like Cache Wrapper for Core PHP using Symfony Cache
uxmansarwar/cache is a Laravel package for laravel-like cache wrapper for core php using symfony cache.
It currently has 0 GitHub stars and 6 downloads on Packagist (latest version v1.0.1).
Install it with composer require uxmansarwar/cache.
Discover more Laravel packages by uxmansarwar
or browse all Laravel packages to compare alternatives.
Last updated
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.
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:
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.
Install Symfony Cache:
composer require symfony/cache
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.
Before using the cache, initialize it:
use UxmanSarwar\Cache;
Cache::init(
namespace: 'my_app',
defaultLifetime: 3600,
directory: __DIR__ . '/storage/cache'
);
$value = Cache::get('user_1');
Cache::put('user_1', ['name' => 'John'], 600); // 10 mins
Cache::forever('site_name', 'My Awesome App');
if (Cache::has('user_1')) {
echo "User found!";
}
Cache::forget('user_1');
Cache::flush();
$data = Cache::remember('config_data', 3600, function () {
return expensiveFetch();
});
$data = Cache::rememberForever('constants', function () {
return loadConstants();
});
$data = Cache::pull('temp_code'); // returns and deletes it
$wasAdded = Cache::add('otp_123', 456789, 120);
$newValue = Cache::increment('views', 1);
$newValue = Cache::decrement('downloads', 2);
composer require --dev pestphp/pest phpstan/phpstan
./vendor/bin/pest --init
This will create the tests/ directory and Pest.php bootstrap file.
Create phpstan.neon:
includes:
- vendor/phpstan/phpstan/conf/bleedingEdge.neon
parameters:
level: 8
paths:
- src
- tests
composer.json"scripts": {
"test": "pest",
"stan": "phpstan analyse"
}
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();
});
cache/
โโโ src/
โ โโโ Cache.php
โโโ tests/
โ โโโ CacheTest.php
โโโ storage/
โ โโโ cache/ (default)
โโโ composer.json
โโโ phpstan.neon
โโโ README.md
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
This project is open-sourced under the MIT License.
This wrapper is built using the power of Symfony Cache and inspired by Laravel's elegant Cache facade.
If this package helped you, consider โญ starring the repo and sharing it with your fellow PHP developers!