V 3.5 RC 1

This commit is contained in:
Romain Neutron
2011-12-05 00:23:28 +01:00
parent 6f1ee368aa
commit 4c5b7eb658
5563 changed files with 466984 additions and 985416 deletions

267
lib/classes/cache/adapter.class.php vendored Normal file
View File

@@ -0,0 +1,267 @@
<?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
*/
class cache_adapter implements cache_interface
{
/**
*
* @var Array
*/
protected $adapters = array(
'nocache' => array(
'cache_nocache'
),
'memcached' => array(
'cache_memcached'
, 'cache_memcache'
),
'redis' => array(
'cache_redis'
)
);
/**
*
* @var cache_interface
*/
protected $cache;
/**
*
* @var cache
*/
protected static $_instance;
/**
*
* @var boolean
*/
protected static $_loaded;
/**
*
* @var string
*/
protected $current_adapter;
/**
*
* @var string
*/
protected $prefix;
/**
*
* @param registryInterface $registry
* @param string $type
* @return cache_adapter
*/
protected function __construct(registryInterface $registry)
{
$type = $registry->get('GV_cache_server_type');
if (trim($type) === '')
$type = 'nocache';
$this->prefix = $registry->get('GV_sit');
if (self::$_loaded === true)
throw new Exception('Already tried to load, no adapters');
if (!isset($this->adapters[$type]))
throw new Exception(sprintf('Unknow cache type %s', $type));
$loaded = false;
$n = 0;
while (!$loaded && $n < count($this->adapters[$type]))
{
try
{
$this->cache = new $this->adapters[$type][$n]($registry);
$this->current_adapter = $this->adapters[$type][$n];
$this->cache->ping();
$loaded = true;
}
catch (Exception $e)
{
$this->cache = $this->current_adapter = null;
$n++;
unset($e);
}
}
if (!($this->cache instanceof cache_interface))
{
$this->current_adapter = 'nocache';
$this->cache = new cache_nocache($registry);
}
self::$_loaded = true;
return $this;
}
/**
*
* @param registryInterface $registry
* @param string $type
* @return cache_adapter
*/
public static function get_instance(registryInterface $registry)
{
if (!self::$_instance instanceof self)
self::$_instance = new self($registry);
return self::$_instance;
}
/**
*
* @return string
*/
public function get_current_adpapter()
{
return $this->current_adapter;
}
/**
*
* @param <type> $key
* @param <type> $value
* @param <type> $expiration
* @return boolean
*/
public function set($key, $value, $expiration = 0)
{
try
{
return $this->cache->set($this->generate_key($key), $value, $expiration);
return true;
}
catch (Exception $e)
{
unset($e);
return false;
}
}
protected function generate_key($key)
{
if (is_string($key))
{
return md5($this->prefix . ' ' . $key);
}
if (is_array($key))
{
$ret = array();
foreach ($key as $k => $v)
{
$ret[$k] = md5($this->prefix . ' ' . $v);
}
return $ret;
}
}
/**
*
* @param <type> $key
* @return <type>
*/
public function get($key)
{
$tmp = $this->cache->get($this->generate_key($key));
return $tmp;
}
/**
*
* @param <type> $key
* @return <type>
*/
public function delete($key)
{
return $this->cache->delete($this->generate_key($key));
}
/**
*
* @param array $array_keys
* @return <type>
*/
public function deleteMulti(Array $array_keys)
{
return $this->cache->deleteMulti($this->generate_key($array_keys));
}
/**
*
* @return <type>
*/
public function getStats()
{
return $this->cache->getStats();
}
/**
*
* @return <type>
*/
public function flush()
{
return $this->cache->flush();
}
/**
*
* @return <type>
*/
public function get_version()
{
return $this->cache->getVersion();
}
/**
*
* @return boolean
*/
public function ping()
{
return $this->cache->ping();
}
/**
*
* @param string $message
* @return cache_adapter
*/
protected function log($message)
{
$registry = registry::get_instance();
$date = new DateTime();
$message = $date->format(DATE_ATOM) . " $message \n";
$filename = $registry->get('GV_RootPath') . 'logs/cache.log';
file_put_contents($filename, $message, FILE_APPEND);
return $this;
}
}

