forked from GNUsocial/gnu-social
		
	use caching in geonames plugin
This commit is contained in:
		@@ -63,6 +63,14 @@ class GeonamesPlugin extends Plugin
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    function onLocationFromName($name, $language, &$location)
 | 
					    function onLocationFromName($name, $language, &$location)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
 | 
					        $loc = $this->getCache(array('name' => $name,
 | 
				
			||||||
 | 
					                                     'language' => $language));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (!empty($loc)) {
 | 
				
			||||||
 | 
					            $location = $loc;
 | 
				
			||||||
 | 
					            return false;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $client = HTTPClient::start();
 | 
					        $client = HTTPClient::start();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // XXX: break down a name by commas, narrow by each
 | 
					        // XXX: break down a name by commas, narrow by each
 | 
				
			||||||
@@ -87,6 +95,10 @@ class GeonamesPlugin extends Plugin
 | 
				
			|||||||
                $location->location_id      = $n->geonameId;
 | 
					                $location->location_id      = $n->geonameId;
 | 
				
			||||||
                $location->location_ns      = self::LOCATION_NS;
 | 
					                $location->location_ns      = self::LOCATION_NS;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                $this->setCache(array('name' => $name,
 | 
				
			||||||
 | 
					                                     'language' => $language),
 | 
				
			||||||
 | 
					                                $location);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                // handled, don't continue processing!
 | 
					                // handled, don't continue processing!
 | 
				
			||||||
                return false;
 | 
					                return false;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
@@ -114,6 +126,13 @@ class GeonamesPlugin extends Plugin
 | 
				
			|||||||
            return true;
 | 
					            return true;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $loc = $this->getCache(array('id' => $id));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (!empty($loc)) {
 | 
				
			||||||
 | 
					            $location = $loc;
 | 
				
			||||||
 | 
					            return false;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $client = HTTPClient::start();
 | 
					        $client = HTTPClient::start();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $str = http_build_query(array('geonameId' => $id,
 | 
					        $str = http_build_query(array('geonameId' => $id,
 | 
				
			||||||
@@ -148,6 +167,9 @@ class GeonamesPlugin extends Plugin
 | 
				
			|||||||
                $location->lat              = $last->lat;
 | 
					                $location->lat              = $last->lat;
 | 
				
			||||||
                $location->lon              = $last->lng;
 | 
					                $location->lon              = $last->lng;
 | 
				
			||||||
                $location->names[$language] = implode(', ', array_reverse($parts));
 | 
					                $location->names[$language] = implode(', ', array_reverse($parts));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                $this->setCache(array('id' => $last->geonameId),
 | 
				
			||||||
 | 
					                                $location);
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -173,6 +195,14 @@ class GeonamesPlugin extends Plugin
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    function onLocationFromLatLon($lat, $lon, $language, &$location)
 | 
					    function onLocationFromLatLon($lat, $lon, $language, &$location)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
 | 
					        $loc = $this->getCache(array('lat' => $lat,
 | 
				
			||||||
 | 
					                                     'lon' => $lon));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (!empty($loc)) {
 | 
				
			||||||
 | 
					            $location = $loc;
 | 
				
			||||||
 | 
					            return false;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $client = HTTPClient::start();
 | 
					        $client = HTTPClient::start();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $str = http_build_query(array('lat' => $lat,
 | 
					        $str = http_build_query(array('lat' => $lat,
 | 
				
			||||||
@@ -211,6 +241,10 @@ class GeonamesPlugin extends Plugin
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
                $location->names[$language] = implode(', ', $parts);
 | 
					                $location->names[$language] = implode(', ', $parts);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                $this->setCache(array('lat' => $lat,
 | 
				
			||||||
 | 
					                                      'lon' => $lon),
 | 
				
			||||||
 | 
					                                $location);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                // Success! We handled it, so no further processing
 | 
					                // Success! We handled it, so no further processing
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                return false;
 | 
					                return false;
 | 
				
			||||||
@@ -242,9 +276,17 @@ class GeonamesPlugin extends Plugin
 | 
				
			|||||||
            return true;
 | 
					            return true;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $n = $this->getCache(array('id' => $location->location_id,
 | 
				
			||||||
 | 
					                                   'language' => $language));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (!empty($n)) {
 | 
				
			||||||
 | 
					            $name = $n;
 | 
				
			||||||
 | 
					            return false;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $client = HTTPClient::start();
 | 
					        $client = HTTPClient::start();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $str = http_build_query(array('geonameId' => $id,
 | 
					        $str = http_build_query(array('geonameId' => $location->location_id,
 | 
				
			||||||
                                      'lang' => $language));
 | 
					                                      'lang' => $language));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $result = $client->get('http://ws.geonames.org/hierarchyJSON?'.$str);
 | 
					        $result = $client->get('http://ws.geonames.org/hierarchyJSON?'.$str);
 | 
				
			||||||
@@ -271,6 +313,9 @@ class GeonamesPlugin extends Plugin
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
                if (count($parts)) {
 | 
					                if (count($parts)) {
 | 
				
			||||||
                    $name = implode(', ', array_reverse($parts));
 | 
					                    $name = implode(', ', array_reverse($parts));
 | 
				
			||||||
 | 
					                    $this->setCache(array('id' => $location->location_id,
 | 
				
			||||||
 | 
					                                          'language' => $language),
 | 
				
			||||||
 | 
					                                    $name);
 | 
				
			||||||
                    return false;
 | 
					                    return false;
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
@@ -326,4 +371,44 @@ class GeonamesPlugin extends Plugin
 | 
				
			|||||||
        // it's been filled, so don't process further.
 | 
					        // it's been filled, so don't process further.
 | 
				
			||||||
        return false;
 | 
					        return false;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    function getCache($attrs)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        $c = common_memcache();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (!$c) {
 | 
				
			||||||
 | 
					            return null;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return $c->get($this->cacheKey($attrs));
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    function setCache($attrs, $loc)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        $c = common_memcache();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (!$c) {
 | 
				
			||||||
 | 
					            return null;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $c->set($this->cacheKey($attrs), $loc);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    function clearCache($attrs)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        $c = common_memcache();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (!$c) {
 | 
				
			||||||
 | 
					            return null;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $c->delete($this->cacheKey($attrs));
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    function cacheKey($attrs)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        return common_cache_key('geonames:'.
 | 
				
			||||||
 | 
					                                implode(',', array_keys($attrs)) . ':'.
 | 
				
			||||||
 | 
					                                common_keyize(implode(',', array_values($attrs))));
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user