Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

支持忽略反序列化数据时的异常 #3056 #3068

Open
wants to merge 1 commit into
base: 8.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions src/think/cache/Driver.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
Expand All @@ -8,7 +9,7 @@
// +----------------------------------------------------------------------
// | Author: liu21st <[email protected]>
// +----------------------------------------------------------------------
declare (strict_types = 1);
declare (strict_types=1);

namespace think\cache;

Expand Down Expand Up @@ -247,18 +248,38 @@ protected function serialize($data): string
/**
* 反序列化数据
* @access protected
* @param string $data 缓存数据
* @param string $data 缓存数据
* @param string $name 缓存名
* @param mixed $default 默认值
* @return mixed
*/
protected function unserialize(string $data)
protected function unserialize(string $data, string $name = '', mixed $default = null)
{
if (is_numeric($data)) {
return $data;
}

$unserialize = $this->options['serialize'][1] ?? "unserialize";
set_error_handler(function ($code, $message, $filename, $line) {
throw new \ErrorException($message, $code, 0, $filename, $line);
});

try {
$unserialize = $this->options['serialize'][1] ?? 'unserialize';
$content = $unserialize($data);

restore_error_handler();
} catch (Throwable $e) {
restore_error_handler();

if (empty($this->options['serialize'][2])) {
throw $e;
} else {
$content = $default;
$this->delete($name);
}
}

return $unserialize($data);
return $content;
}

