Caching
Yabasi framework provides a powerful and flexible caching system to improve your application's performance. The caching system supports multiple cache drivers and offers a clean, unified API.
Configuration
Caching configuration is typically stored in the config/config.php
file:
return [
'cache' => [
'default' => 'file', // Primary cache store
'path' => BASE_PATH . '/storage/cache',
'stores' => [
'file' => [
'driver' => 'file',
'path' => BASE_PATH . '/storage/cache/data',
],
'redis' => [
'driver' => 'redis',
'connection' => 'default',
],
],
],
];
Basic Usage
Here are some basic operations you can perform with the Yabasi caching system:
Setting and Getting Cache Values
// Set a value in the cache for 1 hour
Cache::set('user_count', 100, 3600);
// Get a value from the cache
$userCount = Cache::get('user_count', 0);
Checking for Existence and Removing Cache Items
// Check if a key exists in the cache
if (Cache::has('user_count')) {
echo "User count is cached.";
}
// Remove an item from the cache
Cache::delete('user_count');
Incrementing and Decrementing Values
// Increment a value
$newCount = Cache::increment('user_count', 5);
// Decrement a value
$decrementedCount = Cache::decrement('user_count', 2);
Working with Multiple Cache Items
// Set multiple values
Cache::setMany([
'total_posts' => 1000,
'total_comments' => 5000
], 1800); // Cache for 30 minutes
// Get multiple values
$stats = Cache::many(['total_posts', 'total_comments', 'nonexistent_key']);
// Delete multiple values
Cache::deleteMany(['total_posts', 'total_comments']);
Advanced Usage
Using Different Cache Stores
You can specify which cache store to use for a particular operation:
$value = Cache::store('redis')->get('key');
Cache::store('file')->put('key', 'value', 600);
Atomic Locks
Yabasi provides atomic locks to manage concurrent access to resources:
$lock = Cache::lock('processing-report', 10);
if ($lock->get()) {
// Process the report...
$lock->release();
} else {
// Could not obtain lock...
return 'Try again later.';
}
Cache Tags
Cache tags allow you to tag related items in the cache and then flush all cached values tagged with a given name:
Cache::tags(['people', 'artists'])->put('John', $johnData, 3600);
Cache::tags(['people', 'authors'])->put('Anne', $anneData, 3600);
$john = Cache::tags(['people', 'artists'])->get('John');
$anne = Cache::tags(['people', 'authors'])->get('Anne');
// Remove all entries tagged with 'people'
Cache::tags('people')->flush();
Cache Helpers
Yabasi provides several helper functions to make working with the cache easier:
// Cache a value if it doesn't exist
$value = Cache::remember('key', 3600, function () {
return computeExpensiveValue();
});
// Cache a value forever
Cache::forever('key', 'value');
// Get and forget a value
$value = Cache::pull('key');
// Clear all items from the cache
Cache::flush();
Best Practices
- Use meaningful keys for your cached items to avoid conflicts.
- Set appropriate TTL (Time To Live) values for your cached data to ensure freshness.
- Use cache tags to organize and manage related cache entries.
- Be mindful of cache size, especially when using file-based caching.
- Use atomic locks for operations that require exclusive access.
- Implement cache warming strategies for critical data.
Yabasi's caching system provides a powerful tool to improve your application's performance. By strategically caching data and computationally expensive operations, you can significantly reduce load times and server load.