From 54b38e531a33b2eb993278022cf3e73b0f73237d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andreas=20=C3=85kre=20Solberg?= <andreas.solberg@uninett.no>
Date: Fri, 16 Sep 2011 10:29:52 +0000
Subject: [PATCH] Add negative caching of IP to geo lookups to speed up feed
 generation

git-svn-id: https://simplesamlphp.googlecode.com/svn/trunk@2901 44740490-163a-0410-bde0-09ae8108e29a
---
 modules/discojuice/lib/Country.php | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/modules/discojuice/lib/Country.php b/modules/discojuice/lib/Country.php
index 79d8d0cd6..8de06f89b 100644
--- a/modules/discojuice/lib/Country.php
+++ b/modules/discojuice/lib/Country.php
@@ -65,7 +65,9 @@ class sspmod_discojuice_Country {
 
 		if ($this->store->exists('geo', $ip, NULL)) {
 			SimpleSAML_Logger::debug('IP Geo location (geo): Found ip [' . $ip . '] in cache.');
-			return $this->store->getValue('geo', $ip, NULL);
+			$stored =  $this->store->getValue('geo', $ip, NULL);
+			if ($stored === NULL) throw new Exception('Got negative cache for this IP');
+			return $stored;
 		}
 		
 		SimpleSAML_Logger::debug('Lookup IP');
@@ -75,6 +77,11 @@ class sspmod_discojuice_Country {
 		$data = json_decode($rawdata, TRUE);
 		if (empty($data)) throw new Exception('Error decoding response from looking up IP geo location for [' . $ip . ']');
 		
+		if (empty($data['longitude'])) {
+			$this->store->set('geo', $ip, NULL, NULL);
+		}
+		
+		
 		if (empty($data['longitude'])) throw new Exception('Could not get longitude from IP lookup');
 		if (empty($data['latitude'])) throw new Exception( 'Could not get latitude from IP lookup');
 		
-- 
GitLab