View File

@@ -1,70 +0,0 @@
<?php
class cache_appbox
{
private static $_instance = false;
var $_c_obj = false;
function __construct()
{
$this->_c_obj = cache::getInstance();
}
/**
* @return cache_appbox
*/
public static function getInstance()
{
if (!(self::$_instance instanceof self))
self::$_instance = new self();
return self::$_instance;
}
public function get($id)
{
return $this->_c_obj->get(GV_ServerName.'_appbox_'.$id);
}
public function set($id,$value)
{
return $this->_c_obj->set(GV_ServerName.'_appbox_'.$id,$value);
}
public function delete($id)
{
// if(in_array($id, array('sbas_names','bas_names')))
// {
// $this->delete('list_bases');
// }
if($id == 'list_bases')
{
$this->delete('bas_names');
$this->delete('sbas_names');
$this->delete('sbas_from_bas');
$avLanguages = user::detectlanguage();
foreach($avLanguages as $lng=>$languages)
{
foreach($languages as $locale=>$language)
{
$this->delete('bases_settings_'.$locale);
}
}
}
return $this->_c_obj->delete(GV_ServerName.'_appbox_'.$id);
}
public function is_ok()
{
return $this->_c_obj->is_ok();
}
}

View File

@@ -1,113 +0,0 @@
<?php
class cache_basket
{
private static $_instance = false;
var $_c_obj = false;
function __construct()
{
$this->_c_obj = cache::getInstance();
}
/**
* @return cache_basket
*/
public static function getInstance()
{
if (!(self::$_instance instanceof self))
self::$_instance = new self();
return self::$_instance;
}
public function get($usr_id, $ssel_id)
{
return $this->_c_obj->get(GV_ServerName.'_basket_'.$usr_id.'_'.$ssel_id);
}
public function set($usr_id, $ssel_id,$value)
{
return $this->_c_obj->set(GV_ServerName.'_basket_'.$usr_id.'_'.$ssel_id,$value);
}
public function delete($usr_id, $ssel_id)
{
try
{
$basket = basket::getInstance($ssel_id, $usr_id);
if($basket->valid)
{
foreach($basket->validating_users as $user_data)
{
$this->_c_obj->delete(GV_ServerName.'_basket_'.$user_data['usr_id'].'_'.$ssel_id);
}
}
return $this->_c_obj->delete(GV_ServerName.'_basket_'.$usr_id.'_'.$ssel_id);
}
catch(Exception $e)
{
}
}
/**
* Revoke cache when user rights have been modified - do not cache datas which are now forbidden
* @param $usr_id
* @return boolean
*/
public function revoke_baskets_usr($usr_id)
{
$conn = connection::getInstance();
$ssel_ids = array();
if($conn)
{
$sql = 'SELECT ssel_id FROM ssel WHERE deleted="0"';
if($rs = $conn->query($sql))
{
while($row = $conn->fetch_assoc($rs))
{
$ssel_ids[] = GV_ServerName.'_basket_'.$usr_id.'_'.$row['ssel_id'];
}
$conn->free_result($rs);
}
}
return $this->_c_obj->deleteMulti($ssel_ids);
}
/**
* Revoke cache when user documents have their collection changed or status - do not cache datas which are now forbidden
* @param $usr_id
* @return boolean
*/
public function revoke_baskets_record($records_array)
{
$conn = connection::getInstance();
$keys = array();
if($conn)
{
$sql = 'SELECT s.ssel_id, s.usr_id FROM ssel s, sselcont c WHERE (c.record_id="'.implode('" OR c.record_id="',$records_array).'") AND c.ssel_id = s.ssel_id';
if($rs = $conn->query($sql))
{
while($row = $conn->fetch_assoc($rs))
{
$keys[] = GV_ServerName.'_basket_'.$row['usr_id'].'_'.$row['ssel_id'];
}
$conn->free_result($rs);
}
}
return $this->_c_obj->deleteMulti($keys);
}
}

View File

