Strange Behavior Object Serialization and Caching

I am facing a problem that I cannot seem to narrow down. In a Zend Framework application, I use Zend Cache to cache the tide and weather data contained in a custom Response object. The point at which the data is initially created everything works fine. I am serializing it and caching it. Then when I hit refresh and fetch data from cache I get the following error:

Message: The string could not be parsed as XML

Stack trace:

0 / home / cillosis / mysites / tidely / application / views / scripts / tides / location.phtml (38): SimpleXMLElement → __ construct ('')

1 / home / cillosis / mysites / tidely / library / Zend / View.php (108): enable ('/ home / cillosis / ...')

2 / home / cillosis / mysites / tidely / library / Zend / View / Abstract.php (888): Zend_View → _ run ('/ home / cillosis / ...')

...

This happens in my view, where I access the XML contained in my normal "response object" with this:

<div class="data-box">

<h3>Current Weather</h3>
<hr>
<?php

// *** THIS IS LINE 38  ***                         
$weather_XML = new SimpleXMLElement($this->response->_weatherdata->weatherResults);
$params = $weather_XML->data->parameters;

$img_path = $params->{'conditions-icon'}->{'icon-link'};

echo("<img src='".$img_path."'>");

...

      

Here is a dump of the corresponding part of the object on first launch (before the cache):

["_weatherdata"]=>
  object(Tidely_WeatherData)#79 (6) {
    ["weatherResults"]=>
    string(6399) "<?xml version="1.0"?>
       <dwml version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:noNamespaceSchemaLocation="http://graphical.weather.gov/xml/DWMLgen/schema/DWML.xsd">
          ...
       </dwml> "

      

Where you see "...", there is a lot of other XML data. The XML comes from the NWS (National Weather Service) API and I run it through the XML validation engine and it shows no errors. Problems arise as soon as I serialize my object and cache it. This is how I set up the Zend Cache setup:

// Setup caching
$frontendOptions = array('lifeTime' => 30, 'automatic_seralization' => false);
$backendOptions = array('cache_dir' => '../application/cache');
$this->_cache = Zend_Cache::factory('Core', 'File', $frontendOptions, $backendOptions);

      

I am doing caching and searching with this:

// Assign cache id
// Example query: "Chesapeake Bay Virginia"
$cache_id = 'request_results_' . $this->response->_querydata->query);
$cache_id = str_replace(' ', '_', $cache_id);
$cache_id = str_replace('-', '_', $cache_id);

// Check cache for this query
if ( ($results = $this->_cache->load($cache_id)) === false)
{
   // Cache not found, process request and generate response
   ...

   // Cache request output for specific query
   $serialized_data = serialize($this->response);
   $this->_cache->save($serialized_data, $cache_id);

   // Return processed request results
   return $this->response;
}
else
{
   // Return results from cache
   return unserialize($results);
}

      

As soon as I make a request and it caches it, the object $this->response

is displayed like this:

