function _location_search_results_from_cache in Location 5
Helper function: this function is intended ONLY for use by _location_nearby_postalcodes(). It checks the cache for search-results on a given point and returns the appropriate subset if one exists.
Parameters
$lon: A floating point of the longitude coordinate of the search point
$lat : A floating point of the latitude coordinate of the search point
$distance: A floating point of the number of meters of the search radius
Return value
An array of previous search results. If a previous search result on the same point was for the same search-radius, that result is returned. If a previous search result on the same point was for a longer search-radius, a subset of that result is returned. If a previous search result on the same point was for a shorter search-radius, the cached search-result is thrown out and an empty array is returned. If no previous search-result on the same point is in the cache, an empty array.
1 call to _location_search_results_from_cache()
- _location_nearby_postalcodes in ./
location.inc - This is the main logic-level function for performing proximity postal-code searches. It calls a number of helper functions for finding postal_code results in each country, narrowing down results, formatting the returned array, and sorting the returned…
File
- ./
location.inc, line 887
Code
function _location_search_results_from_cache($lon, $lat, $distance) {
$cache_id_prefix = 'location_prox_search:' . round($lon, 3) . ':' . round($lat, 3) . ':';
$result = db_query("SELECT cid FROM {cache} WHERE cid LIKE '%s%%'", $cache_id_prefix);
if ($result_row = db_fetch_object($result)) {
// A previous search has been done on the same search point, possibily with the an equal or different
// search radius.
$cached_key_fields = explode(':', $result_row->cid);
$previous_search_radius = $cached_key_fields[3];
// If the search-radius is less than or equal to the previous search-radius, then just use
// the appropriate subset of the previous search result.
// This is very convenient since previous search results are sorted in ascending order
// by their distance from the search point.
if ($distance <= $previous_search_radius) {
$cached_search_results = cache_get($result_row->cid);
$cached_search_results = unserialize($cached_search_results->data);
// If the cached-search had the exact same search-radius, just return the entire search result's
// array from before,
// otherwise, go through the distance-sorted search results and pick them out until the distances
// of each search result start being something greater than the current search-radius
if ($distance == $previous_search_radius) {
// DEBUG: commented code is for testing/debugging purposes
//print 'POSTAL CODE SEARCH CACHING: Returning EXACT SAME of search results from before'."<br/>\n";
return $cached_search_results;
}
else {
$current_search_results = array();
foreach ($cached_search_results as $key => $cached_result) {
if ($cached_result['distance'] <= $distance) {
$current_search_results[$key] = $cached_result;
}
else {
break;
}
}
// DEBUG: commented code is for testing/debugging purposes
//print 'POSTAL CODE SEARCH CACHING: Returning SUBSET of a previous search\'s results'."<br/>\n";
return $current_search_results;
}
}
else {
// If the previous search-radius on the same point is smaller than the current search-radius,
// then delete the previous search from the cache to make way in the cache for the results of
// the current, more comprehensive search being done on the same point, but on a larger radius.
// Return an empty array to let the calling function know that it will have to do a new search.
// DEBUG: commented code is for testing/debugging purposes
//print 'POSTAL CODE SEARCH CACHING: Throwing out old search on a point because new search uses larger search-radius'."<br/>\n";
cache_clear_all($result_row->cid, 'cache');
return array();
}
}
else {
// This else-clause ties back to the first if-clause in this function.
// It executes if no search has been done on this point.
// If the {cache} table did not contain any useful cache id's, return the empty array.
// This will let the calling function know that it has to do an original search.
return array();
}
}