PHP共享内存yac操作类
<?php
/**
* 进程间共享内存操作类
*/
class Pshmop
{
protected static $_models = array();
private $_yac = null;
private static $_keyPrefix = 'shm_';
private static $_ttlMaxTime = 7776000; //86400*90 为防止永久贮存及保存时间过久造成内存消耗严重导致数据被踢出
/**
* Returns the static model of the specified AR class.
* @param string $className active record class name.
* @return Order the static model class
*/
public static function model($className = __CLASS__)
{
$model = null;
if (isset(self::$_models[$className]))
$model = self::$_models[$className];
else {
$model = self::$_models[$className] = new $className(null);
}
return $model;
}
public function __construct() {
if(extension_loaded("yac")){
$this->_yac = new Yac(self::$_keyPrefix);
}
}
/**
* add value
* @param mixed $keys
* @param mixed $value
* @param int $ttl
* @return mixed
*/
public function add($key, $value, $ttl=-1){
if(empty($key)){
return null;
}
if(empty($this->_yac)){
return null;
}
if($ttl<0 || $ttl>self::$_ttlMaxTime){
$ttl = self::$_ttlMaxTime;
}
return $this->_yac->add($key, $value, $ttl);
}
/**
* set value
* @param mixed $keys
* @param mixed $value
* @param int $ttl
* @return mixed
*/
public function set($key, $value, $ttl=-1){
if(empty($key)){
return null;
}
if(empty($this->_yac)){
return null;
}
if($ttl<0 || $ttl>self::$_ttlMaxTime){
$ttl = self::$_ttlMaxTime;
}
return $this->_yac->set($key, $value, $ttl);
}
/**
* get value
* @param mixed $keys
* @return mixed
*/
public function get($key){
if(empty($key)){
return null;
}
if(empty($this->_yac)){
return null;
}
return $this->_yac->get($key);
}
/**
* delete key
* @param mixed $keys
* @param int $delay
* @return mixed
*/
public function delete($key, $delay=0){
if(empty($key)){
return null;
}
if(empty($this->_yac)){
return null;
}
return $this->_yac->delete($key, $delay);
}
/**
* flush shm
* @param void
* @return mixed
*/
public function flush(){
if(empty($this->_yac)){
return null;
}
return $this->_yac->flush();
}
/**
* get shm info
* @param void
* @return mixed
*/
public function info(){
if(empty($this->_yac)){
return null;
}
return $this->_yac->info();
}
}