[RedisCache][DiskCache] Check if unserialize succeeded

This commit is contained in:
Alexei Sorokin 2020-08-10 11:12:31 +03:00 committed by Diogo Peralta Cordeiro
parent ed08b46c0b
commit cce2c763d3
2 changed files with 54 additions and 46 deletions

View File

@ -1,56 +1,47 @@
<?php
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU social is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
/**
* StatusNet - the distributed open-source microblogging tool
* Copyright (C) 2009, StatusNet, Inc.
*
* Plugin to implement cache interface with disk files
*
* PHP version 5
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @category Cache
* @package StatusNet
* @package GNUsocial
* @author Evan Prodromou <evan@status.net>
* @copyright 2009 StatusNet, Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link http://status.net/
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
if (!defined('STATUSNET')) {
// This check helps protect against security problems;
// your code file can't be executed directly from the web.
exit(1);
}
defined('GNUSOCIAL') || die();
/**
* A plugin to cache data on local disk
*
* @category Cache
* @package StatusNet
* @package GNUsocial
* @author Evan Prodromou <evan@status.net>
* @copyright 2009 StatusNet, Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link http://status.net/
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
class DiskCachePlugin extends Plugin
{
const PLUGIN_VERSION = '2.0.0';
var $root = '/tmp';
public $root = '/tmp';
function keyToFilename($key)
public function keyToFilename($key)
{
return $this->root . '/' . str_replace(':', '/', $key);
}
@ -65,17 +56,27 @@ class DiskCachePlugin extends Plugin
*
* @return boolean hook success
*/
function onStartCacheGet(&$key, &$value)
public function onStartCacheGet(&$key, &$value)
{
$filename = $this->keyToFilename($key);
$data = false;
if (file_exists($filename)) {
$data = file_get_contents($filename);
if ($data !== false) {
$value = unserialize($data);
}
}
if ($data === false) {
return true;
}
$ret = unserialize($data);
if ($ret === false && $data !== 'b:0;') {
common_log(LOG_ERR, 'DiskCache could not handle: ' . $data);
return true;
}
$value = $ret;
return false;
}
@ -90,7 +91,7 @@ class DiskCachePlugin extends Plugin
*
* @return boolean hook success
*/
function onStartCacheSet(&$key, &$value, &$flag, &$expiry, &$success)
public function onStartCacheSet(&$key, &$value, &$flag, &$expiry, &$success)
{
$filename = $this->keyToFilename($key);
$parent = dirname($filename);
@ -147,7 +148,7 @@ class DiskCachePlugin extends Plugin
*
* @return boolean hook success
*/
function onStartCacheDelete(&$key, &$success)
public function onStartCacheDelete(&$key, &$success)
{
$filename = $this->keyToFilename($key);

View File

@ -15,7 +15,7 @@
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
/**
* Plguin inplementing Redis based caching
* Plugin inplementing Redis based caching
*
* @category Files
* @package GNUsocial
@ -58,23 +58,30 @@ class RedisCachePlugin extends Plugin
{
try {
$this->_ensureConn();
$ret = $this->client->get($key);
$data = $this->client->get($key);
} catch (PredisException $e) {
common_log(LOG_ERR, 'RedisCache encountered exception ' . get_class($e) . ': ' . $e->getMessage());
return true;
}
// Hit, overwrite "value" and return false
// to indicate we took care of this
if ($ret !== null) {
$value = unserialize($ret);
return false;
}
if (is_null($data)) {
// Miss, let GS do its thing
return true;
}
$ret = unserialize($data);
if ($ret === false && $data !== 'b:0;') {
common_log(LOG_ERR, 'RedisCache could not handle: ' . $data);
return true;
}
// Hit, overwrite "value" and return false
// to indicate we took care of this
$value = $ret;
return false;
}
public function onStartCacheSet($key, $value, $flag, $expiry, &$success)
{
$success = false;