object (Tidely_ResponseData) # 65 (7) {["_querydata"] => object (Tidely_QueryData) # 66 (9) {["query"] => string (40) "bayou-la-batre-mississippi- sonic alabama" ["source"] => string (15) "TidesController" ["request_type"] => string (3) "url" ["url_components"] => array (2) {["state"] => string (7 ) "alabama" ["location"] => string (32) "bayou-la-batre-mississippi-sound"} ["return_type"] => string (4) "JSON" ["time_range"] => array ( 2) {["start"] => object (DateTime) # 64 (3) {["date"] => string (19) "2012-04-05 18:06:51" ["timezone_type"] => int (3) ["timezone"] =>string (3) "UTC"} ["end"] => object (DateTime) # 63 (3) {["date"] => string (19) "2012-04-06 18:06:51" [" timezone_type "] => int (3) [" timezone "] => string (3)" UTC "}} [" hasError "] => bool (false) [" userMessage "] => NULL [" logMessage "] = > NULL} ["_locationdata"] => object (Tidely_LocationData) # 80 (31) {["location_id"] => string (5) "18921" ["station_name"] => string (33) "Bayou La Batre, Mississippi Sound "[" station_url_name "] => string (32)" bayou-la-batre-mississi ppi-sound "[" station_type "] => string (11)" Subordinate "[" station_id "] => string (7 ) "8739051"["station_lat"] => string (8) "+30.3717" ["station_lon"] => string (8) "-88.2750" ["station_height_offset_high"] => string (5) "* 1.23" ["station_height_offset_low"] => string (5) "* 1.23" ["station_time_offset_high"] => string (3) "112" ["station_time_offset_low"] => line (2) "74" ["station_ref_station_id"] => line (7) " 8760551 "[" state_id "] => string (1)" 1 "[" state_name "] => string (7)" Alabama "[" state_url_name "] => string (7)" alabama "[" timezone "] = > string (2) "-6" ["dst"] => string (1) "1" ["city_name"] =>string (14) "Bayou La Batre" ["city_zip"] => string (5) "36509" ["city_lat"] => string (7) "30.4014" ["city_lon"] => string (8) "- 88.2467 "[" NeighboringStations "] => string (321)" a: 10: {i: 8739051; d: 2.6562027890531361; s: 7: "TEC4393"; d: 8.0529290086617618; i: 8740448; d: 11.564338218805837; i : 8735180; d: 14.637787569358004; i: 8741196; d: +17.602722038773138; i: 8734635; d: 17.824002491920826; i: 8733810; d: 19.474837234444919; i: 8737048; d: 24.4 14028460259932 : 8742221; d: 27.466706516499691; i: 8731952; d: 31.251405048051701;} "[" city_distance "] => string (15)" 2.6562027890531 "[" multipleResults "] => string (0)" "[" multipleCount "] = >int (0) ["hasMultiple"] => bool (false) ["showStates"] => bool (false) ["showLocations"] => bool (false) ["hasError"] => bool (false) [" userMessage "] => NULL [" logMessage "] => NULL} [" _tidedata "] => object (Tidely_TideData) # 68 (6) {[" tideResults "] => NULL [" queryStart "] => NULL [" queryEnd "] => NULL [" hasError "] => bool (false) [" userMessage "] => NULL [" logMessage "] => NULL} [" _weatherdata "] => object (Tidely_WeatherData) # 79 (6) {["weatherResults"] => string (6399) "weather forecast 2012-04-05T18: 06: 57Zbool (false) ["showLocations"] => bool (false) ["hasError"] => bool (false) ["userMessage"] => NULL ["logMessage"] => NULL} ["_tidedata"] => object (Tidely_TideData) # 68 (6) {["tideResults"] => NULL ["queryStart"] => NULL ["queryEnd"] => NULL ["hasError"] => bool (false) ["userMessage"] => NULL ["logMessage"] => NULL} ["_weatherdata"] => object (Tidely_WeatherData) # 79 (6) {["weatherResults"] => string (6399) "weather forecast 2012-04-05T18: 06 : 57Zbool (false) ["showLocations"] => bool (false) ["hasError"] => bool (false) ["userMessage"] => NULL ["logMessage"] => NULL} ["_tidedata"] => object (Tidely_TideData) # 68 (6) {["tideResults"] => NULL ["queryStart"] => NULL ["queryEnd"] => NULL ["hasError"] => bool (false) ["userMessage"] => NULL ["logMessage"] => NULL} ["_weatherdata"] => object (Tidely_WeatherData) # 79 (6) {["weatherResults"] => string (6399) "weather forecast 2012-04-05T18: 06 : 57ZNULL} ["_tidedata"] => object (Tidely_TideData) # 68 (6) {["tideResults"] => NULL ["queryStart"] => NULL ["queryEnd"] => NULL ["hasError"] => bool (false) ["userMessage"] => NULL ["logMessage"] => NULL} ["_weatherdata"] => object (Tidely_WeatherData) # 79 (6) {["weatherResults"] => string (6399) " weather forecast 2012-04-05T18: 06: 57ZNULL} ["_tidedata"] => object (Tidely_TideData) # 68 (6) {["tideResults"] => NULL ["queryStart"] => NULL ["queryEnd"] => NULL ["hasError"] => bool (false) ["userMessage"] => NULL ["logMessage"] => NULL} ["_weatherdata"] => object (Tidely_WeatherData) # 79 (6) {["weatherResults"] => string (6399) " weather forecast 2012-04-05T18: 06: 57Zobject (Tidely_WeatherData) # 79 (6) {["weatherResults"] => string (6399) "weather forecast 2012-04-05T18: 06: 57Zobject (Tidely_WeatherData) # 79 (6) {["weatherResults"] => string (6399) "weather forecast 2012-04-05T18: 06: 57Zhttp://graphical.weather.gov/xml/ Meteorological Development Laboratory Production Formation Laboratory http://www.nws.noaa.gov/disclaimer.html http://www.weather.gov/ http://www.weather .gov / images / xml_logo.gif http://www.weather.gov/feedback.php point1 http://forecast.weather.gov/MapClick.php?textField1=30.37&textField2=-88.28k-p24h-n1-1 2012-04-05T08: 00: 00-05: 00 2012-04-05T20: 00: 00-05: 00 k-p24h-n1-2 2012-04-05T20: 00: 00- 05: 00 2012-04-06T09: 00: 00-05: 00 k-p3h-n5-3 2012-04-05T13: 00: 00-05: 00 2012-04-05T16: 00: 00-05: 00 2012 -04-05T19: 00: 00-05: 00 2012- 04-05T22: 00: 00-05: 00 2012-04-06T01: 00: 00-05: 00 Daily maximum temperature 81 Daily minimum temperature 63 Temperature 79 79 74 69 67 Dewpoint temperature 66 65 64 64 61 Wind speed 12 11 11 13 14 Wind direction 240 260 280 300 330 Relative humidity 64 63 71 84 81 Weather conditions, coverage and intensity Icons http://forecast.weather.gov/images/ wtf / tsra60.jpg http://forecast.weather.gov/images/wtf/tsra60.jpg http://forecast.weather.gov/images/wtf/scttsra60.jpghttp://forecast.weather.gov/images/wtf/nscttsra20.jpg http://forecast.weather.gov/images/wtf/nsct.jpg "[" queryStart "] => float (1333649211.6989) [" queryEnd " ] => float (1333649217.2038) ["hasError"] => bool (false) ["userMessage"] => NULL ["logMessage"] => NULL} ["h asError"] => bool (false) ["userMessage "] => NULL [" logMessage "] => NULL}

