1 <?php
2
3 namespace Alo\Session;
4
5 use Alo;
6 use Alo\Cache\AbstractCache;
7 use Alo\Cache\MemcachedWrapper;
8 use Alo\Exception\ExtensionException as EE;
9
10 if (!defined('GEN_START')) {
11 http_response_code(404);
12 die();
13 }
14
15 16 17 18 19 20 21
22 class MemcachedSession extends AbstractSession {
23
24 25 26 27 28
29 protected $mc;
30
31 32 33 34 35 36
37 function __construct() {
38 if (!Alo::$cache) {
39 Alo::$cache = new MemcachedWrapper(true);
40 }
41
42 if (!AbstractCache::is_available()) {
43 throw new EE('No caching PHP extension is loaded', EE::E_EXT_NOT_LOADED);
44 } else {
45 $this->mc = &Alo::$cache;
46 parent::__construct();
47 \Log::debug('Initialised Memcached session');
48 }
49 }
50
51 protected function write() {
52 $this->mc->set(ALO_SESSION_MC_PREFIX . $this->id, $this->data, ALO_SESSION_TIMEOUT);
53 \Log::debug('Saved session data');
54
55 return $this;
56 }
57
58 protected function fetch() {
59 $data = $this->mc->get(ALO_SESSION_MC_PREFIX . $this->id);
60
61 if ($data) {
62 $this->data = $data;
63 }
64
65 \Log::debug('Fetched session data');
66
67 return $this;
68 }
69
70 function terminate() {
71 $this->mc->delete(ALO_SESSION_MC_PREFIX . $this->id);
72
73 return parent::terminate();
74 }
75
76 }