/**
Expand Down
7 changes: 4 additions & 3 deletions src/think/cache/driver/File.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
Expand All @@ -8,7 +9,7 @@
// +----------------------------------------------------------------------
// | Author: liu21st <[email protected]>
// +----------------------------------------------------------------------
declare (strict_types = 1);
declare (strict_types=1);

namespace think\cache\driver;

Expand Down Expand Up @@ -135,7 +136,7 @@ public function get($name, $default = null): mixed
{
$raw = $this->getRaw($name);

return is_null($raw) ? $default : $this->unserialize($raw['content']);
return is_null($raw) ? $default : $this->unserialize($raw['content'], $name, $default);
}

/**
Expand Down Expand Up @@ -199,7 +200,7 @@ public function set($name, $value, $expire = null): bool
public function inc($name, $step = 1)
{
if ($raw = $this->getRaw($name)) {
$value = $this->unserialize($raw['content']) + $step;
$value = $this->unserialize($raw['content'], $name, 0) + $step;
$expire = $raw['expire'];
} else {
$value = $step;
Expand Down
7 changes: 4 additions & 3 deletions src/think/cache/driver/Memcache.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
Expand All @@ -8,7 +9,7 @@
// +----------------------------------------------------------------------
// | Author: liu21st <[email protected]>
// +----------------------------------------------------------------------
declare (strict_types = 1);
declare (strict_types=1);

namespace think\cache\driver;

Expand Down Expand Up @@ -52,7 +53,7 @@ public function __construct(array $options = [])
$this->options = array_merge($this->options, $options);
}

$this->handler = new \Memcache;
$this->handler = new \Memcache();

// 支持集群
$hosts = (array) $this->options['host'];
Expand Down Expand Up @@ -95,7 +96,7 @@ public function get($name, $default = null): mixed
{
$result = $this->handler->get($this->getCacheKey($name));

return false !== $result ? $this->unserialize($result) : $default;
return false !== $result ? $this->unserialize($result, $name, $default) : $default;
}

/**
Expand Down
7 changes: 4 additions & 3 deletions src/think/cache/driver/Memcached.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
Expand All @@ -8,7 +9,7 @@
// +----------------------------------------------------------------------
// | Author: liu21st <[email protected]>
// +----------------------------------------------------------------------
declare (strict_types = 1);
declare (strict_types=1);

namespace think\cache\driver;

Expand Down Expand Up @@ -53,7 +54,7 @@ public function __construct(array $options = [])
$this->options = array_merge($this->options, $options);
}

$this->handler = new \Memcached;
$this->handler = new \Memcached();

if (!empty($this->options['option'])) {
$this->handler->setOptions($this->options['option']);
Expand Down Expand Up @@ -109,7 +110,7 @@ public function get($name, $default = null): mixed
{
$result = $this->handler->get($this->getCacheKey($name));

return false !== $result ? $this->unserialize($result) : $default;
return false !== $result ? $this->unserialize($result, $name, $default) : $default;
}

/**
Expand Down
9 changes: 5 additions & 4 deletions src/think/cache/driver/Redis.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
Expand All @@ -8,7 +9,7 @@
// +----------------------------------------------------------------------
// | Author: liu21st <[email protected]>
// +----------------------------------------------------------------------
declare (strict_types = 1);
declare (strict_types=1);

namespace think\cache\driver;

Expand Down Expand Up @@ -54,7 +55,7 @@ public function handler()
{
if (!$this->handler) {
if (extension_loaded('redis')) {
$this->handler = new \Redis;
$this->handler = new \Redis();

if ($this->options['persistent']) {
$this->handler->pconnect($this->options['host'], (int) $this->options['port'], (int) $this->options['timeout'], 'persistent_id_' . $this->options['select']);
Expand Down Expand Up @@ -120,7 +121,7 @@ public function get($name, $default = null): mixed
return $default;
}

return $this->unserialize($value);
return $this->unserialize($value, $name, $value);
}

/**
Expand Down Expand Up @@ -243,5 +244,5 @@ public function getTagItems($tag): array
public function __call($method, $args)
{
return call_user_func_array([$this->handler(), $method], $args);
}
}
}
5 changes: 3 additions & 2 deletions src/think/cache/driver/Wincache.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
Expand All @@ -8,7 +9,7 @@
// +----------------------------------------------------------------------
// | Author: liu21st <[email protected]>
// +----------------------------------------------------------------------
declare (strict_types = 1);
declare (strict_types=1);

namespace think\cache\driver;

Expand Down Expand Up @@ -75,7 +76,7 @@ public function get($name, $default = null): mixed
{
$key = $this->getCacheKey($name);

return wincache_ucache_exists($key) ? $this->unserialize(wincache_ucache_get($key)) : $default;
return wincache_ucache_exists($key) ? $this->unserialize(wincache_ucache_get($key), $name, $default) : $default;
}

/**
Expand Down
23 changes: 22 additions & 1 deletion tests/CacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ public function testFileCache()
$this->cache->setMultiple(['foo' => ['foobar', 'bar'], 'foobar' => ['foo', 'bar']]);
$this->cache->tag('foo')->setMultiple(['foo' => ['foobar', 'bar'], 'foobar' => ['foo', 'bar']]);
$this->assertEquals(['foo' => ['foobar', 'bar'], 'foobar' => ['foo', 'bar']], $this->cache->getMultiple(['foo', 'foobar']));
$this->assertIsInt($this->cache->getWriteTimes());
$this->assertTrue($this->cache->deleteMultiple(['foo', 'foobar']));
}

Expand Down Expand Up @@ -151,4 +150,26 @@ public function testRedisCache()
$this->cache->tag('foo')->set('bar', 'foobar');
$this->cache->tag('foo')->clear();
}

// 忽略反序列化数据时的异常
public function testIgnoreUnserializeException()
{
$root = vfsStream::setup();

$this->config->shouldReceive('get')->with('cache.default', null)->andReturn('file');

$this->config->shouldReceive('get')->with('cache.stores.file', null)
->andReturn(['type' => 'file', 'path' => $root->url(), 'serialize' => ['serialize', 'unserialize', true]]);

$this->cache->set('unserializeException', ['name' => 'thinkphp']);
$this->assertEquals(['name' => 'thinkphp'], $this->cache->get('unserializeException'));

// 篡改缓存文件,使缓存中的序列化数据无效
$filename = $this->cache->getCacheKey('unserializeException');
file_put_contents($filename, str_replace('s:8:"thinkphp"', '', file_get_contents($filename)));
$this->assertEquals(['name' => 'thinkphp1'], $this->cache->get('unserializeException', ['name' => 'thinkphp1']));

// 序列化数据无效会自动删除缓存
$this->assertFalse($this->cache->has('unserializeException'));
}
}
Loading