@@ -1,47 +0,0 @@
<?php
class cache_baskets
{
private static $_instance = false;
var $_c_obj = false;
function __construct()
{
$this->_c_obj = cache::getInstance();
}
/**
* @return cache_baskets
*/
public static function getInstance()
{
if (!(self::$_instance instanceof self))
self::$_instance = new self();
return self::$_instance;
}
public function get($usr_id)
{
return $this->_c_obj->get(GV_ServerName.'_baskets_'.$usr_id);
}
public function set($usr_id,$value)
{
return $this->_c_obj->set(GV_ServerName.'_baskets_'.$usr_id,$value);
}
public function delete($usr_id)
{
return $this->_c_obj->delete(GV_ServerName.'_baskets_'.$usr_id);
}
}

View 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 cache
* @license http://opensource.org/licenses/gpl-3.0 GPLv3
* @link www.phraseanet.com
*/
interface cache_cacheableInterface
{
public function get_cache_key($option = null);
public function get_data_from_cache($option = null);
public function set_data_to_cache($value, $option = null, $duration = 0);
public function delete_data_from_cache($option = null);
}

View File

@@ -1,47 +0,0 @@
<?php
class cache_collection
{
private static $_instance = false;
var $_c_obj = false;
function __construct()
{
$this->_c_obj = cache::getInstance();
}
/**
* @return cache_collection
*/
public static function getInstance()
{
if (!(self::$_instance instanceof self))
self::$_instance = new self();
return self::$_instance;
}
public function get($base_id,$what)
{
return $this->_c_obj->get(GV_ServerName.'_collection_'.$base_id.'_'.$what);
}
public function set($base_id,$what,$bin)
{
return $this->_c_obj->set(GV_ServerName.'_collection_'.$base_id.'_'.$what,$bin);
}
public function delete($base_id,$what)
{
return $this->_c_obj->delete(GV_ServerName.'_collection_'.$base_id.'_'.$what);
}
}

View File

