1: <?php
2: /**
3: * PHP Simple Cache Memcache driver.
4: *
5: * @package Simple Cache
6: * @author Vee W.
7: * @license http://opensource.org/licenses/MIT
8: *
9: */
10:
11:
12: namespace Rundiz\SimpleCache\Drivers;
13:
14: use Rundiz\SimpleCache\SimpleCacheInterface;
15:
16: /**
17: * Memcache driver class
18: *
19: * @since 2.0
20: */
21: class Memcache implements SimpleCacheInterface
22: {
23:
24:
25: /**
26: * Memcache class.
27: * @var \Memcache
28: */
29: protected $Memcache;
30:
31:
32: /**
33: * Class constructor.
34: *
35: * @param \Memcache $memcache Memcache class.
36: */
37: public function __construct(\Memcache $memcache)
38: {
39: $this->Memcache = $memcache;
40: }// __construct
41:
42:
43: /**
44: * {@inheritDoc}
45: */
46: public function clear()
47: {
48: return $this->Memcache->flush();
49: }// clear
50:
51:
52: /**
53: * {@inheritDoc}
54: */
55: public function delete($id)
56: {
57: $flags = null;
58: $this->Memcache->get($id, $flags);
59:
60: if ($flags === null) {
61: return false;
62: }
63: return $this->Memcache->delete($id);
64: }// delete
65:
66:
67: /**
68: * {@inheritDoc}
69: */
70: public function get($id)
71: {
72: return $this->Memcache->get($id);
73: }// get
74:
75:
76: /**
77: * {@inheritDoc}
78: */
79: public function save($id, $data, $lifetime = 60)
80: {
81: if (!is_int($lifetime) || is_int($lifetime) && $lifetime <= 0) {
82: $lifetime = 60;
83: }
84:
85: if ($lifetime > 2592000) {
86: $lifetime = 2591999;
87: }
88:
89: return $this->Memcache->set($id, $data, 0, $lifetime);
90: }// save
91:
92:
93: }
94: