From d92a348cf049e9caf7f45fee388ac9d6ca15fe6e Mon Sep 17 00:00:00 2001
From: Michael Vogel <icarus@dabo.de>
Date: Fri, 9 Aug 2013 18:28:50 +0200
Subject: [PATCH] - Using APC to store config values (when present) - Enabling
 interlacing for pictures

---
 include/Photo.php  |  4 ++++
 include/config.php | 43 +++++++++++++++++++++++++++++++++++++++----
 2 files changed, 43 insertions(+), 4 deletions(-)

diff --git a/include/Photo.php b/include/Photo.php
index 69b08f629..f1c9128d3 100644
--- a/include/Photo.php
+++ b/include/Photo.php
@@ -534,6 +534,9 @@ class Photo {
 
         ob_start();
 
+	// Enable interlacing
+	imageinterlace($this->image, true);
+
         switch($this->getType()){
             case "image/png":
                 $quality = get_config('system','png_quality');
@@ -676,6 +679,7 @@ function guess_image_type($filename, $fromcurl=false) {
              */
             $image = new Imagick($filename);
             $type = $image->getImageMimeType();
+            $image->setInterlaceScheme(Imagick::INTERLACE_PLANE);
         } else {
             $ext = pathinfo($filename, PATHINFO_EXTENSION);
             $types = Photo::supportedTypes();
diff --git a/include/config.php b/include/config.php
index af44f6cc2..4f44de24f 100644
--- a/include/config.php
+++ b/include/config.php
@@ -18,6 +18,9 @@
 if(! function_exists('load_config')) {
 function load_config($family) {
 	global $a;
+
+	// To-Do: How to integrate APC here?
+
 	$r = q("SELECT * FROM `config` WHERE `cat` = '%s'", dbesc($family));
 	if(count($r)) {
 		foreach($r as $rr) {
@@ -67,9 +70,16 @@ function get_config($family, $key, $instore = false) {
 	if (function_exists("apc_fetch") AND function_exists("apc_exists"))
 		if (apc_exists($family."|".$key)) {
 			$val = apc_fetch($family."|".$key);
+			//logger("APC: fetched stored value ".$family."|".$key, LOGGER_DEBUG);
 			$a->config[$family][$key] = $val;
-			return $val;
-		}
+
+			if ($val === '!<unset>!')
+				return false;
+			else
+				return $val;
+		} else
+			//logger("APC: cache miss for value ".$family."|".$key, LOGGER_DEBUG);
+
 
 	$ret = q("SELECT `v` FROM `config` WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
 		dbesc($family),
@@ -79,10 +89,19 @@ function get_config($family, $key, $instore = false) {
 		// manage array value
 		$val = (preg_match("|^a:[0-9]+:{.*}$|s", $ret[0]['v'])?unserialize( $ret[0]['v']):$ret[0]['v']);
 		$a->config[$family][$key] = $val;
+
+		// If APC is enabled then store the data there
+		if (function_exists("apc_store"))
+			apc_store($family."|".$key, $val, 600);
+
 		return $val;
 	}
 	else {
 		$a->config[$family][$key] = '!<unset>!';
+
+		// If APC is enabled then store the data there
+		if (function_exists("apc_store"))
+			apc_store($family."|".$key, '!<unset>!', 600);
 	}
 	return false;
 }}
@@ -181,9 +200,16 @@ function get_pconfig($uid,$family, $key, $instore = false) {
 	if (function_exists("apc_fetch") AND function_exists("apc_exists"))
 		if (apc_exists($uid."|".$family."|".$key)) {
 			$val = apc_fetch($uid."|".$family."|".$key);
+			//logger("APC: fetched stored value ".$uid."|".$family."|".$key, LOGGER_DEBUG);
 			$a->config[$uid][$family][$key] = $val;
-			return $val;
-		}
+
+			if ($val === '!<unset>!')
+				return false;
+			else
+				return $val;
+		} else
+			//logger("APC: cache miss for value ".$family."|".$key, LOGGER_DEBUG);
+
 
 	$ret = q("SELECT `v` FROM `pconfig` WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s' LIMIT 1",
 		intval($uid),
@@ -194,10 +220,19 @@ function get_pconfig($uid,$family, $key, $instore = false) {
 	if(count($ret)) {
 		$val = (preg_match("|^a:[0-9]+:{.*}$|s", $ret[0]['v'])?unserialize( $ret[0]['v']):$ret[0]['v']);
 		$a->config[$uid][$family][$key] = $val;
+
+		// If APC is enabled then store the data there
+		if (function_exists("apc_store"))
+			apc_store($uid."|".$family."|".$key, $val, 600);
+
 		return $val;
 	}
 	else {
 		$a->config[$uid][$family][$key] = '!<unset>!';
+
+		// If APC is enabled then store the data there
+		if (function_exists("apc_store"))
+			apc_store($uid."|".$family."|".$key, '!<unset>!', 600);
 	}
 	return false;
 }}