Modules

Kohana_Cache_Apcu
extends Cache
extends Kohana_Cache

Implements: Cache_Arithmetic | Kohana_Cache_Arithmetic

This class is a transparent base class for Cache_Apcu and should not be accessed directly.

Kohana Cache APCu data store driver for Kohana Cache library.

Configuration example

Below is an example of an apcu server configuration.

return [
    // Driver group
    'apcu' => [
        // Using APCu driver
        'driver' => 'apcu',
    ],
];

In cases where only one cache group is required, if the group is named default there is no need to pass the group name when instantiating a cache instance.

General cache group configuration settings

Below are the settings available to all types of cache driver.

Name Required Description
driver YES (string) The driver type to use

System requirements

  • Kohana 3.0.x
  • PHP 5.2.4 or greater
  • APCu PHP extension
package
Kohana/Cache
category
Base
author
Kohana Team
copyright
© 2009-2012 Kohana Team
license
https://kohana.top/license

Class declared in MODPATH/cache/classes/Kohana/Cache/Apcu.php on line 42.

Constants

DEFAULT_EXPIRE

integer 3600

Properties

public static string $default

default driver to use

string(4) "file"

public static Kohana_Cache $instances

instances

array(0) 

protected Config $_config

Default value:
array(0) 

Methods

public decrement( string $id [, int $step = integer 1 ] ) (defined in Kohana_Cache_Apcu)

Decrements a given value by the step value supplied. Useful for shared counters and other persistent integer based tracking.

Parameters

  • string $id required - Id of cache entry to decrement
  • int $step = integer 1 - Step value to decrement by

Return Values

  • integer
  • boolean

Source Code

public function decrement($id, $step = 1)
{
    if (apcu_exists($id)) {
        return apcu_dec($id, $step);
    } else {
        return false;
    }
}

public delete( string $id ) (defined in Kohana_Cache_Apcu)

Delete a cache entry based on id

// Delete 'foo' entry from the apcu group
Cache::instance('apcu')->delete('foo');

Parameters

  • string $id required - Id to remove from cache

Return Values

  • boolean

Source Code

public function delete($id)
{
    return apcu_delete($this->_sanitize_id($id));
}

public delete_all( ) (defined in Kohana_Cache_Apcu)

Delete all cache entries.

Beware of using this method when using shared memory cache systems, as it will wipe every entry within the system for all clients.

// Delete all cache entries in the apcu group
Cache::instance('apcu')->delete_all();

Return Values

  • boolean

Source Code

public function delete_all()
{
    return apcu_clear_cache();
}

public get( string $id [, string $default = NULL ] ) (defined in Kohana_Cache_Apcu)

Retrieve a cached value entry by id.

// Retrieve cache entry from apcu group
$data = Cache::instance('apcu')->get('foo');

// Retrieve cache entry from apcu group and return 'bar' if miss
$data = Cache::instance('apcu')->get('foo', 'bar');

Parameters

  • string $id required - Id of cache to entry
  • string $default = NULL - Default value to return if cache miss

Tags

Return Values

  • mixed

Source Code

public function get($id, $default = null)
{
    $data = apcu_fetch($this->_sanitize_id($id), $success);

    return $success ? $data : $default;
}

public increment( string $id [, int $step = integer 1 ] ) (defined in Kohana_Cache_Apcu)

Increments a given value by the step value supplied. Useful for shared counters and other persistent integer based tracking.

Parameters

  • string $id required - Id of cache entry to increment
  • int $step = integer 1 - Step value to increment by

Return Values

  • integer
  • boolean

Source Code

public function increment($id, $step = 1)
{
    if (apcu_exists($id)) {
        return apcu_inc($id, $step);
    } else {
        return false;
    }
}

public set( string $id , string $data [, integer $lifetime = NULL ] ) (defined in Kohana_Cache_Apcu)

Set a value to cache with id and lifetime

$data = 'bar';

