【php开源代码栏目提醒】:网学会员,鉴于大家对php开源代码十分关注,论文会员在此为大家搜集整理了“CacheShmop.class.php”一文,供大家参考学习!
<?php
// +----------------------------------------------------------------------
// | ThinkPHP
// +----------------------------------------------------------------------
// | Copyright (c) 2008 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <
CacheShmop.class.php为[网学网-网友上传,谢谢支持]。>
// +----------------------------------------------------------------------
// $Id$
/**
+------------------------------------------------------------------------------
* Shmop缂撳瓨绫?
+------------------------------------------------------------------------------
* @category Think
* @package Think
* @subpackage Util
* @author liu21st <
CacheShmop.class.php为[网学网-网友上传,谢谢支持]。>
* @version $Id$
+------------------------------------------------------------------------------
*/
class CacheShmop extends Cache
{//绫诲畾涔夊紑濮?
/**
+----------------------------------------------------------
* 鏋舵瀯鍑芥暟
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
*/
public function __construct($options='')
{
if ( !extension_loaded('shmop') ) {
throw_exception(L('绯荤粺涓嶆敮鎸乻hmop'));
}
if(!empty($options)){
$options = array(
'size' => C('SHARE_MEM_SIZE'),
'tmp' => TEMP_PATH,
'project' => 's'
);
}
$this->options = $options;
$this->handler = $this->_ftok($this->options['project']);
$this->type = strtoupper(substr(__CLASS__,6));
}
/**
+----------------------------------------------------------
* 璇诲彇缂撳瓨
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @param string $name 缂撳瓨鍙橀噺鍚?
+----------------------------------------------------------
* @return mixed
+----------------------------------------------------------
*/
public function get($name = false)
{
$this->Q(1);
$id = shmop_open($this->handler, 'c', 0600, 0);
if ($id !== false) {
$ret = unserialize(shmop_read($id, 0, shmop_size($id)));
shmop_close($id);
if ($name === false) {
return $ret;
}
if(isset($ret[$name])) {
$content = $ret[$name];
if(C('DATA_CACHE_COMPRESS') && function_exists('gzcompress')) {
//鍚敤鏁版嵁鍘嬬缉
$content = gzuncompress($content);
}
return $content;
}else {
return null;
}
}else {
return false;
}
}
/**
+----------------------------------------------------------
* 鍐欏叆缂撳瓨
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @param string $name 缂撳瓨鍙橀噺鍚?
* @param mixed $value 瀛樺偍鏁版嵁
+----------------------------------------------------------
* @return boolen
+----------------------------------------------------------
*/
public function set($name, $value)
{
$this->W(1);
$lh = $this->_lock();
$val = $this->get();
if (!is_array($val)) {
$val = array();
}
if( C('DATA_CACHE_COMPRESS') && function_exists('gzcompress')) {
//鏁版嵁鍘嬬缉
$value = gzcompress($value,3);
}
$val[$name] = $value;
$val = serialize($val);
return $this->_write($val, $lh);
}
/**
+----------------------------------------------------------
* 鍒犻櫎缂撳瓨
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @param string $name 缂撳瓨鍙橀噺鍚?
+----------------------------------------------------------
* @return boolen
+----------------------------------------------------------
*/
public function rm($name)
{
$lh = $this->_lock();
$val = $this->get();
if (!is_array($val)) {
$val = array();
}
unset($val[$name]);
$val = serialize($val);
return $this->_write($val, $lh);
}
/**
+----------------------------------------------------------
* 鐢熸垚IPC key
+----------------------------------------------------------
* @access private
+----------------------------------------------------------
* @param string $project 椤圭洰鏍囪瘑鍚?
+----------------------------------------------------------
* @return integer
+----------------------------------------------------------
*/
private function _ftok($project)
{
if (function_exists('ftok')) {
return ftok(__FILE__, $project);
}
if(strtoupper(PHP_OS) == 'WINNT'){
$s = stat(__FILE__);
return sprintf("%u", (($s['ino'] & 0xffff) | (($s['dev'] & 0xff) << 16) |
(($project & 0xff) << 24)));
}else {
$filename = __FILE__ . (string) $project;
for($key = array(); sizeof($key) < strlen($filename); $key[] = ord(substr($filename, sizeof($key), 1)));
return dechex(array_sum($key));
}
}
/**
+----------------------------------------------------------
* 鍐欏叆鎿嶄綔
+----------------------------------------------------------
* @access private
+----------------------------------------------------------
* @param string $name 缂撳瓨鍙橀噺鍚?
+----------------------------------------------------------
* @return integer|boolen
+----------------------------------------------------------
*/
private function _write(&$val, &$lh)
{
$id = shmop_open($this->handler, 'c', 0600, $this->options['size']);
if ($id) {
$ret = shmop_write($id, $val, 0) == strlen($val);
shmop_close($id);
$this->_unlock($lh);
return $ret;
}
$this->_unlock($lh);
return false;
}
/**
+----------------------------------------------------------
* 鍏变韩閿佸畾
+----------------------------------------------------------
* @access private
+----------------------------------------------------------
* @param string $name 缂撳瓨鍙橀噺鍚?
+----------------------------------------------------------
* @return boolen
+----------------------------------------------------------
*/
private function &_lock()
{
if (function_exists('sem_get')) {
$fp = sem_get($this->handler, 1, 0600, 1);
sem_acquire ($fp);
} else {
$fp = fopen($this->options['tmp'].$this->prefix.md5($this->handler), 'w');
flock($fp, LOCK_EX);
}
return $fp;
}
/**
+----------------------------------------------------------
* 瑙i櫎鍏变韩閿佸畾
+----------------------------------------------------------
* @access private
+------------------------------------------------------
上一篇:
caches.php
下一篇:
哈弗F7 VS名爵HS,谁更懂得年轻人的心?