Cache

<a name="configuration"></a> ### การตั้งค่า

Laravel เตรียมรูปแบบของการ ` cache ไว้ให้เราใช้เเล้วหลายตัวเลยนะครับ . ซึ่งรายชื่อและการตั้งค่าอยู่ที่ app/config/cache.php`. ในไฟล์เราสามารถตั้งค่าให้กับ cache ที่เราชื่นชอบได้เลย Laravel สนับสนุนทั้ง Memcached กับ Redis อยู่แล้วครับ.

ก่อนจะทำการตั้งค่าใดๆ ลองไปศึกษาก่อนนะครับ ตัว laravel ใช้ file cache เป็นตัวเริ่มต้นนะครับ ถ้าเว็บไซต์มีขนาดใหญ่ควรเปลี่ยนไปใช้ Memcached หรือ APC จะดีกว่า

<a name="cache-usage"></a> ### การใช้งาน Cache


การเก็บค่าลง cache

1 Cache::put('key', 'value', $minutes);

เก็บลงในกรณีที่ไม่มีค่านั้นอยู่

1 Cache::add('key', 'value', $minutes);

ตรวจว่ามีค่าไหม

1 if (Cache::has('key'))
2 {
3 	//
4 }

ดึงค่าจากแคช

1 $value = Cache::get('key');

ดึงค่าโดยเรียกค่าเริ่มต้น

1 $value = Cache::get('key', 'default');
2 
3 $value = Cache::get('key', function() { return 'default'; });

กำหนดให้ค่าชนิดนี้ไม่มีวันหมดอายุ

1 Cache::forever('key', 'value');

บางเวลาเราต้องการเรียกใช้ค่าจากแคช แต่ไม่มีค่าแล้ว เราสามารถเรียกใช้ Cache::remember โดยการดึงค่าจากฐานข้อมูลขึ้นมาเก็บไว้ในแคช

1 $value = Cache::remember('users', $minutes, function()
2 {
3 	return DB::table('users')->get();
4 });

และยังสามารถผสมคำสั่ง remember กับ forever

1 $value = Cache::rememberForever('users', function()
2 {
3 	return DB::table('users')->get();
4 });

เราสามารถเก็บค่าชนิดใดก็ได้เพราะรูปแบบการเก็บค่าใช้แบบ serialize

การลบค่าออกจากคีย์

1 Cache::forget('key');

<a name="increments-and-decrements"></a> ## การเพิ่มและการลดค่า

แคชทุกชนิดยกเว้น file กับ database สนับสนุนการทำเพิ่มและลดค่าของแคช

การเพิ่มค่า

1 Cache::increment('key');
2 
3 Cache::increment('key', $amount);

การลดค่า

1 Cache::decrement('key');
2 
3 Cache::decrement('key', $amount);

<a name="cache-sections"></a> ## Cache Sections

หมายเหตุ: Cache sections ไม่สนับสนุนแคชแบบ file หรือ database

Cache sections คือการให้เราสามารถจับกลุ่มให้กับแคชที่มีรูปแบบการเก็บค่าที่คล้ายๆ กันโดยใช้คำสั่งsection

ตัวอย่างการใช้งาน Cache section

1 Cache::section('people')->put('John', $john);
2 
3 Cache::section('people')->put('Anne', $anne);

การเข้าถึงค่าของแคช

1 $anne = Cache::section('people')->get('Anne');

flush คือการลบค่าออก:

1 Cache::section('people')->flush();

<a name="database-cache"></a> ## การเก็บแคชไว้ในฐานข้อมูล

เมื่อเราจะใช้ฐานข้อมูลเก็บค่าแคช เราต้องเพิ่มตารางที่จะใช้เก็บก่อน นี่คือตัวอย่างการสร้างตารางที่ใช้เก็บแคชในรูปแบบ ของ laravel

1 Schema::create('cache', function($table)
2 {
3 	$table->string('key')->unique();
4 	$table->text('value');
5 	$table->integer('expiration');
6 });