mirror of
https://gitee.com/ShopeX/OMS
synced 2026-03-23 02:45:33 +08:00
142 lines
3.7 KiB
PHP
Executable File
142 lines
3.7 KiB
PHP
Executable File
<?php
|
|
/**
|
|
* Copyright 2012-2026 ShopeX (https://www.shopex.cn)
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
/**
|
|
* 导出数据存储本地文件类
|
|
*
|
|
* @author kamisama.xia@gmail.com
|
|
* @version 0.1
|
|
*/
|
|
class taskmgr_cache_filesystem extends taskmgr_cache_abstract implements taskmgr_cache_interface{
|
|
|
|
static private $_cacheObj = null;
|
|
|
|
private $_header = '<?php exit();?>';
|
|
|
|
private $_path = 0;
|
|
|
|
private $_header_length = 0;
|
|
|
|
function __construct($path)
|
|
{
|
|
$this->_path = is_object($path) ? 'object' : $path;
|
|
$this->_header_length = strlen($this->_header);
|
|
}
|
|
|
|
/**
|
|
* fetch
|
|
* @param mixed $key key
|
|
* @param mixed $result result
|
|
* @return mixed 返回值
|
|
*/
|
|
|
|
public function fetch($key, &$result)
|
|
{
|
|
$file = $this->get_save_file($key);
|
|
if(file_exists($file)){
|
|
$data = unserialize(substr(file_get_contents($file),$this->_header_length));
|
|
if($data['ttl'] > 0 && ($data['dateline']+$data['ttl']) < time()){
|
|
return false;
|
|
}
|
|
$result = $data['value'];
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* store
|
|
* @param mixed $key key
|
|
* @param mixed $value value
|
|
* @param mixed $ttl ttl
|
|
* @return mixed 返回值
|
|
*/
|
|
public function store($key, $value, $ttl=0)
|
|
{
|
|
$this->check_dir();
|
|
$data = array();
|
|
$data['value'] = $value;
|
|
$data['ttl'] = $ttl;
|
|
$data['dateline'] = time();
|
|
$org_file = $this->get_save_file($key);
|
|
$tmp_file = $org_file . '.' . str_replace(' ', '.', microtime()) . '.' . mt_rand();
|
|
if(file_put_contents($tmp_file, $this->_header.serialize($data))){
|
|
if(copy($tmp_file, $org_file)){
|
|
@chmod($org_file,0666);
|
|
@unlink($tmp_file);
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* 删除
|
|
* @param mixed $key key
|
|
* @return mixed 返回值
|
|
*/
|
|
public function delete($key)
|
|
{
|
|
$file = $this->get_save_file($key);
|
|
if(file_exists($file)){
|
|
return @unlink($file);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* increment
|
|
* @param mixed $key key
|
|
* @param mixed $value value
|
|
* @return mixed 返回值
|
|
*/
|
|
public function increment($key, $value=1)
|
|
{
|
|
$file = $this->get_save_file($key);
|
|
|
|
$rhandle = fopen($file,'rb+');
|
|
flock($rhandle, LOCK_EX);
|
|
|
|
$old_cnts = fread($rhandle, filesize($file));
|
|
$data = unserialize(substr($old_cnts,$this->_header_length));
|
|
if($data){
|
|
$data['value'] = $data['value'] + $value;
|
|
|
|
ftruncate($rhandle, 0);
|
|
rewind($rhandle);
|
|
fwrite($rhandle, $this->_header.serialize($data));
|
|
}
|
|
|
|
flock($rhandle, LOCK_UN);
|
|
fclose($rhandle);
|
|
|
|
return true;
|
|
}
|
|
|
|
private function check_dir()
|
|
{
|
|
if(!is_dir(DATA_DIR.'/export/cache/'.$this->_path)){
|
|
utils::mkdir_p(DATA_DIR.'/export/cache/'.$this->_path);
|
|
}
|
|
}
|
|
|
|
private function get_save_file($key)
|
|
{
|
|
$key = $this->create_key($key);
|
|
return DATA_DIR.'/export/cache/'.$this->_path.'/'.$key.'.php';
|
|
}
|
|
}
|