Kotchasan Framework Documentation

Kotchasan Framework Documentation

Cache System Overview

EN 05 Feb 2026 07:40

Cache System Overview

Kotchasan's Cache system improves application performance by storing data for reuse.

Table of Contents

  1. Overview
  2. Cache Implementations
  3. Choosing a Cache
  4. Examples

Overview

Class Description Best For
MemoryCache In-memory storage Request scope
FileCache File-based storage Single server, persistent
RedisCache Redis storage Multiple servers, high performance
QueryCache DB query caching Database optimization

Basic Usage

use Kotchasan\Cache\CacheFactory;

// Create cache based on config
$cache = CacheFactory::create([
    'driver' => 'file',
    'path' => ROOT_PATH . 'datas/cache/'
]);

// CRUD operations
$cache->set('key', $value, 3600);  // Store for 1 hour
$value = $cache->get('key');        // Retrieve
$cache->delete('key');              // Delete
$cache->has('key');                 // Check existence
$cache->clear();                    // Clear all

Choosing a Cache

// Development: MemoryCache
$cache = new MemoryCache();

// Single Server Production: FileCache
$cache = new FileCache(['path' => '/tmp/cache/']);

// Multiple Servers: RedisCache
$cache = new RedisCache('redis.example.com', 6379);

Cache Classes