1: <?php
2: /**
3: * @filesource Kotchasan/Cache/ApcCache.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:
16: /**
17: * APC cache driver.
18: *
19: * @author Goragod Wiriya <admin@goragod.com>
20: *
21: * @since 1.0
22: */
23: class ApcCache extends Cache
24: {
25: /**
26: * Class constructor
27: *
28: * @throws Exception ถ้า Server ไม่รองรับ APC
29: */
30: public function __construct()
31: {
32: if (!extension_loaded('apc') || !is_callable('apc_fetch')) {
33: throw new \Exception('APC not supported.');
34: }
35: }
36:
37: /**
38: * เคลียร์แคช
39: * คืนค่า true ถ้าลบเรียบร้อย, หรือ false ถ้าไม่สำเร็จ.
40: *
41: * @return bool
42: */
43: public function clear()
44: {
45: return \apc_clear_cache('user');
46: }
47:
48: /**
49: * ลบแคชหลายๆรายการ
50: * คืนค่า true ถ้าสำเร็จ, false ถ้าไม่สำเร็จ.
51: *
52: * @param array $keys
53: *
54: * @return bool
55: */
56: public function deleteItems(array $keys)
57: {
58: if ($this->cache_dir) {
59: foreach ($keys as $key) {
60: \apc_delete($key);
61: }
62: }
63:
64: return true;
65: }
66:
67: /**
68: * อ่านแคชหลายรายการ.
69: *
70: * @param array $keys
71: *
72: * @return array
73: */
74: public function getItems(array $keys = array())
75: {
76: $resuts = array();
77: $success = false;
78: $values = \apc_fetch($keys, $success);
79: if ($success && is_array($values)) {
80: foreach ($values as $key => $value) {
81: $item = new Item($key);
82: $resuts[$key] = $item->set($value);
83: }
84: }
85:
86: return $resuts;
87: }
88:
89: /**
90: * ตรวจสอบแคช
91: * คืนค่า true ถ้ามี.
92: *
93: * @param string $key
94: *
95: * @return bool
96: */
97: public function hasItem($key)
98: {
99: return \apc_exists($key);
100: }
101:
102: /**
103: * บันทึกแคช
104: * สำเร็จคืนค่า true ไม่สำเร็จคืนค่า false.
105: *
106: * @param CacheItemInterface $item
107: *
108: * @throws CacheException
109: *
110: * @return bool
111: */
112: public function save(CacheItemInterface $item)
113: {
114: return \apc_store($item->getKey(), $item->get(), self::$cfg->get('cache_expire', 5));
115: }
116: }
117: