1. Go to this page and download the library: Download cmatosbc/mnemosyne 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/ */
cmatosbc / mnemosyne example snippets
use Mnemosyne\Cache;
use Mnemosyne\CacheTrait;
use Psr\SimpleCache\CacheInterface;
class UserService
{
use CacheTrait; // Required to enable caching functionality
public function __construct(CacheInterface $cache)
{
$this->cache = $cache;
}
#[Cache(ttl: 3600)]
public function getUser(int $id): array
{
return $this->cacheCall('doGetUser', func_get_args());
}
private function doGetUser(int $id): array
{
// Expensive database query here
return ['id' => $id, 'name' => 'John Doe'];
}
}
class UserService
{
use CacheTrait;
// Automatically serialize complex objects
#[Cache(key: 'user:{id}', ttl: 3600, serialize: true)]
public function getUser(int $id): User
{
return $this->cacheCall('doGetUser', func_get_args());
}
// Store simple arrays without serialization
#[Cache(key: 'users:list', ttl: 3600, serialize: false)]
public function getUsersList(): array
{
return $this->cacheCall('doGetUsersList', func_get_args());
}
}
class UserService
{
use CacheTrait;
#[Cache(key: 'user:{id}', ttl: 3600)]
public function getUser(int $id): array
{
return $this->cacheCall('doGetUser', func_get_args());
}
#[Cache(key: 'users:dept:{deptId}:status:{status}', ttl: 3600)]
public function getUsersByDepartment(int $deptId, string $status): array
{
return $this->cacheCall('doGetUsersByDepartment', func_get_args());
}
}
class UserService
{
use CacheTrait;
#[Cache(
key: 'user:{id}',
ttl: 3600
)]
public function getUser(int $id): array
{
return $this->cacheCall('doGetUser', func_get_args());
}
#[Cache(invalidates: ['user:{id}'])]
public function updateUser(int $id, array $data): void
{
$this->cacheCall('doUpdateUser', func_get_args());
}
#[Cache(
key: 'user:profile:{id}',
ttl: 3600,
invalidates: ['user:{id}', 'users:dept:{deptId}:status:active']
)]
public function updateProfile(int $id, int $deptId): array
{
return $this->cacheCall('doUpdateProfile', func_get_args());
}
}
class UserService
{
use CacheTrait;
public function forceRefresh(int $userId): void
{
$this->invalidateCache("user:$userId");
// Or invalidate multiple keys:
$this->invalidateCacheKeys([
"user:$userId",
"user:profile:$userId"
]);
}
}
class UserService
{
use CacheTrait;
#[Cache(
key: 'user:{id}',
ttl: 3600,
tags: ['user', 'user-{id}']
)]
public function getUser(int $id): array
{
return $this->cacheCall('doGetUser', func_get_args());
}
#[Cache(
key: 'user:profile:{id}',
ttl: 3600,
tags: ['user', 'user-{id}']
)]
public function getUserProfile(int $id): array
{
return $this->cacheCall('doGetUserProfile', func_get_args());
}
public function updateUser(int $id): void
{
// Invalidate all caches for a specific user
$this->invalidateTag("user-$id");
}
public function clearAllUserCaches(): void
{
// Invalidate all user-related caches
$this->invalidateTag('user');
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.