Hoa central
Xcache.php
Go to the documentation of this file.
1 <?php
2 
37 namespace Hoa\Cache\Backend;
38 
39 use Hoa\Cache;
40 
50 class Xcache extends Backend
51 {
59  public function __construct(Array $parameters = [])
60  {
61  if (!extension_loaded('xcache')) {
62  throw new Cache\Exception(
63  'XCache is not loaded on server.',
64  0
65  );
66  }
67 
68  parent::__construct($parameters);
69 
70  return;
71  }
72 
80  public function store($data)
81  {
82  $this->clean();
83 
84  if (true === $this->_parameters->getParameter('serialize_content')) {
85  $data = serialize($data);
86  }
87 
88  xcache_set(
89  $this->getIdMd5(),
90  $data,
91  $this->_parameters->getParameter('lifetime')
92  );
93 
94  return;
95  }
96 
102  public function load()
103  {
104  $this->clean();
105 
106  $content = xcache_get($this->getIdMd5());
107 
108  if (true === $this->_parameters->getParameter('serialize_content')) {
109  $content = unserialize($content);
110  }
111 
112  return $content;
113  }
114 
124  public function clean($lifetime = Cache::CLEAN_EXPIRED)
125  {
126  switch ($lifetime) {
127  case Cache::CLEAN_ALL:
128  for ($i = 0, $n = xcache_count(XC_TYPE_VAR); $i < $n; $i++) {
129  if (false !== xcache_clear_cache(XC_TYPE_VAR, $i)) {
130  throw new Cache\Exception(
131  'Clear all cache of XCache failed ' .
132  '(maybe for the cache number %s).',
133  1,
134  $i
135  );
136  }
137  }
138 
139  break;
140 
142  // Manage by XCache.
143  break;
144 
145  case Cache::CLEAN_USER:
146  throw new Cache\Exception(
147  '\Hoa\Cache::CLEAN_USER constant is not supported by ' .
148  'XCache backend.',
149  2
150  );
151  }
152 
153  return;
154  }
155 
162  public function remove()
163  {
164  xcache_clear_cache(XC_TYPE_VAR, $this->getId());
165 
166  return;
167  }
168 }
clean($lifetime=Cache::CLEAN_EXPIRED)
Definition: Xcache.php:124
const CLEAN_EXPIRED
Definition: Cache.php:62
const CLEAN_ALL
Definition: Cache.php:55
$content
Definition: Hoa.php:119
__construct(Array $parameters=[])
Definition: Xcache.php:59
const CLEAN_USER
Definition: Cache.php:69