Skip to content

Commit

Permalink
# Putting a versioning part in the cache key for newflag caching
Browse files Browse the repository at this point in the history
# resulted in way too many cache files when using the file cache layer.
# On my live system, this even used up all my /tmp filesystem's inodes.
# So without active cleanup scripts, we could expect user problems.
# I changed the caching layers. They do now take a $version parameter
# for the put and get functions. This parameter can be considered as
# a version based TTL. So if a key is requested with a newer version
# than the one that is stored in the cache, NULL will be returned.
# I also updated the newflag caching calls to use this new method
# of cache versioning.
  • Loading branch information
mmakaay committed Jan 20, 2007
1 parent a08b4eb commit 3034b4d
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 77 deletions.
36 changes: 22 additions & 14 deletions include/cache/apc.php
Expand Up @@ -25,20 +25,28 @@
* This function returns the cached data for the given key
* or NULL if no data is cached for this key
*/
function phorum_cache_get($type,$key) {
if(is_array($key)) {
$ret=array();
foreach($key as $realkey) {
$getkey=$type."_".$realkey;
$ret[$realkey]=apc_fetch($getkey);
}
} else {
$getkey=$type."_".$key;
$ret=apc_fetch($getkey);
}
function phorum_cache_get($type,$key,$version=NULL) {
if(is_array($key)) {
$ret=array();
foreach($key as $realkey) {
$getkey=$type."_".$realkey;
$data = apc_fetch($getkey);
if ($version == NULL ||
($data[1] != NULL && $data[1] == $version)) {
$ret[$realkey]=$data[0];
}
}
} else {
$getkey=$type."_".$key;
$data = apc_fetch($getkey);
if ($version == NULL ||
($data[1] != NULL && $data[1] == $version)) {
$ret=$data[0];
}
}

if($ret === false || (is_array($ret) && count($ret) == 0))
$ret=NULL;
$ret=NULL;

return $ret;

Expand All @@ -49,9 +57,9 @@ function phorum_cache_get($type,$key) {
* returns number of bytes written (something 'true') or false ...
* depending of the success of the function
*/
function phorum_cache_put($type,$key,$data,$ttl=PHORUM_CACHE_DEFAULT_TTL) {
function phorum_cache_put($type,$key,$data,$ttl=PHORUM_CACHE_DEFAULT_TTL,$version=NULL) {

$ret=apc_store($type."_".$key, $data, $ttl);
$ret=apc_store($type."_".$key, array($data,$version), $ttl);
return $ret;
}

Expand Down
101 changes: 58 additions & 43 deletions include/cache/file.php
Expand Up @@ -32,59 +32,74 @@
$PHORUM['real_cache']=$PHORUM['cache']."/".md5(__FILE__);

/*
* This function returns the cached data for the given key
* or NULL if no data is cached for this key
* This function returns the cached data for the given key(s)
* or NULL if no data is cached.
*/
function phorum_cache_get($type,$key) {
function phorum_cache_get($type,$key,$version=NULL) {

$partpath=$GLOBALS['PHORUM']['real_cache']."/".$type;

if(is_array($key)) {
$ret=array();
foreach($key as $realkey) {
$path=$partpath."/".wordwrap(md5($realkey), PHORUM_CACHE_SPLIT, "/", true)."/data.php";
if(file_exists($path)){
$retval=unserialize(file_get_contents($path));
// the data is: array($ttl_time,$data)
if($retval[0] < time()) { // timeout
unlink($path);
} else {
$ret[$realkey]=$retval[1];
}
unset($retval);
}
}
} else {
$path=$partpath."/".wordwrap(md5($key), PHORUM_CACHE_SPLIT, "/", true)."/data.php";
if(!file_exists($path)){
$ret=NULL;
} else {
$ret=unserialize(file_get_contents($path));
// the data is: array($ttl_time,$data)
if($ret[0] < time()) { // timeout
$ret=NULL;
unlink($path);
} else {
$ret=$ret[1];
}
}
}


if(is_array($ret) && count($ret) == 0) {
$ret=NULL;
}
$partpath=$GLOBALS['PHORUM']['real_cache']."/".$type;

if(is_array($key)) {
$ret=array();
foreach($key as $realkey) {
$path=$partpath."/".wordwrap(md5($realkey), PHORUM_CACHE_SPLIT, "/", true)."/data.php";
if(file_exists($path)) {
// the data is: array($ttl_time,$data,$version)
// $version might not be set.
$retval=unserialize(file_get_contents($path));

// timeout?
if($retval[0] < time()) {
unlink($path);
// version expired?
} elseif ($version != NULL &&
(!isset($retval[2]) || $retval[2] != $version)) {
unlink($path);
} else {
$ret[$realkey]=$retval[1];
}

unset($retval);
}
}

if(count($ret) == 0) $ret = NULL;

} else {
$path=$partpath."/".wordwrap(md5($key), PHORUM_CACHE_SPLIT, "/", true)."/data.php";
if(!file_exists($path)){
$ret=NULL;
} else {
// the data is: array($ttl_time,$data,$version)
// $version might not be set.
$retval=unserialize(file_get_contents($path));

// timeout?
if($retval[0] < time()) {
$ret = NULL;
unlink($path);
// version expired?
} elseif ($version != NULL &&
(!isset($retval[2]) || $retval[2]<$version)) {
$ret = NULL;
unlink($path);
} else {
$ret = $retval[1];
}

unset($retval);
}
}

return $ret;

}

/*
* Puts some data into the cache
* returns number of bytes written (something 'true') or false ...
* depending of the success of the function
*/
function phorum_cache_put($type,$key,$data,$ttl=PHORUM_CACHE_DEFAULT_TTL) {
function phorum_cache_put($type,$key,$data,$ttl=PHORUM_CACHE_DEFAULT_TTL,$version = NULL) {

$path=$GLOBALS['PHORUM']['real_cache']."/$type/".wordwrap(md5($key), PHORUM_CACHE_SPLIT, "/", true);
if(!file_exists($path)){
Expand All @@ -93,7 +108,7 @@ function phorum_cache_put($type,$key,$data,$ttl=PHORUM_CACHE_DEFAULT_TTL) {
$file=$path."/data.php";
$ttl_time=time()+$ttl;
$fp=fopen($file,"w");
$ret=fwrite($fp,serialize(array($ttl_time,$data)));
$ret=fwrite($fp,serialize(array($ttl_time,$data,$version)));
fclose($fp);

return $ret;
Expand Down
14 changes: 8 additions & 6 deletions include/cache/memcached.php
Expand Up @@ -35,7 +35,7 @@
* This function returns the cached data for the given key
* or NULL if no data is cached for this key
*/
function phorum_cache_get($type,$key) {
function phorum_cache_get($type,$key,$version=NULL) {
if(is_array($key)) {
$getkey=array();
foreach($key as $realkey) {
Expand All @@ -51,8 +51,11 @@ function phorum_cache_get($type,$key) {
if(is_array($getkey)) {
$typelen=(strlen($type)+1);
foreach($ret as $retkey => $retdata) {
$ret[substr($retkey,$typelen)]=$retdata;
unset($ret[$retkey]);
if ($version == NULL ||
($retdata[1] != NULL && $retdata[1] == $version)) {
$ret[substr($retkey,$typelen)]=$retdata;
}
unset($ret[$retkey]);
}
}
if($ret === false || (is_array($ret) && count($ret) == 0))
Expand All @@ -67,9 +70,8 @@ function phorum_cache_get($type,$key) {
* returns number of bytes written (something 'true') or false ...
* depending of the success of the function
*/
function phorum_cache_put($type,$key,$data,$ttl=PHORUM_CACHE_DEFAULT_TTL) {

$ret=$GLOBALS['PHORUM']['memcache_obj']->set($type."_".$key, $data, 0, $ttl);
function phorum_cache_put($type,$key,$data,$ttl=PHORUM_CACHE_DEFAULT_TTL,$version=NULL) {
$ret=$GLOBALS['PHORUM']['memcache_obj']->set($type."_".$key, array($data,$version), 0, $ttl);
return $ret;
}

Expand Down
6 changes: 3 additions & 3 deletions include/controlcenter/subthreads.php
Expand Up @@ -126,13 +126,13 @@
if (! isset($PHORUM['user']['newinfo'][$forum_id])) {
$PHORUM['user']['newinfo'][$forum_id] = null;
if ($PHORUM['cache_newflags']) {
$newflagkey = $forum_id."-".$forums[$forum_id]['cache_version']."-".$PHORUM['user']['user_id'];
$PHORUM['user']['newinfo'][$forum_id] = phorum_cache_get('newflags',$newflagkey);
$newflagkey = $forum_id."-".$PHORUM['user']['user_id'];
$PHORUM['user']['newinfo'][$forum_id] = phorum_cache_get('newflags',$newflagkey,$forums[$forum_id]['cache_version']);
}
if ($PHORUM['user']['newinfo'][$forum_id] == null) {
$PHORUM['user']['newinfo'][$forum_id] = phorum_db_newflag_get_flags($forum_id);
if($PHORUM['cache_newflags']) {
phorum_cache_put('newflags',$newflagkey,$PHORUM['user']['newinfo'][$forum_id],86400);
phorum_cache_put('newflags',$newflagkey,$PHORUM['user']['newinfo'][$forum_id],86400,$forums[$forum_id]['cache_version']);
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions include/index_new.php
Expand Up @@ -63,7 +63,7 @@

// get newflag count for the vroot
// this is the announcement count
// TODO this needs caching too.
// TODO: cache this too?
list($vroot_new_messages, $vroot_new_threads) = phorum_db_newflag_get_unread_count($PHORUM["vroot"]);

foreach( $folders as $folder_key=>$folder_id ) {
Expand Down Expand Up @@ -100,14 +100,14 @@

$newflagcounts = null;
if($PHORUM['cache_newflags']) {
$newflagkey = $forum["forum_id"]."-".$forum['cache_version']."-".$PHORUM['user']['user_id'];
$newflagcounts = phorum_cache_get('newflags_index',$newflagkey);
$newflagkey = $forum["forum_id"]."-".$PHORUM['user']['user_id'];
$newflagcounts = phorum_cache_get('newflags_index',$newflagkey,$forum['cache_version']);
}

if($newflagcounts == null) {
$newflagcounts = phorum_db_newflag_get_unread_count($forum["forum_id"]);
if($PHORUM['cache_newflags']) {
phorum_cache_put('newflags_index',$newflagkey,$newflagcounts,86400);
phorum_cache_put('newflags_index',$newflagkey,$newflagcounts,86400,$forum['cache_version']);
}
}

Expand Down
2 changes: 1 addition & 1 deletion index.php
Expand Up @@ -33,7 +33,7 @@
unset($PHORUM['user']['newinfo']);
phorum_db_newflag_allread($PHORUM["forum_id"]);
if($PHORUM['cache_newflags']) {
$newflagkey = $PHORUM["forum_id"]."-".$PHORUM['cache_version']."-".$PHORUM['user']['user_id'];
$newflagkey = $PHORUM["forum_id"]."-".$PHORUM['user']['user_id'];
phorum_cache_remove('newflags', $newflagkey);
phorum_cache_remove('newflags_index', $newflagkey);
}
Expand Down
6 changes: 3 additions & 3 deletions list.php
Expand Up @@ -45,7 +45,7 @@
exit();
}

$newflagkey = $PHORUM["forum_id"]."-".$PHORUM['cache_version']."-".$PHORUM['user']['user_id'];
$newflagkey = $PHORUM["forum_id"]."-".$PHORUM['user']['user_id'];

// check for markread
if (!empty($PHORUM["args"][1]) && $PHORUM["args"][1] == 'markread' && $PHORUM["DATA"]["LOGGEDIN"]){
Expand All @@ -70,13 +70,13 @@
$PHORUM['user']['newinfo'] = null;

if($PHORUM['cache_newflags']) {
$PHORUM['user']['newinfo']=phorum_cache_get('newflags',$newflagkey);
$PHORUM['user']['newinfo']=phorum_cache_get('newflags',$newflagkey,$PHORUM['cache_version']);
}

if($PHORUM['user']['newinfo'] == null) {
$PHORUM['user']['newinfo']=phorum_db_newflag_get_flags();
if($PHORUM['cache_newflags']) {
phorum_cache_put('newflags',$newflagkey,$PHORUM['user']['newinfo'],86400);
phorum_cache_put('newflags',$newflagkey,$PHORUM['user']['newinfo'],86400,$PHORUM['cache_version']);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions read.php
Expand Up @@ -42,20 +42,20 @@
exit();
}

$newflagkey = $PHORUM["forum_id"]."-".$PHORUM['cache_version']."-".$PHORUM['user']['user_id'];
$newflagkey = $PHORUM["forum_id"]."-".$PHORUM['user']['user_id'];

if ($PHORUM["DATA"]["LOGGEDIN"]) { // reading newflags in

$PHORUM['user']['newinfo'] = null;

if($PHORUM['cache_newflags']) {
$PHORUM['user']['newinfo']=phorum_cache_get('newflags',$newflagkey);
$PHORUM['user']['newinfo']=phorum_cache_get('newflags',$newflagkey,$PHORUM['cache_version']);
}

if($PHORUM['user']['newinfo'] == null) {
$PHORUM['user']['newinfo']=phorum_db_newflag_get_flags();
if($PHORUM['cache_newflags']) {
phorum_cache_put('newflags',$newflagkey,$PHORUM['user']['newinfo'],86400);
phorum_cache_put('newflags',$newflagkey,$PHORUM['user']['newinfo'],86400,$PHORUM['cache_version']);
}
}
}
Expand Down

0 comments on commit 3034b4d

Please sign in to comment.