Kotchasan Framework Documentation

Kotchasan Framework Documentation

Cache System Overview

TH 05 Feb 2026 07:40

Cache System Overview

ระบบ Cache ของ Kotchasan ช่วยเพิ่มประสิทธิภาพแอปพลิเคชันโดยเก็บข้อมูลไว้ใช้ซ้ำ

สารบัญ

  1. ภาพรวม
  2. Cache Implementations
  3. การเลือก Cache
  4. ตัวอย่าง

ภาพรวม

Class คำอธิบาย เหมาะสำหรับ
MemoryCache เก็บในหน่วยความจำ Request scope
FileCache เก็บในไฟล์ Single server, persistent
RedisCache เก็บใน Redis Multiple servers, high performance
QueryCache Cache สำหรับ DB queries Database optimization

การใช้งานพื้นฐาน

use Kotchasan\Cache\CacheFactory;

// สร้าง cache ตาม config
$cache = CacheFactory::create([
    'driver' => 'file',
    'path' => ROOT_PATH . 'datas/cache/'
]);

// CRUD operations
$cache->set('key', $value, 3600);  // เก็บ 1 ชั่วโมง
$value = $cache->get('key');        // ดึงค่า
$cache->delete('key');              // ลบ
$cache->has('key');                 // ตรวจสอบ
$cache->clear();                    // ล้างทั้งหมด

การเลือก 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

  • MemoryCache - Cache ในหน่วยความจำ
  • [FileCache] - Cache ในไฟล์
  • RedisCache - Redis Cache
  • QueryCache - Cache สำหรับ Query
  • CacheFactory - Factory สำหรับสร้าง Cache