1. Go to this page and download the library: Download tedon/kachet 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/ */
tedon / kachet example snippets
$user = new UserRepository();
// Direct call - always executes the method
$result = $user->findById(1);
// Cached call - uses cache if available
$result = $user->cached()->findById(1);
use Tedon\Kachet\Traits\Kachetable;
use Tedon\Kachet\UseKachet;
use Tedon\Kachet\KachetProxy;
/**
* @method KachetProxy<static> cached()
*/
class UserRepository
{
use Kachetable;
#[UseKachet(cacheKey: 'user:%d', ttl: 3600)]
public function findById(int $id): array
{
// Expensive database query
return DB::table('users')->find($id);
}
#[UseKachet(cacheKey: 'users:latest', ttl: 60)]
public function listLatest(): array
{
return DB::table('users')
->orderBy('created_at', 'desc')
->limit(10)
->get();
}
}
// Usage
$repo = new UserRepository();
// Direct call - always executes the query
$user = $repo->findById(1);
// Cached call - uses cache for 3600 seconds
$user = $repo->cached()->findById(1);
// Cache key: "kachet:user:1"
// List without cache
$users = $repo->listLatest();
// List with cache
$users = $repo->cached()->listLatest();
// Cache key: "kachet:users:latest"
use Tedon\Kachet\Traits\Kachetable;
use Tedon\Kachet\KachetProxy;
/**
* @method KachetProxy<static> cached()
*/
class ProductRepository
{
use Kachetable;
public function findById(int $id): array
{
return DB::table('products')->find($id);
}
public function listByCategory(string $category): array
{
return DB::table('products')
->where('category', $category)
->get();
}
public function cachedMethods(): array
{
return [
[
'methodName' => 'findById',
'cacheKey' => 'product:%d',
'ttl' => 3600,
],
[
'methodName' => 'listByCategory',
'cacheKey' => 'products:category:%s',
'ttl' => 1800,
],
];
}
}
// Usage
$repo = new ProductRepository();
// Cache product for 1 hour
$product = $repo->cached()->findById(42);
// Cache key: "kachet:product:42"
// Cache category listing for 30 minutes
$products = $repo->cached()->listByCategory('electronics');
// Cache key: "kachet:products:category:electronics"
#[UseKachet(cacheKey: 'post:%d:comments:page:%d', ttl: 600)]
public function getPostComments(int $postId, int $page): array
{
return DB::table('comments')
->where('post_id', $postId)
->paginate($page);
}
// Usage
$comments = $repo->cached()->getPostComments(123, 2);
// Cache key: "kachet:post:123:comments:page:2"
#[UseKachet(cacheKey: 'settings')]
public function getSettings(): array
{
return DB::table('settings')->pluck('value', 'key');
}
use Tedon\Kachet\Traits\Kachetable;
use Tedon\Kachet\UseKachet;
use Tedon\Kachet\KachetProxy;
/**
* @method KachetProxy<static> cached()
*/
#[UseKachet(cacheKey: 'myapp:v2:')]
class MyRepository
{
use Kachetable;
#[UseKachet(cacheKey: 'user:%d', ttl: 3600)]
public function findById(int $id): array
{
return DB::table('users')->find($id);
}
}
// Cache key will be: "myapp:v2:user:1"
$user = $repo->cached()->findById(1);
#[UseKachet(
cacheKey: 'my:cache:key', // Required: Cache key with optional sprintf placeholders
ttl: 3600, // Optional: Time to live in seconds (null = forever)
)]
[
'methodName' => 'myMethod', // Required: Method name to cache
'cacheKey' => 'my:cache:key', // Required: Cache key with sprintf placeholders
'ttl' => 3600, // Optional: Time to live (null = forever)
'tags' => ['tag1', 'tag2'], // Optional: Cache tags
'driver' => 'redis', // Optional: Specific cache driver
'cacheNullValue' => true, // Optional: Cache null results (default: false)
'storePattern' => CachePattern::JSON, // Optional: Serialization pattern
]
use Tedon\Kachet\Traits\Kachetable;
use Tedon\Kachet\UseKachet;
use Tedon\Kachet\KachetProxy;
use Tedon\Kachet\Constants\CachePattern;
/**
* @method KachetProxy<static> cached()
*/
class BlogRepository
{
use Kachetable;
protected string $cachePrefix = 'blog:v1:';
// Simple attribute-based caching
#[UseKachet(cacheKey: 'post:%d', ttl: 3600)]
public function findPost(int $id): array
{
return DB::table('posts')->find($id);
}
// Multi-parameter cache key
#[UseKachet(cacheKey: 'posts:%s:page:%d', ttl: 600)]
public function listByCategory(string $category, int $page = 1): array
{
return DB::table('posts')
->where('category', $category)
->paginate($page);
}
// Forever cache
#[UseKachet(cacheKey: 'categories')]
public function getAllCategories(): array
{
return DB::table('categories')->pluck('name', 'id');
}
// Complex programmatic configuration
public function getStats(int $year): array
{
return DB::table('posts')
->whereYear('created_at', $year)
->selectRaw('COUNT(*) as total, AVG(views) as avg_views')
->first();
}
public function cachedMethods(): array
{
return [
[
'methodName' => 'getStats',
'cacheKey' => 'stats:%d',
'ttl' => 86400,
'tags' => ['statistics', 'posts'],
'storePattern' => CachePattern::JSON,
'driver' => 'redis',
],
];
}
}
// Usage examples
$blog = new BlogRepository();
// Direct calls - no caching
$post = $blog->findPost(1);
$posts = $blog->listByCategory('tech', 2);
$categories = $blog->getAllCategories();
$stats = $blog->getStats(2024);
// Cached calls
$post = $blog->cached()->findPost(1);
// Key: "blog:v1:post:1", TTL: 3600s
$posts = $blog->cached()->listByCategory('tech', 2);
// Key: "blog:v1:posts:tech:page:2", TTL: 600s
$categories = $blog->cached()->getAllCategories();
// Key: "blog:v1:categories", TTL: forever
$stats = $blog->cached()->getStats(2024);
// Key: "blog:v1:stats:2024", TTL: 86400s
// Serialized as JSON, tagged with ['statistics', 'posts']
// Clear specific caches
Cache::tags(['statistics'])->flush();
use Tedon\Kachet\Facades\Kachet;
// Check if a cache key exists
if (Kachet::has('user:123')) {
// ...
}
// Manually cache a value
Kachet::put('custom:key', $value, 3600);
// Retrieve a cached value
$value = Kachet::get('custom:key');
use Tedon\Kachet\Traits\Kachetable;
use Tedon\Kachet\KachetProxy;
/**
* @method KachetProxy<static> cached()
*/
class UserRepository
{
use Kachetable;
public function findById(int $id): array { /* ... */ }
public function listLatest(): array { /* ... */ }
}
use Illuminate\Support\Facades\Cache;
// Clear specific key
Cache::forget('kachet:user:123');
// Clear by pattern (Redis only)
Cache::tags(['users'])->flush();
// Clear all cache
Cache::flush();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.