Skip to content

Commit

Permalink
Cache decoded content in response objects.
Browse files Browse the repository at this point in the history
This makes multiple reads less expensive.
  • Loading branch information
markstory committed Jan 30, 2013
1 parent 520bcd3 commit 1a0f475
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions lib/Cake/Network/Http/Response.php
Expand Up @@ -89,6 +89,20 @@ class Response extends Message {
*/
protected $_body;

/**
* Cached decoded XML data.
*
* @var SimpleXMLElement
*/
protected $_xml;

/**
* Cached decoded JSON data.
*
* @var SimpleXMLElement
*/
protected $_json;

/**
* Map of public => property names for __get()
*
Expand Down Expand Up @@ -312,9 +326,13 @@ public function body($parser = null) {
* @return null|array
*/
protected function _getJson() {
if (!empty($this->_json)) {
return $this->_json;
}
$data = json_decode($this->_body, true);
if ($data) {
return $data;
$this->_json = $data;
return $this->_json;
}
return null;
}
Expand All @@ -325,10 +343,14 @@ protected function _getJson() {
* @return null|SimpleXML
*/
protected function _getXml() {
if (!empty($this->_xml)) {
return $this->_xml;
}
$restore = libxml_use_internal_errors();
$data = simplexml_load_string($this->_body);
if ($data) {
return $data;
$this->_xml = $data;
return $this->_xml;
}
return null;
}
Expand Down

0 comments on commit 1a0f475

Please sign in to comment.