@@ -1,120 +1,193 @@
<?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_databox
{
private static $_instance = false;
var $_c_obj = false;
function __construct()
{
$this->_c_obj = cache::getInstance();
}
/**
* @return cache_databox
*/
public static function getInstance()
{
if (!(self::$_instance instanceof self))
self::$_instance = new self();
return self::$_instance;
}
public function get($type,$what)
{
return $this->_c_obj->get(GV_ServerName.'_databox_'.$type.'_'.$what);
}
public function set($type,$what,$bin)
{
return $this->_c_obj->set(GV_ServerName.'_databox_'.$type.'_'.$what,$bin);
}
public function delete($type,$what)
{
return $this->_c_obj->delete(GV_ServerName.'_databox_'.$type.'_'.$what);
}
function refresh($sbas_id)
{
$date = new DateTime('-30 seconds');
$cache_appbox = cache_appbox::getInstance();
$last_update = $cache_appbox->get('memcached_update');
if($last_update)
$last_update = new DateTime($last_update);
else
$last_update = new DateTime('-10 years');
/**
*
* @var cache_databox
*/
private static $_instance = false;
/**
*
* @var cache
*/
protected $_c_obj = false;
if($date > $last_update && $cache_appbox->is_ok())
{
$connsbas = connection::getInstance($sbas_id);
if($connsbas)
{
$sql = 'SELECT type, value FROM memcached WHERE site_id="'.$connsbas->escape_string(GV_ServerName).'"';
if($rs = $connsbas->query($sql))
{
$cache_record = cache_record::getInstance();
$cache_thumbnail = cache_thumbnail::getInstance();
$cache_preview = cache_preview::getInstance();
while($row = $connsbas->fetch_assoc($rs))
{
switch($row['type'])
{
case 'record':
$cache_record->delete($sbas_id,$row['value'],false);
$cache_thumbnail->delete($sbas_id,$row['value'],false);
$cache_preview->delete($sbas_id,$row['value'],false);
$sql = 'DELETE FROM memcached WHERE site_id="'.$connsbas->escape_string(GV_ServerName).'" AND type="record" AND value="'.$row['value'].'"';
$connsbas->query($sql);
break;
case 'structure':
$cache_appbox->delete('list_bases');
$sql = 'DELETE FROM memcached WHERE site_id="'.$connsbas->escape_string(GV_ServerName).'" AND type="structure" AND value="'.$row['value'].'"';
$connsbas->query($sql);
break;
}
}
}
}
$date = new DateTime();
$now = phraseadate::format_mysql($date);
$cache_appbox->set('memcached_update',$now);
$conn = connection::getInstance();
$sql = 'UPDATE sitepreff SET memcached_update="'.$conn->escape_string($now).'"';
$conn->query($sql);
}
}
function update($sbas_id, $type, $value='')
{
$connbas = connection::getInstance($sbas_id);
if($connbas)
{
$sql = 'SELECT distinct site_id as site_id FROM clients WHERE site_id != "'.$connbas->escape_string(GV_ServerName).'"';
if($rs = $connbas->query($sql))
{
while($row = $connbas->fetch_assoc($rs))
{
$sql = 'REPLACE INTO memcached (site_id, type, value) VALUES ("'.$connbas->escape_string($row['site_id']).'","'.$connbas->escape_string($type).'","'.$connbas->escape_string($value).'")';
$connbas->query($sql);
}
$connbas->free_result($rs);
}
}
return true;
}
}
/**
*
* @return cache_databox
*/
function __construct()
{
$this->_c_obj = cache_adapter::getInstance(registry::get_instance());
return $this;
}
/**
* @return cache_databox
*/
public static function getInstance()
{
if (!(self::$_instance instanceof self))
self::$_instance = new self();
return self::$_instance;
}
/**
*
* @param string $type
* @param string $what
* @return boolean
*/
public function get($type, $what)
{
return $this->_c_obj->get('_databox_' . $type . '_' . $what);
}
/**
*
* @param string $type
* @param string $what
* @param mixed content $bin
* @return boolean
*/
public function set($type, $what, $bin)
{
return $this->_c_obj->set('_databox_' . $type . '_' . $what, $bin);
}
/**
*
* @param string $type
* @param string $what
* @return boolean
*/
public function delete($type, $what)
{
return $this->_c_obj->delete('_databox_' . $type . '_' . $what);
}
/**
*
* @param int $sbas_id
* @return cache_databox
*/
function refresh($sbas_id)
{
$date = new DateTime('-30 seconds');
$registry = registry::get_instance();
$cache_appbox = cache_appbox::getInstance();
$last_update = $cache_appbox->get('memcached_update');
if ($last_update)
$last_update = new DateTime($last_update);
else
$last_update = new DateTime('-10 years');
if ($date <= $last_update || !$cache_appbox->is_ok())
return $this;
$connsbas = connection::getInstance($sbas_id);
if (!$connsbas)
return $this;
$sql = 'SELECT type, value FROM memcached
WHERE site_id="' . $connsbas->escape_string($registry->get('GV_ServerName')) . '"';
if ($rs = $connsbas->query($sql))
{
$cache_thumbnail = cache_thumbnail::getInstance();
$cache_preview = cache_preview::getInstance();
while ($row = $connsbas->fetch_assoc($rs))
{
switch ($row['type'])
{
case 'record':
$cache_thumbnail->delete($sbas_id, $row['value'], false);
$cache_preview->delete($sbas_id, $row['value'], false);
$sql = 'DELETE FROM memcached
WHERE site_id="' . $connsbas->escape_string($registry->get('GV_ServerName')) . '"
AND type="record" AND value="' . $row['value'] . '"';
$connsbas->query($sql);
break;
case 'structure':
$cache_appbox->delete('list_bases');
$sql = 'DELETE FROM memcached
WHERE site_id="' . $connsbas->escape_string($registry->get('GV_ServerName')) . '"
AND type="structure" AND value="' . $row['value'] . '"';
$connsbas->query($sql);
break;
}
}
$connsbas->free_result($rs);
}
$date = new DateTime();
$now = phraseadate::format_mysql($date);
$cache_appbox->set('memcached_update', $now);
$conn = connection::getInstance();
$sql = 'UPDATE sitepreff
SET memcached_update="' . $conn->escape_string($now) . '"';
$conn->query($sql);
return $this;
}
/**
*
* @param int $sbas_id
* @param string $type
* @param mixed content $value
* @return Void
*/
function update($sbas_id, $type, $value='')
{
$connbas = connection::getPDOConnection($sbas_id);
$registry = registry::get_instance();
$sql = 'SELECT distinct site_id as site_id
FROM clients
WHERE site_id != :site_id';
$stmt = $connbas->prepare($sql);
$stmt->execute(array(':site_id' => $registry->get('GV_ServerName')));
$rs = $stmt->fetchAll(PDO::FETCH_ASSOC);
$stmt->closeCursor();
$sql = 'REPLACE INTO memcached (site_id, type, value)
VALUES (:site_id, :type, :value)';
$stmt = $connbas->prepare($sql);
foreach ($rs as $row)
{
$stmt->execute(array(':site_id' => $row['site_id'], ':type' => $type, ':value' => $value));
}
$stmt->closeCursor();
return;
}
}

