DiscuzX/upload/source/class/memory/memory_driver_file.php

110 lines
2.3 KiB
PHP
Raw Normal View History

2017-02-13 21:14:27 -05:00
<?php
/**
* [Discuz!] (C)2001-2099 Comsenz Inc.
* This is NOT a freeware, use is subject to license terms
*
* $Id: memory_driver_yac.php 27635 2017-02-02 17:02:46Z NaiXiaoxIN $
*/
if (!defined('IN_DISCUZ')) {
exit('Access Denied');
}
class memory_driver_file {
public $cacheName = 'File';
public $enable;
public $path;
public function env() {
return true;
}
public function init($config) {
$this->path = $config['server'].'/';
if($config['server']) {
$this->enable = is_dir(DISCUZ_ROOT.$this->path);
if(!$this->enable) {
dmkdir(DISCUZ_ROOT.$this->path);
$this->enable = is_dir(DISCUZ_ROOT.$this->path);
}
} else {
$this->enable = false;
}
}
2017-02-13 22:35:09 -05:00
2017-02-13 21:14:27 -05:00
private function cachefile($key) {
return str_replace('_', '/', $key).'/'.$key;
}
2017-02-13 22:35:09 -05:00
2017-02-13 21:25:10 -05:00
public function get($key) {
2017-02-13 21:14:27 -05:00
$file = DISCUZ_ROOT.$this->path.$this->cachefile($key).'.php';
2017-02-13 22:35:09 -05:00
if(!file_exists($file)) {
2017-02-13 21:14:27 -05:00
return false;
}
$data = null;
2017-02-13 21:25:10 -05:00
@include $file;
2017-02-13 21:14:27 -05:00
if($data !== null) {
2017-02-13 22:35:09 -05:00
if($data['exp'] && $data['exp'] < TIMESTAMP) {
2017-02-13 21:14:27 -05:00
return false;
} else {
return $data['data'];
}
} else {
return false;
}
}
public function set($key, $value, $ttl = 0) {
$file = DISCUZ_ROOT.$this->path.$this->cachefile($key).'.php';
dmkdir(dirname($file));
$data = array(
'exp' => $ttl ? TIMESTAMP + $ttl : 0,
'data' => $value,
);
file_put_contents($file, "<?php\n\$data = ".var_export($data, 1).";\n");
return true;
}
public function rm($key) {
2017-06-03 05:46:39 -04:00
return @unlink(DISCUZ_ROOT.$this->path.$this->cachefile($key).'.php');
2017-02-13 21:14:27 -05:00
}
2017-02-13 22:35:09 -05:00
private function dir_clear($dir) {
2017-02-13 21:14:27 -05:00
if($directory = @dir($dir)) {
while($entry = $directory->read()) {
$filename = $dir.'/'.$entry;
if($entry != '.' && $entry != '..') {
if(is_file($filename)) {
@unlink($filename);
} else {
$this->dir_clear($filename);
@rmdir($filename);
}
}
}
2017-02-13 22:35:09 -05:00
$directory->close();
2017-02-13 21:14:27 -05:00
}
}
public function clear() {
return $this->dir_clear(DISCUZ_ROOT.$this->path);
}
public function inc($key, $step = 1) {
$old = $this->get($key);
if (!$old) {
return false;
}
return $this->set($key, $old + $step);
}
public function dec($key, $step = 1) {
$old = $this->get($key);
if (!$old) {
return false;
}
return $this->set($key, $old - $step);
}
2017-02-13 22:35:09 -05:00
}