mirror of
https://github.com/alchemy-fr/Phraseanet.git
synced 2025-10-09 19:13:26 +00:00
V 3.5 RC 1
This commit is contained in:
174
lib/classes/cache/opcode/adapter.class.php
vendored
Normal file
174
lib/classes/cache/opcode/adapter.class.php
vendored
Normal file
@@ -0,0 +1,174 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Phraseanet
|
||||
*
|
||||
* (c) 2005-2010 Alchemy
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
/**
|
||||
*
|
||||
* @package cache
|
||||
* @license http://opensource.org/licenses/gpl-3.0 GPLv3
|
||||
* @link www.phraseanet.com
|
||||
*/
|
||||
class cache_opcode_adapter implements cache_opcode_Interface
|
||||
{
|
||||
/**
|
||||
*
|
||||
*/
|
||||
const APC = 0;
|
||||
/**
|
||||
*
|
||||
*/
|
||||
const XCACHE = 1;
|
||||
/**
|
||||
*
|
||||
*/
|
||||
const NOCACHE = 2;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var const
|
||||
*/
|
||||
protected $cache_method;
|
||||
/**
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected static $_static_cache = array();
|
||||
/**
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $prefix;
|
||||
|
||||
/**
|
||||
*
|
||||
* @return cache_opcode_adapter
|
||||
*/
|
||||
public function __construct($prefix = '')
|
||||
{
|
||||
if (!http_request::is_command_line() && function_exists('apc_store'))
|
||||
$this->cache_method = self::APC;
|
||||
elseif (!http_request::is_command_line() && function_exists('xcache_set'))
|
||||
$this->cache_method = self::XCACHE;
|
||||
else
|
||||
$this->cache_method = self::NOCACHE;
|
||||
|
||||
$this->prefix = $prefix;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param string $key
|
||||
* @return mixed
|
||||
*/
|
||||
public function get($key)
|
||||
{
|
||||
$key = sprintf('%s_%s', $this->prefix, $key);
|
||||
switch ($this->cache_method)
|
||||
{
|
||||
case self::APC:
|
||||
return apc_fetch($key);
|
||||
break;
|
||||
case self::XCACHE:
|
||||
return xcache_get($key);
|
||||
break;
|
||||
default:
|
||||
return isset(self::$_static_cache[$key]) ? self::$_static_cache[$key] : null;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed $var
|
||||
* @return cache_opcode_adapter
|
||||
*/
|
||||
public function set($key, $var)
|
||||
{
|
||||
$key = sprintf('%s_%s', $this->prefix, $key);
|
||||
switch ($this->cache_method)
|
||||
{
|
||||
case self::APC:
|
||||
if ($this->is_set($key))
|
||||
apc_delete($key);
|
||||
apc_store($key, $var);
|
||||
break;
|
||||
case self::XCACHE:
|
||||
if ($this->is_set($key))
|
||||
$this->un_set ($key);
|
||||
xcache_set($key, $var);
|
||||
break;
|
||||
default:
|
||||
self::$_static_cache[$key] = $var;
|
||||
break;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param string $key
|
||||
* @return boolean
|
||||
*/
|
||||
public function is_set($key)
|
||||
{
|
||||
$key = sprintf('%s_%s', $this->prefix, $key);
|
||||
switch ($this->cache_method)
|
||||
{
|
||||
case self::APC:
|
||||
if (function_exists('apc_exists'))
|
||||
{
|
||||
return apc_exists($key);
|
||||
}
|
||||
else
|
||||
{
|
||||
apc_fetch($key, $succes);
|
||||
|
||||
return $succes;
|
||||
}
|
||||
break;
|
||||
case self::XCACHE:
|
||||
return xcache_isset($key);
|
||||
break;
|
||||
default:
|
||||
return isset(self::$_static_cache[$key]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param string $key
|
||||
* @return cache_opcode_adapter
|
||||
*/
|
||||
public function un_set($key)
|
||||
{
|
||||
$key = sprintf('%s_%s', $this->prefix, $key);
|
||||
switch ($this->cache_method)
|
||||
{
|
||||
case self::APC:
|
||||
apc_delete($key);
|
||||
break;
|
||||
case self::XCACHE:
|
||||
xcache_unset($key);
|
||||
break;
|
||||
default:
|
||||
if (isset(self::$_static_cache[$key]))
|
||||
unset(self::$_static_cache[$key]);
|
||||
break;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
}
|
27
lib/classes/cache/opcode/interface.class.php
vendored
Normal file
27
lib/classes/cache/opcode/interface.class.php
vendored
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Phraseanet
|
||||
*
|
||||
* (c) 2005-2010 Alchemy
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
/**
|
||||
*
|
||||
* @package
|
||||
* @license http://opensource.org/licenses/gpl-3.0 GPLv3
|
||||
* @link www.phraseanet.com
|
||||
*/
|
||||
interface cache_opcode_interface
|
||||
{
|
||||
public function get($key);
|
||||
|
||||
public function set($key, $var);
|
||||
|
||||
public function is_set($key);
|
||||
|
||||
public function un_set($key);
|
||||
}
|
Reference in New Issue
Block a user