View File

@@ -1,47 +0,0 @@
<?php
class cache_feed
{
private static $_instance = false;
var $_c_obj = false;
function __construct()
{
$this->_c_obj = cache::getInstance();
}
/**
* @return cache_feed
*/
public static function getInstance()
{
if (!(self::$_instance instanceof self))
self::$_instance = new self();
return self::$_instance;
}
public function get($feed_id)
{
return $this->_c_obj->get(GV_ServerName.'_feed_'.$feed_id);
}
public function set($feed_id,$datas)
{
return $this->_c_obj->set(GV_ServerName.'_feed_'.$feed_id,$datas, 1800);
}
public function delete($feed_id)
{
return $this->_c_obj->delete(GV_ServerName.'_feed_'.$feed_id);
}
}

35
lib/classes/cache/interface.class.php vendored Normal file
View File

@@ -0,0 +1,35 @@
<?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
*/
interface cache_interface
{
public function set($key, $value, $expiration);
public function get($key);
public function delete($key);
public function deleteMulti(Array $array_keys);
public function getStats();
public function flush();
public function get_version();
public function ping();
}

158
lib/classes/cache/memcache.class.php vendored Normal file
View File

@@ -0,0 +1,158 @@
<?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_memcache implements cache_interface
{
/**
*
* @var cache_interface
*/
protected $memcache;
/**
*
* @var boolean
*/
protected $online = false;
protected $host;
protected $port;
/**
*
* @param registryInterface $registry
* @return cache_memcache
*/
public function __construct(registryInterface $registry)
{
if (!extension_loaded('Memcache'))
{
throw new Exception('Memcache is not loaded');
}
$this->memcache = new Memcache();
$this->host = $registry->get('GV_cache_server_host');
$this->port = $registry->get('GV_cache_server_port');
$this->memcache->addServer($this->host, $this->port);
if (!$this->memcache->getServerStatus($this->host, $this->port))
throw new Exception('Unable to connect');
$this->online = true;
return $this;
}
/**
*
* @param string $key
* @param mixed $value
* @param int $expiration
* @return cache_memcache
*/
public function set($key, $value, $expiration)
{
$this->memcache->set($key, $value, 0, $expiration);
return $this;
}
/**
*
* @param string $key
* @return mixed
*/
public function get($key)
{
$value = $this->memcache->get($key);
if ($value === false)
{
throw new Exception('Unable to retrieve the value');
}
return $value;
}
/**
*
* @param string $key
* @return cache_memcache
*/
public function delete($key)
{
$this->memcache->delete($key);
return $this;
}
/**
*
* @param array $array_keys
* @return cache_memcache
*/
public function deleteMulti(Array $array_keys)
{
foreach ($array_keys as $key)
$this->memcache->delete($key);
return $this;
}
/**
*
* @return array
*/
public function getStats()
{
return array(
$this->host . ':' . $this->port => $this->memcache->getStats()
);
}
/**
*
* @return cache_memcache
*/
public function flush()
{
$this->memcache->flush();
return $this;
}
/**
*
* @return string
*/
public function get_version()
{
return $this->memcache->getVersion();
}
/**
*
* @return boolean
*/
public function ping()
{
return!!$this->online;
}
}

170
lib/classes/cache/memcached.class.php vendored Normal file
View File

