Skip to main content

Caching Strategy

Comprehensive caching strategy for Netasampark.

Cache Types

Application Cache

  • Storage: Redis
  • TTL: Varies by data type
  • Invalidation: Manual or TTL-based

Query Cache

  • Storage: Redis
  • TTL: 1 hour
  • Invalidation: On data update

Page Cache

  • Storage: Filesystem/Redis
  • TTL: 24 hours
  • Invalidation: On content update

CDN Cache

  • Storage: CDN edge servers
  • TTL: 1 year (static assets)
  • Invalidation: Version-based

Implementation

Laravel Cache

// Cache query results
$users = Cache::remember('users.active', 3600, function () {
return User::where('status', 'active')->get();
});

// Cache with tags
Cache::tags(['users', 'active'])->put('users.active', $users, 3600);

// Cache forever
Cache::forever('config.settings', $settings);

Redis Configuration

// config/cache.php
'redis' => [
'client' => 'phpredis',
'default' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD'),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
],
],

Cache Invalidation

Manual Invalidation

// Clear specific cache
Cache::forget('users.active');

// Clear tagged cache
Cache::tags(['users'])->flush();

// Clear all cache
Cache::flush();

Automatic Invalidation

// Invalidate on model update
class User extends Model
{
protected static function booted()
{
static::updated(function ($user) {
Cache::forget('users.active');
});
}
}

Best Practices

  1. Cache Key Strategy: Use descriptive, unique keys
  2. TTL Strategy: Set appropriate TTLs
  3. Invalidation: Clear cache on updates
  4. Monitoring: Monitor cache hit rates
  5. Fallback: Handle cache misses gracefully

Next Steps