1. Go to this page and download the library: Download bit-mx/cache-entities 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/ */
bit-mx / cache-entities example snippets
namespace App\CacheEntities;
use BitMx\CacheEntities\CacheEntity;
use App\Models\User;
use Carbon\CarbonInterval;
class CurrentUserCache extends CacheEntity
{
public function __construct(
protected int $id,
)
{
}
protected function resolveKey(): string
{
return sprintf('current_user:%s', $this->id);
}
protected function resolveTtl(): CarbonInterval
{
return CarbonInterval::days(12);
}
protected function resolveValue(): mixed
{
return User::find($this->id);
}
}
namespace App\CacheEntities;
use BitMx\CacheEntities\CacheEntity;
use App\Models\User;
use Carbon\CarbonInterval;
class CurrentUserCache extends CacheEntity
{
protected function resolveCacheStore(): string
{
return 'redis';
}
}
use App\CacheEntities\CurrentUserCacheEntity;
$cacheEntity = new CurrentUserCacheEntity(1);
$user = $cacheEntity->get();
use App\CacheEntities\CurrentUserCacheEntity;
$cacheEntity = new CurrentUserCacheEntity(1);
$user = $cacheEntity->get();
if ($cacheEntity->exists()) {
// The cache key exists
} else {
// The cache key does not exist
}
use App\CacheEntities\CurrentUserCacheEntity;
$cacheEntity = new CurrentUserCacheEntity(1);
$user = $cacheEntity->get();
if ($cacheEntity->doesNotExist()) {
// The cache key does not exist
} else {
// The cache key exists
}
use App\CacheEntities\CurrentUserCacheEntity;
$cacheEntity = new CurrentUserCacheEntity(1);
$user = $cacheEntity->get();
$cacheEntity->forget();