@@ -0,0 +1,170 @@
<?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_memcached implements cache_interface
{
/**
*
* @var cache_interface
*/
protected $memcached;
/**
*
* @var boolean
*/
protected $online = false;
/**
*
* @param registryInterface $registry
* @return cache_memcached
*/
public function __construct(registryInterface $registry)
{
if (!extension_loaded('Memcached'))
{
throw new Exception('Memcached is not loaded');
}
$this->memcached = new Memcached();
$host = $registry->get('GV_cache_server_host');
$port = $registry->get('GV_cache_server_port');
/**
* We do not activate binary protocol because if some issues
*
* https://code.google.com/p/memcached/issues/detail?id=106
*
*/
// $this->memcached->setOption(Memcached::OPT_BINARY_PROTOCOL, true);
$this->memcached->setOption(Memcached::OPT_CONNECT_TIMEOUT, 500);
$this->memcached->setOption(Memcached::OPT_SEND_TIMEOUT, 500);
$this->memcached->setOption(Memcached::OPT_RECV_TIMEOUT, 500);
$this->memcached->setOption(Memcached::OPT_SERVER_FAILURE_LIMIT, 1);
$this->memcached->setOption(Memcached::OPT_DISTRIBUTION, Memcached::DISTRIBUTION_CONSISTENT);
// @$this->memcached->setOption(Memcached::OPT_SERIALIZER, Memcached::SERIALIZER_IGBINARY);
$this->memcached->addServer($host, $port);
$this->memcached->getVersion();
if ($this->memcached->getResultCode() !== Memcached::RES_SUCCESS)
throw new Exception('Unable to connect');
$this->online = true;
return $this;
}
/**
*
* @param string $key
* @param mixed $value
* @param int $expiration
* @return cache_memcached
*/
public function set($key, $value, $expiration)
{
$this->memcached->set($key, $value, $expiration);
return $this;
}
/**
*
* @param string $key
* @return mixed
*/
public function get($key)
{
$value = $this->memcached->get($key);
if ($this->memcached->getResultCode() !== Memcached::RES_SUCCESS)
{
throw new Exception('Unable to retrieve the value');
}
return $value;
}
/**
*
* @param string $key
* @return cache_memcached
*/
public function delete($key)
{
$this->memcached->delete($key);
return $this;
}
/**
*
* @param array $array_keys
* @return cache_memcached
*/
public function deleteMulti(Array $array_keys)
{
foreach ($array_keys as $key)
$this->memcached->delete($key);
return $this;
}
/**
*
* @return array
*/
public function getStats()
{
return $this->memcached->getStats();
}
/**
*
* @return cache_memcached
*/
public function flush()
{
$this->memcached->flush();
return $this;
}
/**
*
* @return string
*/
public function get_version()
{
return $this->memcached->getVersion();
}
/**
*
* @return boolean
*/
public function ping()
{
return $this->online;
}
}

138
lib/classes/cache/nocache.class.php vendored Normal file
View File

@@ -0,0 +1,138 @@
<?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_nocache implements cache_interface
{
/**
*
* @var cache_interface
*/
protected $cache;
/**
*
* @var array
*/
protected $datas = array();
/**
*
* @var boolean
*/
protected $online = false;
/**
*
* @param registryInterface $registry
* @return cache_nocache
*/
public function __construct(registryInterface $registry)
{
return $this;
}
/**
*
* @param string $key
* @param mixed $value
* @param int $expiration
* @return cache_nocache
*/
public function set($key, $value, $expiration)
{
$this->datas[$key] = $value;
return $this;
}
/**
*
* @param string $key
* @return mixed
*/
public function get($key)
{
if (!isset($this->datas[$key]))
throw new Exception('Unable to retrieve the value');
return $this->datas[$key];
}
/**
*
* @param string $key
* @return cache_nocache
*/
public function delete($key)
{
if (isset($this->datas[$key]))
unset($this->datas[$key]);
return $this;
}
/**
*
* @param array $array_keys
* @return cache_nocache
*/
public function deleteMulti(Array $array_keys)
{
foreach ($array_keys as $key)
$this->delete($key);
return $this;
}
/**
*
* @return array
*/
public function getStats()
{
return array();
}
/**
*
* @return cache_nocache
*/
public function flush()
{
$this->datas = array();
return $this;
}
/**
*
* @return string
*/
public function get_version()
{
return '';
}
/**
*
* @return boolean
*/
public function ping()
{
return true;
}
}

