Browse Source

Rewritten Cache in a more proper fashion. Added md5ed cache.

pull/5/head
Etenil 15 years ago
parent
commit
5ddbbf9240
  1. 136
      lib/Cache.php
  2. 4
      lib/XMPPConnect.php
  3. 2
      lib/widgets/Friends/Friends.php
  4. 4
      lib/widgets/Profile/Profile.php

136
lib/Cache.php

@ -1,32 +1,116 @@
<?php
<?php
function movim_cache($key)
/**
* A fully-static class that deals with caching.
*/
class Cache
{
$arglist = func_get_args();
$key = $arglist[0];
$content = $arglist[1];
$user = new User();
$login = $user->getLogin();
if(!is_dir(BASE_PATH."/user/".$login."/cache"))
mkdir(BASE_PATH."/user/".$login."/cache", 0755);
if(func_num_args() == 1) {
if(file_exists(BASE_PATH."/user/".$login."/cache/".$key)) {
$content = unserialize(file_get_contents(BASE_PATH."/user/".$login."/cache/".$key));
}
if(isset($content) && $content != "")
return $content;
else
return "";
}
if(func_num_args() == 2) {
if(!file_put_contents(BASE_PATH."/user/".$login."/cache/".$key, serialize($content))) {
private static $login;
private static $ttl; // TODO
/**
* Fetches or commits an object to cache with the provided key.
*
* Prototype: Cache::handle(string $key, ...)
*
* The following fetches an object from cache.
* Cache::handle('key')
*
* This commits an object to cache.
* Cache::handle('key', $object);
*
* Several objects can be commited to cache in this manner:
* Cache::handle('key', $object1, $object2, $object3);
* And retrieved as follows:
* list($object1, $object2, $object3) = Cache::handle('key');
*/
public static function handle($key)
{
$arglist = func_get_args();
$key = $arglist[0];
// Saving the user's login.
if(self::$login == "") {
$user = new User();
self::$login = $user->getLogin();
}
if(func_num_args() == 1) {
$content = self::read_cache($key);
if(isset($content) && $content != "") {
return $content;
} else {
return false; // FALSE is better for testing.
}
}
if(func_num_args() == 2) {
return self::write_cache($key, $arglist[1]);
}
else if(func_num_args() > 2) {
// Cutting a piece of the args.
$content = array_slice($argslist, 1);
return self::write_cache($key, $content);
}
}
private static function cache_dir($file = "")
{
$cache_dir = BASE_PATH."/user/" . self::$login . "/cache";
if($file != "") {
return $cache_dir . '/' . $file;
} else {
return $cache_dir;
}
}
/**
* Serializes data in a proper fashion.
*/
private static function write_cache($key, $object)
{
$s_object = base64_encode(serialize($object));
$md5 = md5($s_object);
// Let's see if the cache's dir exists.
if(!is_dir(self::cache_dir())) {
mkdir(self::cache_dir(), 0755);
}
// OK, writing with its md5 buddy.
if(!file_put_contents(self::cache_dir($key), $s_object)
|| !file_put_contents(self::cache_dir($key.'.md5'), $md5)) {
throw new MovimException(sprintf(t("Couldn't set cache file %s"), $key));
}
}
return true; // Just in case.
}
/**
* Unserializes data.
*/
private static function read_cache($key)
{
if(!file_exists(self::cache_dir($key)) || !file_exists(self::cache_dir($key.'.md5'))) {
return false;
}
$s_object = file_get_contents(self::cache_dir($key));
$md5 = file_get_contents(self::cache_dir($key.'.md5'));
if(md5($s_object) != $md5) {
// No good. We summarily clean these files.
@unlink(self::cache_dir($key));
@unlink(self::cache_dir($key.'.md5'));
return false;
}
// All good now, unserializing and sending through.
return unserialize(base64_decode($s_object));
}
}
?>

4
lib/XMPPConnect.php

@ -197,10 +197,10 @@ class XMPPConnect
public function handle($payload) {
$evt = new EventHandler();
if(isset($payload['vCard'])) { // Holy mackerel, that's a vcard!
movim_cache("vcard".$payload["from"], $payload);
Cache::handle("vcard".$payload["from"], $payload);
$evt->runEvent('vcardreceived', $payload);
} elseif($payload['queryXmlns'] == "jabber:iq:roster") {
movim_cache("roster", $payload);
Cache::handle("roster", $payload);
$evt->runEvent('rosterreceived', $payload);
} else {
$evt->runEvent('none', var_export($payload, true));

2
lib/widgets/Friends/Friends.php

@ -104,7 +104,7 @@ class Friends extends Widget
<h3><?php echo t('Contacts');?></h3>
<div id="tinylist">
<?php echo $this->prepareRoster(movim_cache('roster')); ?>
<?php echo $this->prepareRoster(Cache::handle('roster')); ?>
</div>
</div>
<?php

4
lib/widgets/Profile/Profile.php

@ -62,10 +62,10 @@ class Profile extends Widget
<div id="avatar">
<?php
if(isset($_GET['f']))
echo $this->prepareVcard(movim_cache('vcard'.$_GET['f']));
echo $this->prepareVcard(Cache::handle('vcard'.$_GET['f']));
else {
$user = new User();
echo $this->prepareVcard(movim_cache('vcard'));
echo $this->prepareVcard(Cache::handle('vcard'));
}
?>
</div>

Loading…
Cancel
Save