public function __construct($file_index="filecache_index.dat", $file_data="filecache_data.dat"){
'ref_count'=>array(1,'V'),//Quote here, imitating PHP's reference count destruction mode
$this->_file_index = fopen($this->_file_index_name, "rb+");
$this->_file_index = fopen($this->_file_index_name, "wb+");//Binary storage requires the use of b
throw new fileCacheException("Could't open index file:".$this->_file_index_name);
$this->_index_puts(0, '<'.'?php exit()?'.'>');//Locate the file flow to the starting position 0, and place a php tag to prevent downloading
$this->_index_puts($this->_file_header_size, pack("V1", 0));
}
//Create storage file
private function _create_data(){
$this->_file_data = fopen($this->_file_data_name, "wb+");//Binary storage requires the use of b
if(!$this->_file_index)
throw new fileCacheException("Could't open index file:".$this->_file_data_name);
$this->_data_puts(0, '<'.'?php exit()?'.'>');//Locate the file flow to the starting position 0, and place a php tag to prevent downloading
}
private function _index_puts($offset, $data, $length=false){
fseek($this->_file_index, $offset);
if($length)
fputs($this->_file_index, $data, $length);
else else
fputs($this->_file_index, $data);
}
private function _data_puts($offset, $data, $length=false){
fseek($this->_file_data, $offset);
if($length)
fputs($this->_file_data, $data, $length);
else else
fputs($this->_file_data, $data);
}
/**
* File lock
* @param $is_block Whether it is an exclusive or blocking lock
*/
private function _lock($file_res, $is_block=true){
flock($file_res, $is_block ? LOCK_EX : LOCK_EX|LOCK_NB);
}
private function _unlock($file_res){
flock($file_res, LOCK_UN);
}
public function add($key, $value){
$key = md5($key);
$value = serialize($value);
$this->_lock($this->_file_index, true);
$this->_lock($this->_file_data, true);
fseek($this->_file_index, $this->_file_header_size);
list(, $index_count) = unpack('V1', fread($this->_file_index, 4));
$data_size = filesize($this->_file_data_name);
fseek($this->_file_data, $data_size);
$value_size = strlen($value);
$this->_data_puts(filesize($this->_file_data_name), $value);
$node_data =
pack("V1V1V1V1V1H32", ($index_count==0) ? 0 : $index_count*$this->_inx_node_size, 0, filesize($this->_file_data_name), strlen($value), 0, $ key);
$index_count++;
$this->_index_puts($this->_file_header_size, $index_count, 4);
$this->_index_puts($this->get_new_node_pos($index_count), $node_data);
$this->_unlock($this->_file_data);
$this->_unlock($this->_file_index);
}
public function get_new_node_pos($index_count){
return $this->_file_header_size + 4 + $this->_inx_node_size * ($index_count-1);
}
public function get_node($key){
$key = md5($key);
fseek($this->_file_index, $this->_file_header_size);
$index_count = fread($this->_file_index, 4);
if($index_count>0) {
for ($i=0; $i < $index_count ; $i++) {
fseek($this->_file_index, $this->_file_header_size + 4 + $this->_inx_node_size * $i);
$data = fread($this->_file_index, $this->_inx_node_size);
$node = unpack("V1next/V1prev/V1data_offset/V1data_size/V1ref_count/H32key", $data);
if($key == $node['key']){
return $node;
}
}
}else{
return null;
}
}
public function get_data($offset, $length){
fseek($this->_file_data, $offset);
return unserialize(fread($this->_file_data, $length));
}
}
//使用方法
$cache = new fileCache();
$cache->add('abcdefg' , 'testabc');
$data = $cache->get_node('abcdefg');
print_r($data);
echo $cache->get_data($data['data_offset'], $data['data_size']);
error_reporting(E_ALL);
class fileCacheException extends Exception{
}
//Key-Value型文件存储
class fileCache{
private $_file_header_size = 14;
private $_file_index_name;
private $_file_data_name;
private $_file_index;//索引文件句柄
private $_file_data;//数据文件句柄
private $_node_struct;//索引结点结构体
private $_inx_node_size = 36;//索引结点大小
public function __construct($file_index="filecache_index.dat", $file_data="filecache_data.dat"){
$this->_node_struct = array(
'next'=>array(1, 'V'),
'prev'=>array(1, 'V'),
'data_offset'=>array(1,'V'),//数据存储起始位置
'data_size'=>array(1,'V'),//数据长度
'ref_count' = & gt; array (1, 'v'), // Quote here, imitate the reference counting of PHP, the destruction mode
'key'=>array(16,'H*'),//Storage KEY
);
$this->_file_index_name = $file_index;
$this->_file_data_name = $file_data;
if(!file_exists($this->_file_index_name)){
$this->_create_index();
}else{
$this->_file_index = fopen($this->_file_index_name, "rb+");
}
if(!file_exists($this->_file_data_name)){
$this->_create_data();
}else{
$this->_file_data = fopen($this->_file_data_name, "rb+");//Binary storage needs to use b
}
}
//Create index file
private function _create_index(){
$this->_file_index = fopen($this->_file_index_name, "wb+");//Binary storage needs to use b
if(!$this->_file_index)
throw new fileCacheException("Could't open index file:".$this->_file_index_name);
$this->_index_puts(0, '<'.'?php exit()?'.'>');//Locate the file flow to the starting position 0, and place a php tag to prevent downloading
$this->_index_puts($this->_file_header_size, pack("V1", 0));
}
//Create storage file
private function _create_data(){
$this->_file_data = fopen($this->_file_data_name, "wb+");//Binary storage needs to use b
if(!$this->_file_index)
throw new fileCacheException("Could't open index file:".$this->_file_data_name);
$this->_data_puts(0, '<'.'?php exit()?'.'>');//Locate the file flow to the starting position 0, and place a php tag to prevent downloading
}
private function _index_puts($offset, $data, $length=false){
fseek($this->_file_index, $offset);
if($length)
fputs($this->_file_index, $data, $length);
else
fputs($this->_file_index, $data);
}
private function _data_puts($offset, $data, $length=false){
fseek($this->_file_data, $offset);
if($length)
fputs($this->_file_data, $data, $length);
else
fputs($this->_file_data, $data);
}
/**
* File Lock
* @param $is_block Whether it is an exclusive or blocking lock
*/
private function _lock($file_res, $is_block=true){
flock($file_res, $is_block ? LOCK_EX : LOCK_EX|LOCK_NB);
}
private function _unlock($file_res){
flock($file_res, LOCK_UN);
}
public function add($key, $value){
$key = md5($key);
$value = serialize($value);
$this->_lock($this->_file_index, true);
$this->_lock($this->_file_data, true);
fseek($this->_file_index, $this->_file_header_size);
list(, $index_count) = unpack('V1', fread($this->_file_index, 4));
$data_size = filesize($this->_file_data_name);
fseek($this->_file_data, $data_size);
$value_size = strlen($value);
$this->_data_puts(filesize($this->_file_data_name), $value);
$node_data =
pack("V1V1V1V1V1H32", ($index_count==0) ? 0 : $index_count*$this->_inx_node_size, 0, filesize($this->_file_data_name), strlen($value), 0, $key);
$index_count++;
$this->_index_puts($this->_file_header_size, $index_count, 4);
$this->_index_puts($this->get_new_node_pos($index_count), $node_data);
$this->_unlock($this->_file_data);
$this->_unlock($this->_file_index);
}
public function get_new_node_pos($index_count){
return $this->_file_header_size + 4 + $this->_inx_node_size * ($index_count-1);
}
public function get_node($key){
$key = md5($key);
fseek($this->_file_index, $this->_file_header_size);
$index_count = fread($this->_file_index, 4);
if($index_count>0) {
for ($i=0; $i < $index_count ; $i++) {
fseek($this->_file_index, $this->_file_header_size + 4 + $this->_inx_node_size * $i);
$data = fread($this->_file_index, $this->_inx_node_size);
$node = unpack("V1next/V1prev/V1data_offset/V1data_size/V1ref_count/H32key", $data);
if($key == $node['key']){
return $node;
}
}
}else{
return null;
}
}
public function get_data($offset, $length){
fseek($this->_file_data, $offset);
return unserialize(fread($this->_file_data, $length));
}
}
//使用方法
$cache = new fileCache();
$cache->add('abcdefg' , 'testabc');
$data = $cache->get_node('abcdefg');
print_r($data);
echo $cache->get_data($data['data_offset'], $data['data_size']);
案例四、socket通信加密
通信双方都定义好加密格式:
例如:
[php]
LOGIN = array(
'COMMAND'=>array('a30', 'LOGIN'),
'DATA'=>array('a30', 'HELLO')
);
$LOGOUT = array(
'COMMAND'=>array('a30', 'LOGOUT'),
'DATA'=>array('a30', 'GOOD BYE')
);
$LOGIN_SUCCESS = array(
'COMMAND'=>array('a30', 'LOGIN_SUCCESS'),
'DATA'=>array('V1', 1)
);
$LOGOUT_SUCCESS = array(
'COMMAND'=>array('a30', 'LOGIN_SUCCESS'),
'DATA'=>array('V1', time())
);
$LOGIN = array(
'COMMAND'=>array('a30', 'LOGIN'),
'DATA'=>array('a30', 'HELLO')
);
$LOGOUT = array(
'COMMAND'=>array('a30', 'LOGOUT'),
'DATA'=>array('a30', 'GOOD BYE')
);
$LOGIN_SUCCESS = array(
'COMMAND'=>array('a30', 'LOGIN_SUCCESS'),
'DATA'=>array('V1', 1)
);
$LOGOUT_SUCCESS = array(
'COMMAND'=>array('a30', 'LOGIN_SUCCESS'),
'DATA'=>array('V1', time())
);服务器端与客户端根据解析COMMAND格式,找到对应的DATA解码方式,得到正确的数据
http://www.bkjia.com/PHPjc/477666.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/477666.htmlTechArticle网络通信、文件存储中经常需要交换数据,为了减少网络通信流量、文件存储大小以及加密通信规则,经常需要对数据进行双向加解密以保...