View 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;
}
}

View 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);
}

View File

@@ -1,54 +0,0 @@
<?php
class cache_preview
{
private static $_instance = false;
var $_c_obj = false;
function __construct()
{
$this->_c_obj = cache::getInstance();
}
/**
* @return cache_preview
*/
public static function getInstance()
{
if (!(self::$_instance instanceof self))
self::$_instance = new self();
return self::$_instance;
}
public function get($sbas_id,$record_id, $canPrev)
{
cache_databox::refresh($sbas_id);
return $this->_c_obj->get(GV_ServerName.'_preview_'.$sbas_id.'_'.$record_id.'_'.($canPrev ? '1':'0'));
}
public function set($sbas_id,$record_id, $canPrev, $value)
{
return $this->_c_obj->set(GV_ServerName.'_preview_'.$sbas_id.'_'.$record_id.'_'.($canPrev ? '1':'0'),$value);
}
public function delete($sbas_id,$record_id,$update_distant_boxes = true)
{
$this->_c_obj->delete(GV_ServerName.'_preview_'.$sbas_id.'_'.$record_id.'_0');
$this->_c_obj->delete(GV_ServerName.'_preview_'.$sbas_id.'_'.$record_id.'_1');
if($update_distant_boxes)
cache_databox::update($sbas_id,'record',$record_id);
return true;
}
}

View File

@@ -1,56 +0,0 @@
<?php
class cache_record
{
private static $_instance = false;
var $_c_obj = false;
function __construct()
{
$this->_c_obj = cache::getInstance();
}
/**
* @return cache_record
*/
public static function getInstance()
{
if (!(self::$_instance instanceof self))
self::$_instance = new self();
return self::$_instance;
}
public function get($sbas_id,$record_id,$type)
{
cache_databox::refresh($sbas_id);
return $this->_c_obj->get(GV_ServerName.'_'.$sbas_id.'_'.$type.'_'.$record_id);
}
public function set($sbas_id,$record_id,$type,$value)
{
return $this->_c_obj->set(GV_ServerName.'_'.$sbas_id.'_'.$type.'_'.$record_id,$value,14400);
}
/**
* Update Delayed Cache on distant databoxes for other clients
* @param1 $sbas_id : sbas_id of databox
* @param2 $record_id : record_id updated
*/
public function delete($sbas_id,$record_id,$update_distant_boxes = true)
{
$this->_c_obj->delete(GV_ServerName.'_'.$sbas_id.'_caption_'.$record_id);
$this->_c_obj->delete(GV_ServerName.'_'.$sbas_id.'_caption_bounce_'.$record_id);
$this->_c_obj->delete(GV_ServerName.'_'.$sbas_id.'_title_'.$record_id);
if($update_distant_boxes)
cache_databox::update($sbas_id,'record',$record_id);
return true;
}
}

151
lib/classes/cache/redis.class.php vendored Normal file
View File

@@ -0,0 +1,151 @@
<?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_redis implements cache_interface
{
/**
*
* @var Redis
*/
protected $redis;
protected $igbinary = true;
/**
*
* @param registryInterface $registry
* @return cache_redis
*/
public function __construct(registryInterface $registry)
{
if (!extension_loaded('Redis'))
{
throw new Exception('Redis is not loaded');
}
$this->redis = new Redis();
$this->redis->connect($registry->get('GV_cache_server_host'), $registry->get('GV_cache_server_port'));
if (!$this->redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_IGBINARY))
{
$this->igbinary = false;
$this->redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP);
}
return $this;
}
/**
*
* @param string $key
* @param mixed $value
* @param int $expiration
* @return cache_redis
*/
public function set($key, $value, $expiration)
{
if ($expiration != 0)
$this->redis->setex($key, $expiration, $value);
else
$this->redis->set($key, $value);
return $this;
}
/**
*
* @param string $key
* @return mixed
*/
public function get($key)
{
$tmp = $this->redis->get($key);
if ($tmp === false && $this->redis->exists($key) === false)
throw new Exception('Unable to retrieve the value ' . $key);
return $tmp;
}
/**
*
* @param string $key
* @return cache_redis
*/
public function delete($key)
{
$this->redis->delete($key);
return $this;
}
/**
*
* @param array $array_keys
* @return cache_redis
*/
public function deleteMulti(Array $array_keys)
{
$this->redis->delete($array_keys);
return $this;
}
/**
*
* @return array
*/
public function getStats()
{
return array('Redis Server ('.($this->igbinary ? 'YES':'NO').' Igbinary)' => $this->redis->info());
}
/**
*
* @return cache_redis
*/
public function flush()
{
$this->redis->flushAll();
return $this;
}
/**
*
* @return string
*/
public function get_version()
{
$infos = $this->getStats();
if (isset($infos['redis_version']))
return $infos['redis_version'];
return false;
}
/**
*
* @return boolean
*/
public function ping()
{
return ($this->redis->ping() === '+PONG');
}
}

