XOOPS  2.6.0
Access.php
Go to the documentation of this file.
1 <?php
2 /*
3  You may not change or alter any portion of this comment or credits
4  of supporting developers from this source code or any supporting source code
5  which is considered copyrighted (c) material of the original comment or credit authors.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 */
11 
12 namespace Xoops\Core\Cache;
13 
14 use Stash\Pool;
17 
28 class Access
29 {
35  protected $pool = null;
36 
42  public function __construct(PoolInterface $pool)
43  {
44  $this->pool = $pool;
45  }
46 
58  public function write($key, $value, $ttl = null)
59  {
60  $item = $this->pool->getItem($key);
61  return $item->set($value, $ttl);
62  }
63 
71  public function read($key)
72  {
73  $item = $this->pool->getItem($key);
74  $value = $item->get(Invalidation::NONE);
75  return ($item->isMiss()) ? false : $value;
76  }
77 
85  public function delete($key)
86  {
87  $item = $this->pool->getItem($key);
88  return $item->clear();
89  }
90 
107  public function cacheRead($cacheKey, $regenFunction, $ttl = null, $args = null)
108  {
109  if (is_null($args)) {
110  $varArgs = array();
111  } else {
112  $varArgs = func_get_args();
113  array_shift($varArgs); // pull off $key
114  array_shift($varArgs); // pull off $regenFunction
115  array_shift($varArgs); // pull off $ttl
116  }
117 
118  $item = $this->pool->getItem($cacheKey);
119 
120  // Get the data from cache using the Stash\Invalidation::PRECOMPUTE technique
121  // for dealing with stampedes
122  $cachedContent = $item->get(Invalidation::PRECOMPUTE);
123 
124  // Check to see if the cache missed, which could mean that it either didn't exist or was stale.
125  if ($item->isMiss()) {
126  // Mark this instance as the one regenerating the cache.
127  $item->lock();
128 
129  // Run the relatively expensive code.
130  $cachedContent = call_user_func_array($regenFunction, $varArgs);
131 
132  // save result
133  $item->set($cachedContent, $ttl);
134  }
135 
136  return $cachedContent;
137  }
138 
144  public function garbageCollect()
145  {
146  return $this->pool->purge();
147  }
148 
154  public function clear()
155  {
156  return $this->pool->flush();
157  }
158 
167  public function pool()
168  {
169  return $this->pool;
170  }
171 }
const NONE
Definition: install.php:37
__construct(PoolInterface $pool)
Definition: Access.php:42
cacheRead($cacheKey, $regenFunction, $ttl=null, $args=null)
Definition: Access.php:107
write($key, $value, $ttl=null)
Definition: Access.php:58