PHP code example of uxmansarwar / cache
1. Go to this page and download the library: Download uxmansarwar/cache library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
uxmansarwar / cache example snippets
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);
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