Moved caching methods out of functions.inc.php into ContentCache class.

This commit is contained in:
Markus Birth 2022-02-13 16:15:48 +01:00
parent bb98e38522
commit 50c89e2bad
Signed by: mbirth
GPG Key ID: A9928D7A098C3A9A
5 changed files with 110 additions and 68 deletions

View File

@ -1387,57 +1387,5 @@ function serendipity_url_allowed($url) {
return true;
}
use voku\cache\Cache;
// Configure voku/simple-cache to use templates_c as directory for the opcache files, the fallback
// when Memcached and Redis are not used. Returns the configured cache object. Used internally by
// the other cache functions, you most likely never need to call this.
function serendipity_setupCache() {
$cacheManager = new \voku\cache\CacheAdapterAutoManager();
$cacheManager->addAdapter(
\voku\cache\AdapterOpCache::class,
static function () {
global $serendipity;
$cacheDir = $serendipity['serendipityPath'] . '/templates_c/simple_cache';
return $cacheDir;
}
);
$cacheManager->addAdapter(
\voku\cache\AdapterArray::class
);
$cache = new Cache(
null,
null,
false,
true,
false,
false,
false,
false,
'',
$cacheManager,
false
);
return $cache;
}
function serendipity_cleanCache() {
$cache = serendipity_setupCache();
return $cache->removeAll();
}
function serendipity_cacheItem($key, $item, $ttl = 3600) {
$cache = serendipity_setupCache();
return $cache->setItem($key, $item, $ttl);
}
function serendipity_getCacheItem($key) {
$cache = serendipity_setupCache();
return $cache->getItem($key);
}
define("serendipity_FUNCTIONS_LOADED", true);
/* vim: set sts=4 ts=4 expandtab : */

View File

@ -2,6 +2,8 @@
# Copyright (c) 2003-2005, Jannis Hermanns (on behalf the Serendipity Developer Team)
# All rights reserved. See LICENSE file for licensing details
use Serendipity\ContentCache;
if (IN_serendipity !== true) {
die ("Don't hack!");
}
@ -730,7 +732,8 @@ function serendipity_approveComment($cid, $entry_id, $force = false, $moderate =
serendipity_plugin_api::hook_event('backend_approvecomment', $rs);
}
serendipity_cleanCache();
$cache = ContentCache::getInstance();
$cache->clearCache();
if ($flip) {
if ($moderate) return -1; // comment set to pending
if (!$moderate) return 1; // comment set to approved
@ -950,7 +953,8 @@ function serendipity_insertComment($id, $commentInfo, $type = 'NORMAL', $source
if ($GLOBALS['tb_logging'] ?? false) {
fclose($fp);
}
serendipity_cleanCache();
$cache = ContentCache::getInstance();
$cache->clearCache();
return $cid;
}
@ -1237,4 +1241,4 @@ function serendipity_generateCToken($cid) {
VALUES ('" . time() . "', '" . $ctoken . "', 'comment_" . $cid ."')");
return $ctoken;
}
}

View File

