Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402]

   1  <?php
   2  /*
   3   * Copyright 2010 Google Inc.
   4   *
   5   * Licensed under the Apache License, Version 2.0 (the "License");
   6   * you may not use this file except in compliance with the License.
   7   * You may obtain a copy of the License at
   8   *
   9   *     http://www.apache.org/licenses/LICENSE-2.0
  10   *
  11   * Unless required by applicable law or agreed to in writing, software
  12   * distributed under the License is distributed on an "AS IS" BASIS,
  13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14   * See the License for the specific language governing permissions and
  15   * limitations under the License.
  16   */
  17  
  18  if (!class_exists('Google_Client')) {
  19    require_once dirname(__FILE__) . '/../autoload.php';
  20  }
  21  
  22  /**
  23   * A persistent storage class based on the APC cache, which is not
  24   * really very persistent, as soon as you restart your web server
  25   * the storage will be wiped, however for debugging and/or speed
  26   * it can be useful, and cache is a lot cheaper then storage.
  27   *
  28   * @author Chris Chabot <chabotc@google.com>
  29   */
  30  #[AllowDynamicProperties]
  31  class Google_Cache_Apc extends Google_Cache_Abstract
  32  {
  33    /**
  34     * @var Google_Client the current client
  35     */
  36    private $client;
  37  
  38    public function __construct(Google_Client $client)
  39    {
  40      if (! function_exists('apc_add') ) {
  41        $error = "Apc functions not available";
  42  
  43        $client->getLogger()->error($error);
  44        throw new Google_Cache_Exception($error);
  45      }
  46  
  47      $this->client = $client;
  48    }
  49  
  50     /**
  51     * @inheritDoc
  52     */
  53    public function get($key, $expiration = false)
  54    {
  55      $ret = apc_fetch($key);
  56      if ($ret === false) {
  57        $this->client->getLogger()->debug(
  58            'APC cache miss',
  59            array('key' => $key)
  60        );
  61        return false;
  62      }
  63      if (is_numeric($expiration) && (time() - $ret['time'] > $expiration)) {
  64        $this->client->getLogger()->debug(
  65            'APC cache miss (expired)',
  66            array('key' => $key, 'var' => $ret)
  67        );
  68        $this->delete($key);
  69        return false;
  70      }
  71  
  72      $this->client->getLogger()->debug(
  73          'APC cache hit',
  74          array('key' => $key, 'var' => $ret)
  75      );
  76  
  77      return $ret['data'];
  78    }
  79  
  80    /**
  81     * @inheritDoc
  82     */
  83    public function set($key, $value)
  84    {
  85      $var = array('time' => time(), 'data' => $value);
  86      $rc = apc_store($key, $var);
  87  
  88      if ($rc == false) {
  89        $this->client->getLogger()->error(
  90            'APC cache set failed',
  91            array('key' => $key, 'var' => $var)
  92        );
  93        throw new Google_Cache_Exception("Couldn't store data");
  94      }
  95  
  96      $this->client->getLogger()->debug(
  97          'APC cache set',
  98          array('key' => $key, 'var' => $var)
  99      );
 100    }
 101  
 102    /**
 103     * @inheritDoc
 104     * @param String $key
 105     */
 106    public function delete($key)
 107    {
 108      $this->client->getLogger()->debug(
 109          'APC cache delete',
 110          array('key' => $key)
 111      );
 112      apc_delete($key);
 113    }
 114  }