From 50c89e2badbad7ad4542f61724e285a18e4c7e39 Mon Sep 17 00:00:00 2001 From: Markus Birth Date: Sun, 13 Feb 2022 16:15:48 +0100 Subject: [PATCH] Moved caching methods out of functions.inc.php into ContentCache class. --- include/functions.inc.php | 52 -------------------- include/functions_comments.inc.php | 10 ++-- include/functions_entries.inc.php | 21 ++++++--- include/functions_images.inc.php | 19 +++++--- lib/Serendipity/ContentCache.php | 76 ++++++++++++++++++++++++++++++ 5 files changed, 110 insertions(+), 68 deletions(-) create mode 100644 lib/Serendipity/ContentCache.php diff --git a/include/functions.inc.php b/include/functions.inc.php index cbd21468..cb9f7929 100644 --- a/include/functions.inc.php +++ b/include/functions.inc.php @@ -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 : */ diff --git a/include/functions_comments.inc.php b/include/functions_comments.inc.php index 821558a4..e5d4bda7 100644 --- a/include/functions_comments.inc.php +++ b/include/functions_comments.inc.php @@ -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; -} \ No newline at end of file +} diff --git a/include/functions_entries.inc.php b/include/functions_entries.inc.php index 78e15df2..9faaf2f1 100644 --- a/include/functions_entries.inc.php +++ b/include/functions_entries.inc.php @@ -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(); } /** diff --git a/include/functions_images.inc.php b/include/functions_images.inc.php index b86e8665..06073fcc 100644 --- a/include/functions_images.inc.php +++ b/include/functions_images.inc.php @@ -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 ' ' . MEDIA_RENAME_FAILED . "\n"; } - serendipity_cleanCache(); + $cache = ContentCache::getInstance(); + $cache->clearCache(); return TRUE; } @@ -3886,4 +3893,4 @@ function serendipity_checkDirUpload($dir) { } return false; -} \ No newline at end of file +} diff --git a/lib/Serendipity/ContentCache.php b/lib/Serendipity/ContentCache.php new file mode 100644 index 00000000..5623b4c2 --- /dev/null +++ b/lib/Serendipity/ContentCache.php @@ -0,0 +1,76 @@ +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(); + } +}