PHP Classes

File: x0cache/engine_apc.php

Recommend this page to a friend!
  Classes of Roberts   x0Cache   x0cache/engine_apc.php   Download  
File: x0cache/engine_apc.php
Role: Class source
Content type: text/plain
Description: Class source
Class: x0Cache
Store, retrieve values in several cache containers
Author: By
Last change:
Date: 12 years ago
Size: 2,857 bytes
 

Contents

Class file image Download
<?PHP
/*
 * x0Cache engine
 * Version 0.1.0
 * Copyright (c) 2012 F3llony, roberts@x0.lv
 * Website: http://cache.x0.lv/
 * Licensed under the terms of
 * GNU GENERAL PUBLIC LICENSE Version 3
 *
 * APC caching engine for x0Cache.
 */

class apc extends x0cache
   
{
    public
$config;
    public function
__construct($config)
        {
        if (
extension_loaded('apc') == false)
            {
            throw new
Exception('x0Cache: APC engine is loaded but APC extension is not present. Code: 1005', 1005);
            }
        if (
$config->apc->encrypt == true)
            {
            if (
$config->canEncrypt == false)
                {
                throw new
Exception('x0Cache: APC value encryption is enabled but Mcrypt extension is not available. Code: 5002', 5002);
                }
            elseif (empty(
$config->apc->encryptionKey))
                {
                throw new
Exception('x0Cache: APC value encryption is enabled but no encryption key is set. Code: 5003', 5003);
                }
            }
       
$this->config = $config;
        }
   
    public function
get($name)
        {
       
$value = apc_fetch($this->config->namePrefix . $name, $success);
        if (
$success == true)
            {
            if (
substr($value, 0, 7) == 'x0cser:')
                {
               
$value = unserialize(substr($value, 7));
                }
            elseif (
$this->config->apc->encrypt == true)
                {
               
$value = parent::decrypt($this->config->apc->encryptionKey, $value, $this->config->encryptionAlgo);
                }
            return
$value;
            }
        else
            {
            return
false;
            }
        }
   
    public function
set($key, $value, $ttl = 0)
        {
        if (
$this->config->apc->encrypt == false)
            {
            if (
is_object($value) || is_array($value))
                {
               
$value = 'x0cser:' . serialize($value);
                }
            return
apc_store($this->config->namePrefix . $key, $value, $ttl);
            }
        else
            {
           
$sval = parent::encrypt($this->config->apc->encryptionKey, $value, $this->config->encryptionAlgo);
            return
apc_store($this->config->namePrefix . $key, $sval, $ttl);
            }
        }
   
    public function
delete($key)
        {
        return
apc_delete($this->config->namePrefix . $key);
        }
   
    public function
exists($key)
        {
        return
apc_exists($this->config->namePrefix . $key);
        }
   
    public function
flush()
        {
       
$results = new APCIterator('user', '/(' . $this->config->namePrefix . ')+.*/i', APC_ITER_KEY);
        foreach (
$results as $item)
            {
           
apc_delete($item['key']);
            }
        return
count($results);
        }
    }