1: <?php
2: /**
3: * @filesource Kotchasan/Database/DbCache.php
4: *
5: * @copyright 2016 Goragod.com
6: * @license http://www.kotchasan.com/license/
7: *
8: * @see http://www.kotchasan.com/
9: */
10:
11: namespace Kotchasan\Database;
12:
13: use Kotchasan\Cache\CacheItem as Item;
14: use Kotchasan\Cache\FileCache as Cache;
15: use Kotchasan\Text;
16:
17: /**
18: * Database Cache Class
19: *
20: * @author Goragod Wiriya <admin@goragod.com>
21: *
22: * @since 1.0
23: */
24: class DbCache
25: {
26: /**
27: * กำหนดการโหลดข้อมูลจากแคชอัตโนมัติ
28: * 0 ไม่ใช้แคช
29: * 1 โหลดและบันทึกแคชอัตโนมัติ
30: * 2 โหลดข้อมูลจากแคชได้ แต่ไม่บันทึกแคชอัตโนมัติ
31: *
32: * @var int
33: */
34: private $action = 0;
35: /**
36: * คลาส Cache.
37: *
38: * @var Cache
39: */
40: private $db_cache;
41: /**
42: * @var Singleton สำหรับเรียกใช้ class นี้เพียงครั้งเดียวเท่านั้น
43: */
44: private static $instance = null;
45:
46: /**
47: * เปิดการใช้งานแคช
48: * จะมีการตรวจสอบจากแคชก่อนการสอบถามข้อมูล
49: *
50: * @param bool $auto_save (options) true (default) บันทึกผลลัพท์อัตโนมัติ, false ต้องบันทึกแคชเอง
51: */
52: public function cacheOn($auto_save = true)
53: {
54: $this->action = $auto_save ? 1 : 2;
55: }
56:
57: /**
58: * เคลียร์แคช
59: * คืนค่า true ถ้าลบเรียบร้อย, หรือ array ของรายการที่ไม่สำเร็จ
60: *
61: * @return bool
62: */
63: public function clear()
64: {
65: $this->db_cache->clear();
66: }
67:
68: /**
69: * Create Class (Singleton).
70: *
71: * @return \static
72: */
73: public static function create()
74: {
75: if (null === static::$instance) {
76: static::$instance = new static();
77: }
78: return static::$instance;
79: }
80:
81: /**
82: * อ่านข้อมูลจากแคช
83: * คืนค่าข้อมูลหรือ false ถ้าไม่มีแคช
84: *
85: * @param Item $item
86: *
87: * @return mixed
88: */
89: public function get(Item $item)
90: {
91: return $item->isHit() ? $item->get() : false;
92: }
93:
94: /**
95: * อ่านสถานะของแคช
96: * 0 ไม่ใช้แคช
97: * 1 โหลดและบันทึกแคชอัตโนมัติ
98: * 2 โหลดข้อมูลจากแคชได้ แต่ไม่บันทึกแคชอัตโนมัติ.
99: *
100: * @return int
101: */
102: public function getAction()
103: {
104: return $this->action;
105: }
106:
107: /**
108: * กำหนดคีย์ของแคชจาก query
109: *
110: * @param string $sql
111: * @param array $values
112: *
113: * @return Item
114: */
115: public function init($sql, $values)
116: {
117: return $this->db_cache->getItem(Text::replace($sql, $values));
118: }
119:
120: /**
121: * บันทึก cache เมื่อบันทึกแล้วจะปิดการใช้งาน cache อัตโนมัติ
122: * จะใช้คำสั่งนี้เมื่อมีการเรียกใช้แคชด้วยคำสั่ง cacheOn(false) เท่านั้น
123: * query ครั้งต่อไปถ้าจะใช้ cache ต้อง เปิดการใช้งาน cache ก่อนทุกครั้ง
124: * สำเร็จคืนค่า true ไม่สำเร็จคืนค่า false
125: *
126: * @param Item $item
127: * @param array $datas ข้อมูลที่จะบันทึก
128: *
129: * @return bool
130: */
131: public function save(Item $item, $datas)
132: {
133: $this->action = 0;
134: $item->set($datas);
135: return $this->db_cache->save($item);
136: }
137:
138: /**
139: * กำหนดสถานะของแคช
140: * 0 ไม่ใช้แคช
141: * 1 โหลดและบันทึกแคชอัตโนมัติ
142: * 2 โหลดข้อมูลจากแคชได้ แต่ไม่บันทึกแคชอัตโนมัติ
143: *
144: * @param type $value
145: *
146: * @return \static
147: */
148: public function setAction($value)
149: {
150: $this->action = $value;
151: return $this;
152: }
153:
154: /**
155: * ตรวจสอบว่าข้อมูลมาจาก cache หรือไม่
156: *
157: * @param Item $item
158: *
159: * @return bool
160: */
161: public function usedCache(Item $item)
162: {
163: return $item->isHit();
164: }
165:
166: /**
167: * Class constructor
168: */
169: private function __construct()
170: {
171: $this->db_cache = new Cache();
172: }
173: }
174: