Move memcached handling to functions in preparation for adding disk caching.

This commit is contained in:
Retro_Guy 2024-06-07 04:13:23 -07:00
parent 98db866a06
commit 6e7b8b4057
4 changed files with 165 additions and 113 deletions

View File

@ -196,17 +196,17 @@ function message_read($id, $bodynum = 0, $group = "")
}
// MEMCACHE if ($id, 0, $group)
if ($bodynum == 0 && $group != "") {
if (file_exists($config_dir . '/memcache.inc.php')) {
include $config_dir . '/memcache.inc.php';
if (file_exists($config_dir . '/cache.inc.php')) {
include $config_dir . '/cache.inc.php';
}
}
if ($memcacheD) {
$memcache_key = $memcache_key_prefix . '_' . 'message_read-' . $id . '-0-' . $group;
$message_data = $memcacheD->get($memcache_key);
if ($enable_cache) {
$cache_key = $cache_key_prefix . '_' . 'message_read-' . $id . '-0-' . $group;
$message_data = cache_get($cache_key, $enable_cache, $memcacheD);
if ($message_data) {
if ($message = unserialize(gzuncompress($message_data))) {
if ($enable_memcache_logging) {
file_put_contents($cache_log, "\n" . format_log_date() . " (cache hit) $memcache_key", FILE_APPEND);
if ($enable_cache_logging) {
file_put_contents($cache_log, "\n" . format_log_date() . " (cache hit) $cache_key", FILE_APPEND);
}
return $message;
}
@ -306,10 +306,10 @@ function message_read($id, $bodynum = 0, $group = "")
}
}
// MEMCACHE if ($id, 0, $group)
if ($memcacheD) {
$nicole = $memcacheD->add($memcache_key, gzcompress(serialize($message)), $memcache_ttl);
if ($enable_memcache_logging && $nicole) {
file_put_contents($cache_log, "\n" . format_log_date() . " (cache write) " . $memcache_key, FILE_APPEND);
if ($enable_cache) {
$nicole = cache_add($cache_key, gzcompress(serialize($message)), $cache_ttl, $enable_cache, $memcacheD);
if ($enable_cache_logging && $nicole) {
file_put_contents($cache_log, "\n" . format_log_date() . " (cache write) " . $cache_key, FILE_APPEND);
}
}
return $message;

View File

@ -68,17 +68,17 @@ function thread_pageselect($group, $article_count, $first)
function thread_cache_load($group)
{
global $spooldir, $config_dir, $logdir, $compress_spoolfiles;
if (file_exists($config_dir . '/memcache.inc.php')) {
include $config_dir . '/memcache.inc.php';
if (file_exists($config_dir . '/cache.inc.php')) {
include $config_dir . '/cache.inc.php';
}
// Check memcache
if ($memcacheD) {
$memcache_key = $memcache_key_prefix . '_' . 'thread_cache-' . $group;
$message_data = $memcacheD->get($memcache_key);
if ($enable_cache) {
$cache_key = $cache_key_prefix . '_' . 'thread_cache-' . $group;
$message_data = cache_get($cache_key, $enable_cache, $memcacheD);
if ($message_data) {
if ($headers = unserialize(gzuncompress($message_data))) {
if ($enable_memcache_logging) {
file_put_contents($cache_log, "\n" . format_log_date() . " (cache hit) $memcache_key", FILE_APPEND);
if ($enable_cache_logging) {
file_put_contents($cache_log, "\n" . format_log_date() . " (cache hit) $cache_key", FILE_APPEND);
}
return $headers;
}
@ -96,22 +96,22 @@ function thread_cache_load($group)
}
$dbh = null;
}
if ($memcacheD) {
if ($enable_cache) {
$add_thread = gzcompress(serialize($headers), 9);
$thread_bytes = strlen($add_thread);
$too_big = false;
if ($thread_bytes < $memcache_maxitemsize) {
$nicole = $memcacheD->add($memcache_key, $add_thread, $memcache_ttl);
if ($thread_bytes < $cache_maxitemsize) {
$nicole = cache_add($cache_key, $add_thread, $cache_ttl, $enable_cache, $memcacheD);
} else {
$nicole = false;
$too_big = true;
}
if ($nicole && $enable_memcache_logging) {
file_put_contents($cache_log, "\n" . format_log_date() . " (cache write) $memcache_key (" . strlen($add_thread) . " bytes)", FILE_APPEND);
if ($nicole && $enable_cache_logging) {
file_put_contents($cache_log, "\n" . format_log_date() . " (cache write) $cache_key (" . strlen($add_thread) . " bytes)", FILE_APPEND);
}
if ($too_big) {
file_put_contents($cache_log, "\n" . format_log_date() . " $memcache_key too large (" . $thread_bytes . " bytes)", FILE_APPEND);
file_put_contents($cache_log, "\n" . format_log_date() . " $cache_key too large (" . $thread_bytes . " bytes)", FILE_APPEND);
}
}
return ($headers);
@ -128,8 +128,8 @@ function thread_cache_save($headers, $group)
global $spooldir, $compress_spoolfiles, $config_dir, $logdir, $config_name;
$logfile = $logdir . '/newsportal.log';
if (file_exists($config_dir . '/memcache.inc.php')) {
include $config_dir . '/memcache.inc.php';
if (file_exists($config_dir . '/cache.inc.php')) {
include $config_dir . '/cache.inc.php';
}
$database = $spooldir . '/' . $group . '-data.db3';
@ -151,27 +151,27 @@ function thread_cache_save($headers, $group)
]);
$dbh->commit();
$dbh = null;
if ($memcacheD) {
$memcache_key = $memcache_key_prefix . '_' . 'thread_cache-' . $group;
$del = $memcacheD->delete($memcache_key);
if ($enable_cache) {
$cache_key = $cache_key_prefix . '_' . 'thread_cache-' . $group;
$del = cache_delete($cache_key, $enable_cache, $memcacheD);
$add_thread = gzcompress(serialize($headers), 9);
$thread_bytes = strlen($add_thread);
$too_big = false;
if ($thread_bytes < $memcache_maxitemsize) {
$nicole = $memcacheD->add($memcache_key, $add_thread, $memcache_ttl);
if ($thread_bytes < $cache_maxitemsize) {
$nicole = cache_add($cache_key, $add_thread, $cache_ttl, $enable_cache, $memcacheD);
} else {
$too_big = true;
$nicole = false;
}
if ($enable_memcache_logging) {
if ($enable_cache_logging) {
if ($del) {
file_put_contents($cache_log, "\n" . format_log_date() . " (cache delete) $memcache_key", FILE_APPEND);
file_put_contents($cache_log, "\n" . format_log_date() . " (cache delete) $cache_key", FILE_APPEND);
}
if ($nicole) {
file_put_contents($cache_log, "\n" . format_log_date() . " (cache write) $memcache_key (" . $thread_bytes . " bytes)", FILE_APPEND);
file_put_contents($cache_log, "\n" . format_log_date() . " (cache write) $cache_key (" . $thread_bytes . " bytes)", FILE_APPEND);
}
if ($too_big) {
file_put_contents($cache_log, "\n" . format_log_date() . " $memcache_key too large (" . $thread_bytes . " bytes)", FILE_APPEND);
file_put_contents($cache_log, "\n" . format_log_date() . " $cache_key too large (" . $thread_bytes . " bytes)", FILE_APPEND);
}
}
}

View File

@ -602,8 +602,8 @@ function groups_show($gruppen)
return;
global $file_thread, $text_groups;
$logfile = $logdir . '/debug.log';
if (file_exists($config_dir . '/memcache.inc.php')) {
include $config_dir . '/memcache.inc.php';
if (file_exists($config_dir . '/cache.inc.php')) {
include $config_dir . '/cache.inc.php';
}
$c = count($gruppen);
$acttype = "keins";
@ -639,14 +639,14 @@ function groups_show($gruppen)
$found = 0;
// Get last article info from article database
// First check memcache
if ($memcacheD) {
$memcache_key = $memcache_key_prefix . '_' . 'lastarticleinfo-' . $g->name;
if ($enable_cache) {
$memcache_key = $cache_key_prefix . '_' . 'lastarticleinfo-' . $g->name;
$groupfile = $spooldir . '/' . $g->name . '-lastarticleinfo.dat';
$lar = $memcacheD->get($memcache_key);
$lar = cache_get($memcache_key, $enable_cache, $memcacheD);
if ($lar) {
if ($lastarticleinfo = unserialize($lar)) {
if ($lastarticleinfo && file_exists($groupfile) && filemtime($groupfile) <= $lastarticleinfo['date']) {
if ($enable_memcache_logging) {
if ($enable_cache_logging) {
file_put_contents($cache_log, "\n" . format_log_date() . ' (cache hit) lastarticleinfo for ' . $g->name, FILE_APPEND);
}
$found = 1;
@ -688,11 +688,11 @@ function groups_show($gruppen)
}
if ($found == 1) {
$lastarticleinfo = $row;
if ($memcacheD) {
if ($enable_cache) {
touch($groupfile, $lastarticleinfo['date']);
$nicole = $memcacheD->delete($memcache_key);
$memcacheD->add($memcache_key, serialize($row), $memcache_ttl);
if ($enable_memcache_logging) {
$nicole = cache_delete($memcache_key, $enable_cache, $memcacheD);
cache_add($memcache_key, serialize($row), $cache_ttl, $enable_cache, $memcacheD);
if ($enable_cache_logging) {
if ($nicole) {
file_put_contents($cache_log, "\n" . format_log_date() . " (cache update) $memcache_key", FILE_APPEND);
} else {
@ -1622,14 +1622,14 @@ function get_date_interval($value)
function get_newsgroups_by_msgid($msgid, $noarray = false)
{
global $spooldir, $config_dir, $logdir, $CONFIG;
if (file_exists($config_dir . '/memcache.inc.php')) {
include $config_dir . '/memcache.inc.php';
if (file_exists($config_dir . '/cache.inc.php')) {
include $config_dir . '/cache.inc.php';
}
if ($memcacheD) {
$memcache_key = $memcache_key_prefix . '_' . 'get_newsgroups_by_msgid-' . $msgid;
if ($getgroups = $memcacheD->get($memcache_key)) {
if ($enable_cache) {
$memcache_key = $cache_key_prefix . '_' . 'get_newsgroups_by_msgid-' . $msgid;
if ($getgroups = cache_get($memcache_key, $enable_cache, $memcacheD)) {
if ($groups = unserialize($getgroups)) {
if ($enable_memcache_logging) {
if ($enable_cache_logging) {
file_put_contents($cache_log, "\n" . format_log_date() . " (cache hit) $memcache_key", FILE_APPEND);
}
}
@ -1654,8 +1654,8 @@ function get_newsgroups_by_msgid($msgid, $noarray = false)
}
$overview_dbh = null;
if ($groups && $memcacheD) {
$nicole = $memcacheD->add($memcache_key, serialize($groups), $memcache_ttl);
if ($enable_memcache_logging && $nicole) {
$nicole = cache_add($memcache_key, serialize($groups), $cache_ttl, $enable_cache, $memcacheD);
if ($enable_cache_logging && $nicole) {
file_put_contents($cache_log, "\n" . format_log_date() . " (cache write) $memcache_key", FILE_APPEND);
}
}
@ -1905,19 +1905,19 @@ function np_get_db_article($article, $group, $makearray = 1, $dbh = null)
global $config_dir, $path, $groupconfig, $config_name, $logdir, $spooldir;
$logfile = $logdir . '/newsportal.log';
if (file_exists($config_dir . '/memcache.inc.php')) {
include $config_dir . '/memcache.inc.php';
if (file_exists($config_dir . '/cache.inc.php')) {
include $config_dir . '/cache.inc.php';
}
$msg2 = "";
$closeme = 0;
$ok_article = 0;
// Check memcache
if ($memcacheD) {
$article_key = $memcache_key_prefix . '_' . 'article.db3-' . $group . ':' . $article;
if ($msg2 = $memcacheD->get($article_key)) {
if ($enable_cache) {
$article_key = $cache_key_prefix . '_' . 'article.db3-' . $group . ':' . $article;
if ($msg2 = cache_get($article_key, $enable_cache, $memcacheD)) {
$ok_article = 1;
if ($enable_memcache_logging) {
if ($enable_cache_logging) {
file_put_contents($cache_log, "\n" . format_log_date() . " (cache hit) $article_key", FILE_APPEND);
}
}
@ -1952,8 +1952,8 @@ function np_get_db_article($article, $group, $makearray = 1, $dbh = null)
}
}
if ($ok_article == 1 && $memcacheD) {
$nicole = $memcacheD->add($article_key, $msg2, $memcache_ttl);
if ($enable_memcache_logging && $nicole) {
$nicole = cache_add($article_key, $msg2, $cache_ttl, $enable_cache, $memcacheD);
if ($enable_cache_logging && $nicole) {
file_put_contents($cache_log, "\n" . format_log_date() . " (cache write) $article_key", FILE_APPEND);
}
}
@ -2475,13 +2475,13 @@ function insert_article_from_array($this_article, $check_duplicates = true)
unlink($grouppath . "/" . $this_article['local']);
$article_dbh = null;
// Add to memcache
if (file_exists($config_dir . '/memcache.inc.php')) {
include $config_dir . '/memcache.inc.php';
if (file_exists($config_dir . '/cache.inc.php')) {
include $config_dir . '/cache.inc.php';
}
if ($memcacheD) {
$article_key = $memcache_key_prefix . '_' . 'article.db3-' . $group . ':' . $this_article['local'];
$nicole = $memcacheD->add($article_key, $this_article['article'], $memcache_ttl);
if ($enable_memcache_logging && $nicole) {
if ($enable_cache) {
$article_key = $cache_key_prefix . '_' . 'article.db3-' . $group . ':' . $this_article['local'];
$nicole = cache_add($article_key, $this_article['article'], $cache_ttl, $enable_cache, $memcacheD);
if ($enable_cache_logging && $nicole) {
file_put_contents($cache_log, "\n" . format_log_date() . " (cache write) (new) $article_key", FILE_APPEND);
}
}
@ -2554,14 +2554,14 @@ function clear_history_by_group($group)
function get_db_data_from_msgid($msgid, $group)
{
global $spooldir, $config_dir, $logdir;
if (file_exists($config_dir . '/memcache.inc.php')) {
include $config_dir . '/memcache.inc.php';
if (file_exists($config_dir . '/cache.inc.php')) {
include $config_dir . '/cache.inc.php';
}
if ($memcacheD) {
$row_cache = $memcache_key_prefix . '_' . 'get_db_data_from_msgid-' . $msgid;
if ($row = $memcacheD->get($row_cache)) {
if ($enable_memcache_logging) {
if ($enable_cache) {
$row_cache = $cache_key_prefix . '_' . 'get_db_data_from_msgid-' . $msgid;
if ($row = cache_get($row_cache, $enable_cache, $memcacheD)) {
if ($enable_cache_logging) {
file_put_contents($cache_log, "\n" . format_log_date() . " (cache hit) $row_cache", FILE_APPEND);
}
return $row;
@ -2584,9 +2584,9 @@ function get_db_data_from_msgid($msgid, $group)
}
$dbh = null;
if ($found) {
if ($memcacheD) {
$nicole = $memcacheD->add($row_cache, $row, $memcache_ttl);
if ($enable_memcache_logging && $nicole) {
if ($enable_cache) {
$nicole = cache_add($row_cache, $row, $cache_ttl, $enable_cache, $memcacheD);
if ($enable_cache_logging && $nicole) {
file_put_contents($cache_log, "\n" . format_log_date() . " (cache write) $row_cache", FILE_APPEND);
}
}
@ -2624,18 +2624,18 @@ function get_group_array_from_msgid($msgid)
function get_data_from_msgid($msgid, $thisgroup = null)
{
global $spooldir, $config_dir, $logdir, $CONFIG;
if (file_exists($config_dir . '/memcache.inc.php')) {
include $config_dir . '/memcache.inc.php';
if (file_exists($config_dir . '/cache.inc.php')) {
include $config_dir . '/cache.inc.php';
}
if ($CONFIG['article_database'] == '1' && isset($thisgroup)) {
return get_db_data_from_msgid($msgid, $thisgroup);
}
if ($memcacheD) {
$row_cache = $memcache_key_prefix . '_' . 'get_data_from_msgid-' . $msgid;
if ($row = $memcacheD->get($row_cache)) {
if ($enable_memcache_logging) {
if ($enable_cache) {
$row_cache = $cache_key_prefix . '_' . 'get_data_from_msgid-' . $msgid;
if ($row = cache_get($row_cache, $enable_cache, $memcacheD)) {
if ($enable_cache_logging) {
file_put_contents($cache_log, "\n" . format_log_date() . " (cache hit) $row_cache", FILE_APPEND);
}
return $row;
@ -2663,9 +2663,9 @@ function get_data_from_msgid($msgid, $thisgroup = null)
}
$dbh = null;
if ($found) {
if ($memcacheD) {
$nicole = $memcacheD->add($row_cache, $row, $memcache_ttl);
if ($enable_memcache_logging && $nicole) {
if ($enable_cache) {
$nicole = cache_add($row_cache, $row, $cache_ttl, $enable_cache, $memcacheD);
if ($enable_cache_logging && $nicole) {
file_put_contents($cache_log, "\n" . format_log_date() . " (cache write) $row_cache", FILE_APPEND);
}
}
@ -2749,8 +2749,8 @@ function send_admin_message($admin, $from, $subject, $message)
function delete_message($messageid, $group = null, $overview_dbh = null)
{
global $logfile, $logdir, $config_dir, $spooldir, $CONFIG, $webserver_group;
if (file_exists($config_dir . '/memcache.inc.php')) {
include $config_dir . '/memcache.inc.php';
if (file_exists($config_dir . '/cache.inc.php')) {
include $config_dir . '/cache.inc.php';
}
if ($group == null) {
$grouplist = get_newsgroups_by_msgid($messageid);
@ -2817,10 +2817,10 @@ function delete_message($messageid, $group = null, $overview_dbh = null)
':msgid' => $messageid
]);
// Delete article from memcache
if ($memcacheD) {
$article_key = $memcache_key_prefix . '_' . 'article.db3-' . $group . ':' . $row['number'];
$result = $memcacheD->delete($article_key);
if ($enable_memcache_logging) {
if ($enable_cache) {
$article_key = $cache_key_prefix . '_' . 'article.db3-' . $group . ':' . $row['number'];
$result = cache_delete($article_key, $enable_cache, $memcacheD);
if ($enable_cache_logging) {
if ($result) {
file_put_contents($cache_log, "\n" . format_log_date() . " Deleted $article_key", FILE_APPEND);
} else {
@ -2949,4 +2949,37 @@ function delete_message_from_overboard($config_name, $group, $messageid)
file_put_contents($cachefile, serialize($cached_overboard));
}
}
}
function cache_add($cache_key, $data, $cache_ttl, $enable_cache, $memcacheD = null) {
if($enable_cache == 'memcached'){
if($memcacheD) {
if($nicole = $memcacheD->add($cache_key, $data, $cache_ttl)) {
return $nicole;
}
}
return false;
}
}
function cache_delete($cache_key, $enable_cache, $memcacheD = null) {
if($enable_cache == 'memcached'){
if($memcacheD) {
if($nicole = $memcacheD->delete($cache_key)) {
return $nicole;
}
}
return false;
}
}
function cache_get($cache_key, $enable_cache, $memcacheD = null) {
if($enable_cache == 'memcached'){
if($memcacheD) {
if($nicole = $memcacheD->get($cache_key)) {
return $nicole;
}
}
return false;
}
}

View File

@ -1,44 +1,61 @@
<?php
/* memcached and php-memcached must be installed */
global $rslight_version, $spooldir, $logdir;
/*
* Set $enable_cache to the cache type you want to use
* memcached and php-memcached must be installed
* if using memcached.
* $enable_cache = 'memcached';
*
* This will use a directory for caching, no memcache
* $enable_cache = 'diskcache';
*
* or set to false (no quotes) to disable caching:
* $enable_cache = false;
*/
// $enable_cache = 'memcached';
// $enable_cache = 'diskcache';
$enable_cache = false;
// Set to true to enable memcache
$enable_memcache = false;
// Enable logging to file (log file may be large)
$enable_cache_logging = false;
// Server & port details
// Server & port details if using memcached
$memcache_server = '127.0.0.1';
$memcache_port = 11211;
// Enable logging to file (log file may be large)
$enable_memcache_logging = false;
// Time in seconds to cache data
$memcache_ttl = 14400;
/*
* Maximum size of data (in bytes) to save per key in memcache
* This must be less than or equal to
*
* If using memcached This must be less than or equal to
* MAXITEMSIZE in memcached, which is 1MiB by default
* Increasing this here will not work unless it is also
* increased in memcached configuration
*
* If using diskcache, pruning by size is only done daily
*
* You probably do not need to change this
*/
$memcache_maxitemsize = 1024000;
$cache_maxitemsize = 1024000;
/*
// Time in seconds to cache data
$cache_ttl = 14400;
/*
* A string to prepend to cached key names
* Required if using more than one rslight instance
* Necessary if using more than one rslight instance
* with one memcache instance
*/
$memcache_key_prefix = 'rsl';
$cache_key_prefix = 'mysite';
/* PLEASE DO NOT EDIT BELOW THIS LINE */
if ($enable_memcache) {
// Add version to prefix to avoid errors if upgrading
// and not restarting memcached
global $rslight_version;
$memcache_key_prefix .= trim(preg_replace('/\./', '', $rslight_version));
$cache_log = $logdir . '/cache.log';
// Add version to prefix to avoid errors if upgrading
// and not restarting memcached
$cache_key_prefix .= trim(preg_replace('/\./', '', $rslight_version));
/* IF MEMCACHED */
if ($enable_cache == 'memcached') {
$memcacheD = new Memcached('memcacheD');
$memcacheD->setOption(Memcached::OPT_LIBKETAMA_COMPATIBLE, true);
$memcacheD->setOption(Memcached::OPT_CONNECT_TIMEOUT, 1000);
@ -54,5 +71,7 @@ if ($enable_memcache) {
file_put_contents($logdir . '/memcache.log', "\n" . format_log_date() . ' Connected memcache ' . $memcache_server . ':' . $memcache_port, FILE_APPEND);
}
}
} else {
$memcacheD = null;
}
/* END IF MEMCACHED */