@ -2,6 +2,8 @@
# Copyright (c) 2003-2005, Jannis Hermanns (on behalf the Serendipity Developer Team)
# All rights reserved. See LICENSE file for licensing details
use Serendipity\ContentCache;
if (IN_serendipity !== true) {
die ("Don't hack!");
}
@ -214,9 +216,10 @@ function &serendipity_fetchEntries($range = null, $full = true, $limit = '', $fe
if ($serendipity['useInternalCache']) {
$key = md5(serialize($initial_args) . ($serendipity['short_archives'] ?? '') . '||' . (serialize($serendipity['range'] ?? '')) . '||' . ($serendipity['GET']['category'] ?? '') . '||' . ($serendipity['GET']['hide_category'] ?? '') . '||' . ($serendipity['GET']['viewAuthor'] ?? '') . '||' . ($serendipity['GET']['page'] ?? '') . '||' . $serendipity['fetchLimit'] . '||' . $serendipity['max_fetch_limit'] . '||' . ($serendipity['GET']['adminModule'] ?? '') . '||' . serendipity_checkPermission('adminEntriesMaintainOthers') . '||' .$serendipity['showFutureEntries'] . '||' . $serendipity['archiveSortStable'] . '||' . ($serendipity['plugindata']['smartyvars']['uriargs'] ?? '') );
$entries = serendipity_getCacheItem($key);
$cache = ContentCache::getInstance();
$entries = $cache->getItem($key);
if ($entries && $entries !== false) {
$serendipity['fullCountQuery'] = serendipity_getCacheItem($key . '_fullCountQuery');
$serendipity['fullCountQuery'] = $cache->getItem($key . '_fullCountQuery');
return unserialize($entries);
}
}
@ -505,8 +508,9 @@ function &serendipity_fetchEntries($range = null, $full = true, $limit = '', $fe
if ($serendipity['useInternalCache']) {
$key = md5(serialize($initial_args) . ($serendipity['short_archives'] ?? '') . '||' . (serialize($serendipity['range'] ?? '')) . '||' . ($serendipity['GET']['category'] ?? '') . '||' . ($serendipity['GET']['hide_category'] ?? '') . '||' . ($serendipity['GET']['viewAuthor'] ?? '') . '||' . ($serendipity['GET']['page'] ?? '') . '||' . $serendipity['fetchLimit'] . '||' . $serendipity['max_fetch_limit'] . '||' . ($serendipity['GET']['adminModule'] ?? '') . '||' . serendipity_checkPermission('adminEntriesMaintainOthers') . '||' .$serendipity['showFutureEntries'] . '||' . $serendipity['archiveSortStable'] . '||' . ($serendipity['plugindata']['smartyvars']['uriargs'] ?? '') );
serendipity_cacheItem($key, serialize($ret));
serendipity_cacheItem($key . '_fullCountQuery', $serendipity['fullCountQuery']);
$cache = ContentCache::getInstance();
$cache->addItem($key, serialize($ret));
$cache->addItem($key . '_fullCountQuery', $serendipity['fullCountQuery']);
}
return $ret;
}
@ -1545,7 +1549,8 @@ function serendipity_updertEntry($entry) {
serendipity_handle_references($entry['id'], $serendipity['blogTitle'], $drafted_entry['title'], $drafted_entry['body'] . $drafted_entry['extended'], false);
}
serendipity_cleanCache();
$cache = ContentCache::getInstance();
$cache->clearCache();
return (int)$entry['id'];
}
@ -1578,7 +1583,8 @@ function serendipity_deleteEntry($id) {
serendipity_db_query("DELETE FROM {$serendipity['dbPrefix']}comments WHERE entry_id=$id");
serendipity_db_query("DELETE FROM {$serendipity['dbPrefix']}references WHERE entry_id='$id' AND type = ''");
serendipity_db_query("DELETE FROM {$serendipity['dbPrefix']}permalinks WHERE entry_id='$id'");
serendipity_cleanCache();
$cache = ContentCache::getInstance();
$cache->clearCache();
}
/**
@ -1677,7 +1683,8 @@ function serendipity_updateEntryCategories($postid, $categories) {
$query = "INSERT INTO $serendipity[dbPrefix]entrycat (categoryid, entryid) VALUES (" . (int)$cat . ", " . (int)$postid . ")";
serendipity_db_query($query);
}
serendipity_cleanCache();
$cache = ContentCache::getInstance();
$cache->clearCache();
}
/**

View File

@ -2,6 +2,8 @@
# Copyright (c) 2003-2005, Jannis Hermanns (on behalf the Serendipity Developer Team)
# All rights reserved. See LICENSE file for licensing details
use Serendipity\ContentCache;
if (IN_serendipity !== true) {
die ("Don't hack!");
}
@ -651,7 +653,8 @@ function serendipity_insertImageInDatabase($filename, $directory, $authorid = 0,
$image_id = serendipity_db_insert_id('images', 'id');
if ($image_id > 0) {
return $image_id;
serendipity_cleanCache();
$cache = ContentCache::getInstance();
$cache->clearCache();
}
return 0;
@ -1973,7 +1976,8 @@ function serendipity_traversePath($basedir, $dir='', $onlyDirs = true, $pattern
if ($serendipity['useInternalCache']) {
$key = md5($basedir . $dir . $onlyDirs . $pattern . $depth . $max_depth . $apply_ACL . serialize($aExcludeDirs) . $serendipity['serendipityUser']);
$files = serendipity_getCacheItem($key);
$cache = ContentCache::getInstance();
$files = $cache->getItem($key);
if ($files && $files !== false) {
return unserialize($files);
}
@ -2027,7 +2031,8 @@ function serendipity_traversePath($basedir, $dir='', $onlyDirs = true, $pattern
if ($serendipity['useInternalCache']) {
$key = md5($basedir . $dir . $onlyDirs . $pattern . $depth . $max_depth . $apply_ACL . serialize($aExcludeDirs) . $serendipity['serendipityUser']);
serendipity_cacheItem($key, serialize($files));
$cache = ContentCache::getInstance();
$cache->addItem($key, serialize($files));
}
return $files;
@ -2294,7 +2299,8 @@ function serendipity_renameDir($oldDir, $newDir) {
);
serendipity_updateImageInEntries($image['id'], $image);
}
serendipity_cleanCache();
$cache = ContentCache::getInstance();
$cache->clearCache();
return true;
}
return false;
@ -2349,7 +2355,8 @@ function serendipity_renameFile($id, $newName, $path = null) {
} else {
return '<span class="msg_error"><span class="icon-attention-circled" aria-hidden="true"></span> ' . MEDIA_RENAME_FAILED . "</span>\n";
}
serendipity_cleanCache();
$cache = ContentCache::getInstance();
$cache->clearCache();
return TRUE;
}
@ -3886,4 +3893,4 @@ function serendipity_checkDirUpload($dir) {
}
return false;
}
}

View File

@ -0,0 +1,76 @@
<?php
// Serendipity
// See LICENSE file for license information.
namespace Serendipity;
use voku\cache\Cache;
use voku\cache\CacheAdapterAutoManager;
use voku\cache\AdapterArray;
use voku\cache\AdapterOpCache;
class ContentCache
{
protected static $instance = null;
protected $cache_manager;
protected $cache;
public static function getInstance()
{
if (self::$instance === null) {
self::$instance = new self();
}
return self::$instance;
}
private function __construct()
{
// Configure voku/simple-cache to use templates_c as directory for the opcache files, the fallback
// when Memcached and Redis are not used.
// FIXME: Bad hack - remove when no longer needed!
global $serendipity;
$cacheDir = $serendipity['serendipityPath'] . '/templates_c/simple_cache';
$this->cache_manager = new CacheAdapterAutoManager();
$this->cache_manager->addAdapter(
AdapterOpCache::class,
static function () use ($cacheDir) {
return $cacheDir;
}
);
$this->cache_manager->addAdapter(
AdapterArray::class
);
$this->cache = new Cache(
null,
null,
false,
true,
false,
false,
false,
false,
'',
$this->cache_manager,
false
);
}
public function addItem($key, $item, $ttl = 3600)
{
return $this->cache->setItem($key, $item, $ttl);
}
public function getItem($key)
{
return $this->cache->getItem($key);
}
public function clearCache()
{
return $this->cache->removeAll();
}
}