Adding cache mechanism for bbcode. Improving cache to use apc.

This commit is contained in:
Michael Vogel 2014-02-23 15:51:57 +01:00
parent f663676cca
commit 57db109474
2 changed files with 84 additions and 63 deletions

View File

@ -534,77 +534,85 @@ function GetProfileUsername($profile, $username) {
} }
function bb_RemovePictureLinks($match) { function bb_RemovePictureLinks($match) {
$ch = @curl_init($match[1]); $text = Cache::get($match[1]);
@curl_setopt($ch, CURLOPT_NOBODY, true);
@curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
@curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; Friendica)");
@curl_exec($ch);
$curl_info = @curl_getinfo($ch);
if (substr($curl_info["content_type"], 0, 6) == "image/") if(is_null($text)){
$text = "[url=".$match[1]."]".$match[1]."[/url]"; $ch = @curl_init($match[1]);
else { @curl_setopt($ch, CURLOPT_NOBODY, true);
$text = "[url=".$match[2]."]".$match[2]."[/url]"; @curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
@curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; Friendica)");
@curl_exec($ch);
$curl_info = @curl_getinfo($ch);
// if its not a picture then look if its a page that contains a picture link if (substr($curl_info["content_type"], 0, 6) == "image/")
require_once("include/network.php"); $text = "[url=".$match[1]."]".$match[1]."[/url]";
else {
$text = "[url=".$match[2]."]".$match[2]."[/url]";
$body = fetch_url($match[1]); // if its not a picture then look if its a page that contains a picture link
require_once("include/network.php");
$doc = new DOMDocument(); $body = fetch_url($match[1]);
@$doc->loadHTML($body);
$xpath = new DomXPath($doc);
$list = $xpath->query("//meta[@name]");
foreach ($list as $node) {
$attr = array();
if ($node->attributes->length) $doc = new DOMDocument();
foreach ($node->attributes as $attribute) @$doc->loadHTML($body);
$attr[$attribute->name] = $attribute->value; $xpath = new DomXPath($doc);
$list = $xpath->query("//meta[@name]");
foreach ($list as $node) {
$attr = array();
if (strtolower($attr["name"]) == "twitter:image") if ($node->attributes->length)
$text = "[url=".$attr["content"]."]".$attr["content"]."[/url]"; foreach ($node->attributes as $attribute)
$attr[$attribute->name] = $attribute->value;
if (strtolower($attr["name"]) == "twitter:image")
$text = "[url=".$attr["content"]."]".$attr["content"]."[/url]";
}
} }
Cache::set($match[1],$text);
} }
return($text); return($text);
} }
function bb_CleanPictureLinksSub($match) { function bb_CleanPictureLinksSub($match) {
$ch = @curl_init($match[1]); $text = Cache::get($match[1]);
@curl_setopt($ch, CURLOPT_NOBODY, true);
@curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
@curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; Friendica)");
@curl_exec($ch);
$curl_info = @curl_getinfo($ch);
// if its a link to a picture then embed this picture if(is_null($text)){
if (substr($curl_info["content_type"], 0, 6) == "image/") $ch = @curl_init($match[1]);
$text = "[img]".$match[1]."[/img]"; @curl_setopt($ch, CURLOPT_NOBODY, true);
else { @curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$text = "[img]".$match[2]."[/img]"; @curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; Friendica)");
@curl_exec($ch);
$curl_info = @curl_getinfo($ch);
// if its not a picture then look if its a page that contains a picture link // if its a link to a picture then embed this picture
require_once("include/network.php"); if (substr($curl_info["content_type"], 0, 6) == "image/")
$text = "[img]".$match[1]."[/img]";
else {
$text = "[img]".$match[2]."[/img]";
$body = fetch_url($match[1]); // if its not a picture then look if its a page that contains a picture link
require_once("include/network.php");
$doc = new DOMDocument(); $body = fetch_url($match[1]);
@$doc->loadHTML($body);
$xpath = new DomXPath($doc);
$list = $xpath->query("//meta[@name]");
foreach ($list as $node) {
$attr = array();
if ($node->attributes->length) $doc = new DOMDocument();
foreach ($node->attributes as $attribute) @$doc->loadHTML($body);
$attr[$attribute->name] = $attribute->value; $xpath = new DomXPath($doc);
$list = $xpath->query("//meta[@name]");
foreach ($list as $node) {
$attr = array();
if (strtolower($attr["name"]) == "twitter:image") if ($node->attributes->length)
$text = "[img]".$attr["content"]."[/img]"; foreach ($node->attributes as $attribute)
$attr[$attribute->name] = $attribute->value;
if (strtolower($attr["name"]) == "twitter:image")
$text = "[img]".$attr["content"]."[/img]";
}
} }
Cache::set($match[1],$text);
} }
return($text); return($text);
} }

View File

@ -2,29 +2,42 @@
/** /**
* cache api * cache api
*/ */
class Cache { class Cache {
public static function get($key){ public static function get($key) {
if (function_exists("apc_fetch") AND function_exists("apc_exists"))
if (apc_exists($key))
return(apc_fetch($key));
$r = q("SELECT `v` FROM `cache` WHERE `k`='%s' limit 1", $r = q("SELECT `v` FROM `cache` WHERE `k`='%s' limit 1",
dbesc($key) dbesc($key)
); );
if (count($r)) return $r[0]['v']; if (count($r)) {
if (function_exists("apc_store"))
apc_store($key, $r[0]['v'], 600);
return $r[0]['v'];
}
return null; return null;
} }
public static function set($key,$value) { public static function set($key,$value) {
q("REPLACE INTO `cache` (`k`,`v`,`updated`) VALUES ('%s','%s','%s')", q("REPLACE INTO `cache` (`k`,`v`,`updated`) VALUES ('%s','%s','%s')",
dbesc($key), dbesc($key),
dbesc($value), dbesc($value),
dbesc(datetime_convert())); dbesc(datetime_convert()));
if (function_exists("apc_store"))
apc_store($key, $value, 600);
} }
/* /*
* *
* Leaving this legacy code temporaily to see how REPLACE fares * Leaving this legacy code temporaily to see how REPLACE fares
* as opposed to non-atomic checks when faced with fast moving key duplication. * as opposed to non-atomic checks when faced with fast moving key duplication.
* As a MySQL extension it isn't portable, but we're not yet very portable. * As a MySQL extension it isn't portable, but we're not yet very portable.
*/ */
@ -48,11 +61,11 @@
* } * }
*/ */
public static function clear(){ public static function clear(){
q("DELETE FROM `cache` WHERE `updated` < '%s'", q("DELETE FROM `cache` WHERE `updated` < '%s'",
dbesc(datetime_convert('UTC','UTC',"now - 30 days"))); dbesc(datetime_convert('UTC','UTC',"now - 30 days")));
} }
} }