1: <?php
2: /**
3: * @filesource Kotchasan/Cache/Cache.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\Cache;
12:
13: use Kotchasan\Cache\CacheItem as Item;
14: use Psr\Cache\CacheItemInterface;
15: use Psr\Cache\CacheItemPoolInterface;
16:
17: /**
18: * Kotchasan Caching Class (base class).
19: *
20: * @author Goragod Wiriya <admin@goragod.com>
21: *
22: * @since 1.0
23: */
24: abstract class Cache extends \Kotchasan\KBase implements CacheItemPoolInterface
25: {
26: /**
27: * รายการแคชรอบันทึก
28: *
29: * @var array
30: */
31: protected $deferred = array();
32:
33: /**
34: * บันทึกรายการแคชในคิว
35: * คืนค่า true ถ้าสำเร็จ, ถ้ามีบางรายการไม่สำเร็จคืนค่า false.
36: *
37: * @return bool
38: */
39: public function commit()
40: {
41: $cuccess = true;
42: foreach ($this->deferred as $item) {
43: if (!$this->save($item)) {
44: $cuccess = false;
45: }
46: }
47:
48: return $cuccess;
49: }
50:
51: /**
52: * ลบแคช
53: * คืนค่า true ถ้าสำเร็จ, false ถ้าไม่สำเร็จ.
54: *
55: * @param string $key
56: *
57: * @return bool
58: */
59: public function deleteItem($key)
60: {
61: return $this->deleteItems(array($key));
62: }
63:
64: /**
65: * อ่านแคช.
66: *
67: * @param string $key
68: *
69: * @return CacheItemInterface
70: */
71: public function getItem($key)
72: {
73: $items = $this->getItems(array($key));
74:
75: return isset($items[$key]) ? $items[$key] : new Item($key);
76: }
77:
78: /**
79: * กำหนดรายการแคชสำหรับบันทึกในภายหลัง
80: * คืนค่า false ถ้าไม่มีรายการในคิว.
81: *
82: * @param CacheItemInterface $item
83: *
84: * @return bool
85: */
86: public function saveDeferred(CacheItemInterface $item)
87: {
88: $this->deferred[$item->getKey()] = $item;
89:
90: return true;
91: }
92: }
93: