1 <?php
2 3 4 5 6 7
8
9 namespace Cross\Cache\Request;
10
11 use Cross\Cache\Driver\MemcacheDriver;
12 use Cross\Exception\CoreException;
13 use Cross\I\RequestCacheInterface;
14
15 16 17 18 19
20 class Memcache implements RequestCacheInterface
21 {
22 23 24
25 protected $config;
26
27 28 29 30 31
32 protected $cacheKey;
33
34 35 36 37 38
39 protected $expireTime = 3600;
40
41 42 43
44 protected $flag = 0;
45
46 47 48
49 protected $driver;
50
51 52 53 54 55 56
57 function __construct(array $option)
58 {
59 if (empty($option['key'])) {
60 throw new CoreException('请指定缓存KEY');
61 }
62
63 $this->cacheKey = &$option['key'];
64 $this->driver = new MemcacheDriver($option);
65
66 if (isset($option['flag']) && $option['flag']) {
67 $this->flag = &$option['flag'];
68 }
69
70 if (isset($option['expire_time'])) {
71 $this->expireTime = &$option['expire_time'];
72 }
73 }
74
75 76 77 78 79
80 function set($value)
81 {
82 $this->driver->set($this->cacheKey, $value, $this->flag, $this->expireTime);
83 }
84
85 86 87 88 89 90
91 function get(&$flag = 0)
92 {
93 return $this->driver->get($this->cacheKey, $flag);
94 }
95
96 97 98 99 100
101 function isValid()
102 {
103 if ($this->driver->get($this->cacheKey)) {
104 return true;
105 }
106
107 return false;
108 }
109
110 111 112 113 114
115 function setConfig(array $config = array())
116 {
117 $this->config = $config;
118 }
119
120 121 122 123 124
125 function getConfig()
126 {
127 return $this->config;
128 }
129 }
130