// Set 'bar' to 'foo' in apcu group, using default expiry
Cache::instance('apcu')->set('foo', $data);

// Set 'bar' to 'foo' in apcu group for 30 seconds
Cache::instance('apcu')->set('foo', $data, 30);

Parameters

  • string $id required - Id of cache entry
  • string $data required - Data to set to cache
  • integer $lifetime = NULL - Lifetime in seconds

Return Values

  • boolean

Source Code

public function set($id, $data, $lifetime = null)
{
    if ($lifetime === null) {
        $lifetime = Arr::get($this->_config, 'default_expire', Cache::DEFAULT_EXPIRE);
    }

    return apcu_store($this->_sanitize_id($id), $data, $lifetime);
}

final public __clone( ) (defined in Kohana_Cache)

Overload the __clone() method to prevent cloning

Tags

Return Values

  • void

Source Code

final public function __clone()
{
    throw new Cache_Exception('Cloning of Kohana_Cache objects is forbidden');
}

public config( [ mixed $key = NULL , mixed $value = NULL ] ) (defined in Kohana_Cache)

Getter and setter for the configuration. If no argument provided, the current configuration is returned. Otherwise the configuration is set to this class.

// Overwrite all configuration
$cache->config(['driver' => 'memcache', '...']);

// Set a new configuration setting
$cache->config('servers', ['foo' => 'bar', '...']);

// Get a configuration setting
$servers = $cache->config('servers);

Parameters

  • mixed $key = NULL - Key to set to array, either array or config path
  • mixed $value = NULL - Value to associate with key

Return Values

  • mixed

Source Code

public function config($key = null, $value = null)
{
    if ($key === null)
        return $this->_config;

    if (is_array($key)) {
        $this->_config = $key;
    } else {
        if ($value === null)
            return Arr::get($this->_config, $key);

        $this->_config[$key] = $value;
    }

    return $this;
}

public static instance( [ string $group = NULL ] ) (defined in Kohana_Cache)

Creates a singleton of a Kohana Cache group. If no group is supplied the default cache group is used.

// Create an instance of the default group
$default_group = Cache::instance();

// Create an instance of a group
$foo_group = Cache::instance('foo');

// Access an instantiated group directly
$foo_group = Cache::$instances['default'];

Parameters

  • string $group = NULL - The name of the cache group to use [Optional]

Tags

Return Values

  • Cache

Source Code

public static function instance($group = null)
{
    // If there is no group supplied
    if ($group === null) {
        // Use the default setting
        $group = Cache::$default;
    }

    if (isset(Cache::$instances[$group])) {
        // Return the current group if initiated already
        return Cache::$instances[$group];
    }

    $config = Kohana::$config->load('cache');

    if (!$config->offsetExists($group)) {
        throw new Cache_Exception('Failed to load Kohana Cache group: :group', [':group' => $group]);
    }

    $config = $config->get($group);

    // Create a new cache type instance
    $cache_class = 'Cache_' . ucfirst($config['driver']);
    Cache::$instances[$group] = new $cache_class($config);

    // Return the instance
    return Cache::$instances[$group];
}

protected __construct( array $config ) (defined in Kohana_Cache_Apcu)

Check for existence of the APCu extension This method cannot be invoked externally. The driver must be instantiated using the Cache::instance() method.

Parameters

  • array $config required - Configuration

Tags

Source Code

protected function __construct(array $config)
{
    if (!extension_loaded('apcu')) {
        throw new Cache_Exception('PHP APCu extension is not available.');
    }

    parent::__construct($config);
}

protected _sanitize_id( string $id ) (defined in Kohana_Cache)

Replaces troublesome characters with underscores.

// Sanitize a cache id
$id = $this->_sanitize_id($id);

Parameters

  • string $id required - Id of cache to sanitize

Return Values

  • string

Source Code

protected function _sanitize_id($id)
{
    // Change slashes and spaces to underscores
    return str_replace(['/', '\\', ' '], '_', $id);
}