1: <?php namespace Laravel\Cache\Drivers;
2:
3: class Memcached extends Sectionable {
4:
5: 6: 7: 8: 9:
10: public $memcache;
11:
12: 13: 14: 15: 16:
17: protected $key;
18:
19: 20: 21: 22: 23: 24: 25:
26: public function __construct(\Memcached $memcache, $key)
27: {
28: $this->key = $key;
29: $this->memcache = $memcache;
30: }
31:
32: 33: 34: 35: 36: 37:
38: public function has($key)
39: {
40: return ( ! is_null($this->get($key)));
41: }
42:
43: 44: 45: 46: 47: 48:
49: protected function retrieve($key)
50: {
51: if ($this->sectionable($key))
52: {
53: list($section, $key) = $this->parse($key);
54:
55: return $this->get_from_section($section, $key);
56: }
57: elseif (($cache = $this->memcache->get($this->key.$key)) !== false)
58: {
59: return $cache;
60: }
61: }
62:
63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75:
76: public function put($key, $value, $minutes)
77: {
78: if ($this->sectionable($key))
79: {
80: list($section, $key) = $this->parse($key);
81:
82: return $this->put_in_section($section, $key, $value, $minutes);
83: }
84: else
85: {
86: $this->memcache->set($this->key.$key, $value, $minutes * 60);
87: }
88: }
89:
90: 91: 92: 93: 94: 95: 96:
97: public function forever($key, $value)
98: {
99: if ($this->sectionable($key))
100: {
101: list($section, $key) = $this->parse($key);
102:
103: return $this->forever_in_section($section, $key, $value);
104: }
105: else
106: {
107: return $this->put($key, $value, 0);
108: }
109: }
110:
111: 112: 113: 114: 115: 116:
117: public function forget($key)
118: {
119: if ($this->sectionable($key))
120: {
121: list($section, $key) = $this->parse($key);
122:
123: if ($key == '*')
124: {
125: $this->forget_section($section);
126: }
127: else
128: {
129: $this->forget_in_section($section, $key);
130: }
131: }
132: else
133: {
134: $this->memcache->delete($this->key.$key);
135: }
136: }
137:
138: 139: 140: 141: 142: 143:
144: public function forget_section($section)
145: {
146: return $this->memcache->increment($this->key.$this->section_key($section));
147: }
148:
149: 150: 151: 152: 153: 154:
155: protected function section_id($section)
156: {
157: return $this->sear($this->section_key($section), function()
158: {
159: return rand(1, 10000);
160: });
161: }
162:
163: 164: 165: 166: 167: 168:
169: protected function section_key($section)
170: {
171: return $section.'_section_key';
172: }
173:
174: 175: 176: 177: 178: 179: 180:
181: protected function section_item_key($section, $key)
182: {
183: return $section.'#'.$this->section_id($section).'#'.$key;
184: }
185:
186: }