KodExplorer/lib/class/fileCache.class.php

202 lines
5.4 KiB
PHP
Raw Normal View History

2015-03-22 20:54:54 +00:00
<?php
/*
* @link http://www.kalcaddle.com/
* @author warlee | e-mail:kalcaddle@qq.com
* @copyright warlee 2014.(Shanghai)Co.,Ltd
* @license http://kalcaddle.com/tools/licenses/license.txt
*/
/**
* 数据的缓存存储类key=>value 模式value可以是任意类型数据。
* 完整流程测试读取最低5000次/s 含有写的1000次/s
2016-12-21 08:01:06 +00:00
* get($find=null)
* 1.get(); //返回所有
* 2.get(key); //直接通过key获取
* 3.get(data_key,value); //搜索key为value的数据 直接返回数据不含key
* 4.get(array('key','value')); //搜索数据符合key为指定value的所有数据;key value形式
*
* set($find=null,$change=null)
* 1.set(string,val) //添加或更新;
* 2.set(array('key','value_find'),array('key','change_to')) //查找方式更新 多条数据
2015-03-22 20:54:54 +00:00
*
2016-12-21 08:01:06 +00:00
* remove($find,$value)
* 1.remove(); //清空
* 2.remove(string); //删除 eg:set('37'),删除key为37的数据 存在且删除成功则返回true
* 3.remove(array('key','value_find')); //查找方式删除;多条数据
* reset($arr);//初始化数据
2015-03-22 20:54:54 +00:00
*/
2016-12-21 08:01:06 +00:00
2015-03-22 20:54:54 +00:00
define('CONFIG_EXIT', '<?php exit;?>');
2016-12-21 08:01:06 +00:00
class fileCache{
private $data;
private $file;
private $file_hash;//最后一次修改;保存时判断,如果有新修改则先读取再保存
function __construct($file) {
$this->file = $file;
$this->data= self::load($file);
$this->file_change_check();
}
public function get($find=null,$value=null){
if (is_null($find)){
return $this->data;
}else if(is_array($find)){//查找内容数据方式获取;返回多条
$result = array();
foreach ($this->data as $key => $val) {
if ($val[$find[0]] == $find[1]) {
$result[$key] = $this->data[$key];
}
}
if(count($result)!=0){
return $result;
}
}else{//单条数据获取
$find .= '';//字符串
if(!is_null($value)){//通过某个key寻找单条数据
foreach ($this->data as $key => $val) {
if ($val[$find] == $value) {
return $val;
}
}
}
if(isset($this->data[$find])){
return $this->data[$find];
}
}
return false;
}
//添加或更新
public function set($find,$value){
$this->file_change_check();
//最后有修改则先更新本地。
if(is_string($find)){//单条数据更新
$this->data[$find] = $value;
}else if(is_array($find)){//查找方式更新;更新多条
foreach ($this->data as $key => $val) {
if ($val[$find[0]] == $find[1]) {
$this->data[$key][$value[0]] = $value[1];
}
}
}else{
return false;
}
self::save($this->file,$this->data);
return true;
}
//删除,查找删除
public function remove($find){
$this->file_change_check();
if(is_string($find)){//单条数据删除
unset($this->data[$find]);
}else if(is_array($find)){//查找删除
foreach ($this->data as $key => $val) {
if ($val[$find[0]] == $find[1]){
unset($this->data[$key]);
}
}
}else{
return false;
}
self::save($this->file,$this->data);
return true;
}
private function file_change_check(){
if(is_null($this->file_hash)){
$this->file_hash = @md5_file($this->file);
return;
}
//是否发生改变
$last_hash = @md5_file($this->file);
if($last_hash != $this->file_hash){
$this->data= self::load($this->file);
}
}
public function reset($data,$save = true){
$this->data = $data;
if($save){
self::save($this->file,$this->data);
}
}
2015-03-22 20:54:54 +00:00
2016-12-21 08:01:06 +00:00
2015-03-22 20:54:54 +00:00
2016-12-21 08:01:06 +00:00
//=====================================================
public static function arr_sort(&$arr,$key, $type = 'asc'){
$keysvalue = $new_array = array();
foreach ($arr as $k => $v) {
$keysvalue[$k] = $v[$key];
}
if ($type == 'asc') {
asort($keysvalue);
} else {
arsort($keysvalue);
}
reset($keysvalue);
foreach ($keysvalue as $k => $v) {
$new_array[$k] = $arr[$k];
}
return $new_array;
}
2015-03-22 20:54:54 +00:00
2016-12-21 08:01:06 +00:00
public function get_max_id(){
$min_id = 100;
if(count($this->data)==0){
return $min_id;//一切从100开始
}
$id_arr = array_keys($this->data);
rsort($id_arr,SORT_NUMERIC);//id从高到底
$the_id = intval($id_arr[0])+1;
if($the_id<=$min_id){
return $min_id;
}
return $the_id;
}
2015-03-22 20:54:54 +00:00
2016-12-21 08:01:06 +00:00
/**
* 加载数据;并解析成程序数据
*/
public static function load($file){//10000次需要4s 数据量差异不大。
if (!file_exists($file)) touch($file);
$str = file_get_contents($file);
2015-03-22 20:54:54 +00:00
2016-12-21 08:01:06 +00:00
$str = substr($str, strlen(CONFIG_EXIT));
$data= json_decode($str,true);
if (is_null($data)) $data = array();
return $data;
}
/**
* 保存数据;
*/
public static function save($file,$data){//10000次需要6s
if (!$file) return;
if (!file_exists($file)){
@touch($file);
}
2015-03-22 20:54:54 +00:00
2016-12-21 08:01:06 +00:00
chmod_path($file,0777);
if (!path_writeable($file)) {
show_json('the path "data/" can not write!',false);
}
$json_str = json_encode($data);
if(is_null($json_str)){//含有二进制或非utf8字符串对应检测
show_json('json_encode error!',false);
}
2015-03-22 20:54:54 +00:00
2016-12-21 08:01:06 +00:00
if($fp = fopen($file, "w")){
if (flock($fp, LOCK_EX)) { // 进行排它型锁定
$str = CONFIG_EXIT.$json_str;
fwrite($fp, $str);
fflush($fp); // flush output before releasing the lock
flock($fp, LOCK_UN); // 释放锁定
}
fclose($fp);
}
}
}