Skip to content

Commit

Permalink
改进缓存驱动
Browse files Browse the repository at this point in the history
  • Loading branch information
liu21st committed Aug 30, 2017
1 parent 64ccd7e commit a217d88
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 13 deletions.
8 changes: 4 additions & 4 deletions library/think/cache/Driver.php
Expand Up @@ -120,22 +120,22 @@ public function pull($name)
public function remember($name, $value, $expire = null)
{
if (!$this->has($name)) {
while ($this->has($name . '.lock')) {
while ($this->has($name . '_lock')) {
// 存在锁定则等待
}

try {
// 锁定
$this->set($name . '.lock', true);
$this->set($name . '_lock', true);
if ($value instanceof \Closure) {
$value = call_user_func($value);
}
$this->set($name, $value, $expire);
// 解锁
$this->rm($name . '.lock');
$this->rm($name . '_lock');
} catch (\Exception $e) {
// 解锁
$this->rm($name . '.lock');
$this->rm($name . '_lock');
}
} else {
$value = $this->get($name);
Expand Down
12 changes: 3 additions & 9 deletions library/think/cache/driver/File.php
Expand Up @@ -109,12 +109,10 @@ public function get($name, $default = false)
$content = file_get_contents($filename);
if (false !== $content) {
$expire = (int) substr($content, 8, 12);
if (0 != $expire && $_SERVER['REQUEST_TIME'] > filemtime($filename) + $expire && !is_file($filename . '.lock')) {
// 生成过期锁定文件
touch($filename . '.lock');
if (0 != $expire && $_SERVER['REQUEST_TIME'] > filemtime($filename) + $expire) {
return $default;
}
$content = substr($content, 20, -3);
$content = substr($content, 32);
if ($this->options['data_compress'] && function_exists('gzcompress')) {
//启用数据压缩
$content = gzuncompress($content);
Expand Down Expand Up @@ -148,14 +146,10 @@ public function set($name, $value, $expire = null)
//数据压缩
$data = gzcompress($data, 3);
}
$data = "<?php\n//" . sprintf('%012d', $expire) . $data . "\n?>";
$data = "<?php\n//" . sprintf('%012d', $expire) . "\n exit();?>\n" . $data;
$result = file_put_contents($filename, $data);
if ($result) {
isset($first) && $this->setTagItem($filename);
if (is_file($filename . '.lock')) {
// 解除过期锁定文件
unlink($filename . '.lock');
}
clearstatcache();
return true;
} else {
Expand Down

0 comments on commit a217d88

Please sign in to comment.