There is XML in the "weatherResults" section, when I look at the Source I don't copy and paste it because it is quite long. This is what happens when I hit refresh and uses a cached copy of the above object:

object (Tidely_ResponseData) # 65 (7) {["_querydata"] => object (Tidely_QueryData) # 66 (9) {["query"] => string (40) "bayou-la-batre-mississippi- sonic alabama" ["source"] => string (15) "TidesController" ["request_type"] => string (3) "url" ["url_components"] => array (2) {["state"] => string (7 ) "alabama" ["location"] => string (32) "bayou-la-batre-mississippi-sound"} ["return_type"] => string (4) "JSON" ["time_range"] => array ( 2) {["start"] => object (DateTime) # 64 (3) {["date"] => string (19) "2012-04-05 18:10:30" ["timezone_type"] => int (3) ["timezone"] =>string (3) "UTC"} ["end"] => object (DateTime) # 63 (3) {["date"] => string (19) "2012-04-06 18:10:30" [" timezone_type "] => int (3) [" timezone "] => string (3)" UTC "}} [" hasError "] => bool (false) [" userMessage "] => NULL [" logMessage "] = > NULL} ["_locationdata"] => object (Tidely_LocationData) # 67 (31) {["location_id"] => string (0) "[" station_name "] => string (0)" "[" station_url_name "] => string (0) "" ["station_type"] => string (0) "" ["station_id"] => string (0) "" ["station_lat"] => string (0) "[" station_lon " ] =>string (0) "[" station_height_offset_high "] => string (0)" ["station_height_offset_low"] => string (0) "[" station_time_offset_high "] => string (0)" "[" station_time_offset_low "] => string (0) "" ["station_ref_station_id"] => string (0) "" ["state_id"] => string (0) "[" state_name "] => string (0)" ["state_url_name"] => string (0) "" ["timezone"] => string (0) "[" dst "] => string (0)" "[" city_name "] => string (0)" "[" city_zip "] => string (0) "" ["city_lat"] => string (0) "" ["city_lon"] => string (0) "" ["neighboringStations "] => string (0)" ["city_distance"] => string (0) "[" multipleResults "] => string (0)" ["multipleCount"] => int (0) ["hasMultiple"] => bool (false) ["showStates"] => bool (false) ["showLocations"] => bool (false) ["hasError"] => bool (false) ["userMessage"] => NULL ["logMessage "] => NULL} [" _tidedata "] => object (Tidely_TideData) # 68 (6) {[" tideResults "] => NULL [" queryStart "] => NULL [" queryEnd "] => NULL [" hasError "] => bool (false) [" userMessage "] => NULL [" logMessage "] => NULL} [" _weatherdata "] =>object (Tidely_WeatherData) # 69 (6) {["weatherResults"] => NULL ["queryStart"] => NULL ["queryEnd"] => NULL ["hasError"] => bool (false) ["userMessage"] => NULL ["logMessage"] => NULL} ["hasError"] => bool (false) ["userMessage"] => NULL ["logMessage"] => NULL}

In the process of serializing / unserializing and saving the cache, it somehow sets all strings to empty or NULL! So, of course, I am getting an XML error because it is empty.

Anyone else running into object caching issues with Zend Cache (or any other caching library for that matter)? Is it serialization related? Is there a cache setting I am missing? Sorry for the lengthy explanation and in advance for your help!

+3


source to share


1 answer


PHP resources cannot be serialized , so you cannot serialize your object (which serializes a SimpleXML object indirectly).

Before serialization, you need to convert your object to a well-formed XML string ( SimpleXMLElement::asXML()

), and once your object is unserialized, convert your string to a SimpleXML ( simplexml_load_string()

) object .

See here for more information. This question might be helpful too.




Edit after new details : With these new elements in your question, I understand that your problem is not related to the serialized SimpleXML object. Do you have a reason not to use it automatic_serialization

? (with " i " by the way, I guess you were wrong) instead of Zend_Cache automatically serializing / unserializing your object.




Edit # 2 : Read comments from cillosis for a final solution.

+6


source







All Articles