Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 400 and 402] [Versions 400 and 403]

   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  class Google_Cache_Apc extends Google_Cache_Abstract
  31  {
  32    /**
  33     * @var Google_Client the current client
  34     */
  35    private $client;
  36  
  37    public function __construct(Google_Client $client)
  38    {
  39      if (! function_exists('apc_add') ) {
  40        $error = "Apc functions not available";
  41  
  42        $client->getLogger()->error($error);
  43        throw new Google_Cache_Exception($error);
  44      }
  45  
  46      $this->client = $client;
  47    }
  48  
  49     /**
  50     * @inheritDoc
  51     */
  52    public function get($key, $expiration = false)
  53    {
  54      $ret = apc_fetch($key);
  55      if ($ret === false) {
  56        $this->client->getLogger()->debug(
  57            'APC cache miss',
  58            array('key' => $key)
  59        );
  60        return false;
  61      }
  62      if (is_numeric($expiration) && (time() - $ret['time'] > $expiration)) {
  63        $this->client->getLogger()->debug(
  64            'APC cache miss (expired)',
  65            array('key' => $key, 'var' => $ret)
  66        );
  67        $this->delete($key);
  68        return false;
  69      }
  70  
  71      $this->client->getLogger()->debug(
  72          'APC cache hit',
  73          array('key' => $key, 'var' => $ret)
  74      );
  75  
  76      return $ret['data'];
  77    }
  78  
  79    /**
  80     * @inheritDoc
  81     */
  82    public function set($key, $value)
  83    {
  84      $var = array('time' => time(), 'data' => $value);
  85      $rc = apc_store($key, $var);
  86  
  87      if ($rc == false) {
  88        $this->client->getLogger()->error(
  89            'APC cache set failed',
  90            array('key' => $key, 'var' => $var)
  91        );
  92        throw new Google_Cache_Exception("Couldn't store data");
  93      }
  94  
  95      $this->client->getLogger()->debug(
  96          'APC cache set',
  97          array('key' => $key, 'var' => $var)
  98      );
  99    }
 100  
 101    /**
 102     * @inheritDoc
 103     * @param String $key
 104     */
 105    public function delete($key)
 106    {
 107      $this->client->getLogger()->debug(
 108          'APC cache delete',
 109          array('key' => $key)
 110      );
 111      apc_delete($key);
 112    }
 113  }