aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/FixMyStreet/Geocode
diff options
context:
space:
mode:
authorMatthew Somerville <matthew@mysociety.org>2012-12-06 12:22:20 +0000
committerMatthew Somerville <matthew@mysociety.org>2012-12-06 12:22:20 +0000
commit856709c08fb6a6e9c00155baf0b10b797aaa239d (patch)
tree303073fc6e1251cbe28218c3d129c3a1a2199678 /perllib/FixMyStreet/Geocode
parent698d8aed1e16c46fc20431bb48736eed70ab809e (diff)
Add caching of geocoder similar to other geocoders.
Diffstat (limited to 'perllib/FixMyStreet/Geocode')
-rw-r--r--perllib/FixMyStreet/Geocode/Zurich.pm27
1 files changed, 19 insertions, 8 deletions
diff --git a/perllib/FixMyStreet/Geocode/Zurich.pm b/perllib/FixMyStreet/Geocode/Zurich.pm
index 29c3ebdbb..83637cd4b 100644
--- a/perllib/FixMyStreet/Geocode/Zurich.pm
+++ b/perllib/FixMyStreet/Geocode/Zurich.pm
@@ -13,8 +13,11 @@
package FixMyStreet::Geocode::Zurich;
use strict;
+use Digest::MD5 qw(md5_hex);
+use File::Path ();
use Geo::Coordinates::CH1903;
use SOAP::Lite;
+use Storable;
use mySociety::Locale;
my ($soap, $method, $security);
@@ -56,16 +59,24 @@ sub string {
setup_soap();
- my $search = SOAP::Data->name('search' => $s)->type('');
- my $count = SOAP::Data->name('count' => 10)->type('');
+ my $cache_dir = FixMyStreet->config('GEO_CACHE') . 'zurich/';
+ my $cache_file = $cache_dir . md5_hex($s);
my $result;
- eval {
- $result = $soap->call($method, $security, $search, $count);
- };
- if ($@) {
- return { error => 'The geocoder appears to be down.' };
+ if (-s $cache_file) {
+ $result = retrieve($cache_file);
+ } else {
+ my $search = SOAP::Data->name('search' => $s)->type('');
+ my $count = SOAP::Data->name('count' => 10)->type('');
+ eval {
+ $result = $soap->call($method, $security, $search, $count);
+ };
+ if ($@) {
+ return { error => 'The geocoder appears to be down.' };
+ }
+ $result = $result->result;
+ File::Path::mkpath($cache_dir);
+ store $result, $cache_file;
}
- $result = $result->result;
if (!$result || !$result->{Location}) {
return { error => _('Sorry, we could not parse that location. Please try again.') };