"Fossies" - the Fresh Open Source Software Archive

Member "papayacms-core-6.9.4/src/system/Papaya/Cache/Service/APC.php" (13 Dec 2019, 5971 Bytes) of package /linux/www/papayacms-core-6.9.4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "APC.php" see the Fossies "Dox" file reference documentation.

    1 <?php
    2 /**
    3  * papaya CMS
    4  *
    5  * @copyright 2000-2018 by papayaCMS project - All rights reserved.
    6  * @link http://www.papaya-cms.com/
    7  * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU General Public License, version 2
    8  *
    9  *  You can redistribute and/or modify this script under the terms of the GNU General Public
   10  *  License (GPL) version 2, provided that the copyright and license notes, including these
   11  *  lines, remain unmodified. papaya is distributed in the hope that it will be useful, but
   12  *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
   13  *  FOR A PARTICULAR PURPOSE.
   14  */
   15 namespace Papaya\Cache\Service;
   16 
   17 use Papaya\Cache;
   18 
   19 /**
   20  * Papaya Cache Service for APC based cache
   21  *
   22  * @package Papaya-Library
   23  * @subpackage Cache
   24  */
   25 class APC extends Cache\Service {
   26   /**
   27    * process cache - to avoid double requests
   28    *
   29    * @var array
   30    */
   31   protected $_localCache = [];
   32 
   33   /**
   34    * process cache - cache create times
   35    *
   36    * @var array
   37    */
   38   protected $_cacheCreated = [];
   39 
   40   /**
   41    * APC object
   42    *
   43    * @var APC\Wrapper
   44    */
   45   private $_apcObject;
   46 
   47   /**
   48    * The APC does not need any configuration, So just overwrite the abstract function with an empty
   49    * one.
   50    *
   51    * @param Cache\Configuration $configuration
   52    *
   53    * @return bool
   54    */
   55   public function setConfiguration(Cache\Configuration $configuration) {
   56     return TRUE;
   57   }
   58 
   59   /**
   60    * check if APC is here
   61    *
   62    * @param bool $silent
   63    *
   64    * @throws \LogicException
   65    *
   66    * @return bool
   67    */
   68   public function verify($silent = TRUE) {
   69     $valid = $this->getAPCObject()->available();
   70     if (!($silent || $valid)) {
   71       throw new \LogicException('APC is not available');
   72     }
   73     return $valid;
   74   }
   75 
   76   /**
   77    * Get APC mapper object instance
   78    *
   79    * @return APC\Wrapper
   80    */
   81   public function getAPCObject() {
   82     if (NULL === $this->_apcObject) {
   83       $this->_apcObject = new APC\Wrapper();
   84     }
   85     return $this->_apcObject;
   86   }
   87 
   88   /**
   89    * Set APC mapper object instance
   90    *
   91    * @param APC\Wrapper $apcObject
   92    */
   93   public function setAPCObject(APC\Wrapper $apcObject) {
   94     $this->_apcObject = $apcObject;
   95   }
   96 
   97   /**
   98    * Write element to cache
   99    *
  100    * @param string $group
  101    * @param string $element
  102    * @param string $parameters
  103    * @param string $data Element data
  104    * @param int $expires Maximum age in seconds
  105    *
  106    * @return bool
  107    */
  108   public function write($group, $element, $parameters, $data, $expires = NULL) {
  109     if (
  110       $this->verify() &&
  111       ($cacheId = $this->getCacheIdentifier($group, $element, $parameters)) &&
  112       $this->getAPCObject()->store($cacheId, [\time(), $data], $expires)
  113     ) {
  114       return $cacheId;
  115     }
  116     return FALSE;
  117   }
  118 
  119   /**
  120    * Read element from cache
  121    *
  122    * @param string $group
  123    * @param string $element
  124    * @param string $parameters
  125    * @param int $expires Maximum age in seconds
  126    * @param int $ifModifiedSince first possible creation time
  127    *
  128    * @return string|false
  129    */
  130   public function read($group, $element, $parameters, $expires, $ifModifiedSince = NULL) {
  131     if ($this->verify() && ($cacheId = $this->getCacheIdentifier($group, $element, $parameters))) {
  132       if (isset($this->_localCache[$cacheId]) ||
  133         $this->_read($cacheId, $expires, $ifModifiedSince)) {
  134         return $this->_localCache[$cacheId];
  135       }
  136     }
  137     return FALSE;
  138   }
  139 
  140   /**
  141    * Check if element in cache exists and is still valid
  142    *
  143    * @param string $group
  144    * @param string $element
  145    * @param string $parameters
  146    * @param int $expires Maximum age in seconds
  147    * @param int $ifModifiedSince first possible creation time
  148    *
  149    * @return bool
  150    */
  151   public function exists($group, $element, $parameters, $expires, $ifModifiedSince = NULL) {
  152     if ($this->verify() && ($cacheId = $this->getCacheIdentifier($group, $element, $parameters))) {
  153       if (isset($this->_localCache[$cacheId])) {
  154         return !empty($this->_localCache[$cacheId]);
  155       }
  156       return (bool)$this->_read($cacheId, $expires, $ifModifiedSince);
  157     }
  158     return FALSE;
  159   }
  160 
  161   /**
  162    * Check if element in cache exists and which time is was created
  163    *
  164    * @param string $group
  165    * @param string $element
  166    * @param string $parameters
  167    * @param int $expires Maximum age in seconds
  168    * @param int $ifModifiedSince first possible creation time
  169    *
  170    * @return int|false
  171    */
  172   public function created($group, $element, $parameters, $expires, $ifModifiedSince = NULL) {
  173     if ($this->verify() && ($cacheId = $this->getCacheIdentifier($group, $element, $parameters))) {
  174       if (isset($this->_cacheCreated[$cacheId])) {
  175         return $this->_cacheCreated[$cacheId];
  176       }
  177       if ($this->_read($cacheId, $expires, $ifModifiedSince)) {
  178         return $this->_cacheCreated[$cacheId];
  179       }
  180     }
  181     return FALSE;
  182   }
  183 
  184   /**
  185    * Delete element(s) from cache, apc supports no groups - so delete all
  186    *
  187    * @param string $group
  188    * @param string $element
  189    * @param string $parameters
  190    *
  191    * @return int
  192    */
  193   public function delete($group = NULL, $element = NULL, $parameters = NULL) {
  194     if ($this->verify()) {
  195       $this->getAPCObject()->clearCache('user');
  196       return TRUE;
  197     }
  198     return 0;
  199   }
  200 
  201   /**
  202    * internal read item from cache - cache items are arrays with two element,
  203    * first element contains cache time
  204    *
  205    * @param $cacheId
  206    * @param $expires
  207    * @param $ifModifiedSince
  208    *
  209    * @return bool
  210    */
  211   private function _read($cacheId, $expires, $ifModifiedSince) {
  212     $cache = $this->getAPCObject()->fetch($cacheId);
  213     if (\is_array($cache) && 2 === \count($cache)) {
  214       $created = (int)$cache[0];
  215       if (($created + $expires) > \time()) {
  216         if (NULL === $ifModifiedSince || $ifModifiedSince < $created) {
  217           $this->_cacheCreated[$cacheId] = $created;
  218           $this->_localCache[$cacheId] = $cache[1];
  219           return TRUE;
  220         }
  221       }
  222     }
  223     return FALSE;
  224   }
  225 }