View File

@@ -1,54 +0,0 @@
<?php
class cache_status
{
private static $_instance = false;
var $_c_obj = false;
function __construct()
{
$this->_c_obj = cache::getInstance();
}
/**
* @return cache_status
*/
public static function getInstance()
{
if (!(self::$_instance instanceof self))
self::$_instance = new self();
return self::$_instance;
}
public function get($usr_id)
{
return $this->_c_obj->get(GV_ServerName.'_status_'.$usr_id);
}
public function set($usr_id,$value)
{
$statuspool = array();
if(($tmp = $this->_c_obj->get(GV_ServerName.'_statuspool_')) != false)
$statuspool = array();
$statuspool[] = $usr_id;
if($this->_c_obj->set(GV_ServerName.'_statuspool_',$statuspool))
return $this->_c_obj->set(GV_ServerName.'_status_'.$usr_id,$value);
return false;
}
public function delete($usr_id)
{
return $this->_c_obj->delete(GV_ServerName.'_status_'.$usr_id);
}
}

View File

@@ -1,54 +0,0 @@
<?php
class cache_thumbnail
{
private static $_instance = false;
private $_c_obj = false;
function __construct()
{
$this->_c_obj = cache::getInstance();
}
/**
* @return cache_thumbnail
*/
public static function getInstance()
{
if (!(self::$_instance instanceof self))
self::$_instance = new self();
return self::$_instance;
}
public function get($sbas_id, $rid, $getPrev)
{
cache_databox::refresh($sbas_id);
return $this->_c_obj->get(GV_ServerName.'_thumbnail_'.$sbas_id.'_'.$rid.'_'.($getPrev ? '1':'0'));
}
public function set($sbas_id, $rid, $getPrev,$value)
{
return $this->_c_obj->set(GV_ServerName.'_thumbnail_'.$sbas_id.'_'.$rid.'_'.($getPrev ? '1':'0'),$value);
}
public function delete($sbas_id, $rid,$update_distant_boxes = true)
{
$this->_c_obj->delete(GV_ServerName.'_thumbnail_'.$sbas_id.'_'.$rid.'_1');
$this->_c_obj->delete(GV_ServerName.'_thumbnail_'.$sbas_id.'_'.$rid.'_0');
if($update_distant_boxes)
cache_databox::update($sbas_id,'record',$rid);
return true;
}
}

View File

@@ -1,49 +0,0 @@
<?php
class cache_user
{
private static $_instance = false;
var $_c_obj = false;
function __construct()
{
$this->_c_obj = cache::getInstance();
}
/**
* @return cache_user
*/
public static function getInstance()
{
if (!(self::$_instance instanceof self))
self::$_instance = new self();
return self::$_instance;
}
public function get($id)
{
return $this->_c_obj->get(GV_ServerName.'_user_'.$id);
}
public function set($id,$value)
{
return $this->_c_obj->set(GV_ServerName.'_user_'.$id,$value);
}
public function delete($id)
{
$cache_basket = cache_basket::getInstance();
$cache_basket->revoke_baskets_usr($id);
return $this->_c_obj->delete(GV_ServerName.'_user_'.$id);
}
}