tobias74 / reverse-geocoder-cache
缓存PHP中的反向地理编码结果
v0.3.1
2020-06-19 18:31 UTC
Requires
- php: >=5.3.0
Requires (Dev)
- phpunit/phpunit: ^9
This package is auto-updated.
Last update: 2024-09-26 20:08:18 UTC
README
反向地理编码结果的缓存
这个“反向地理编码缓存”可用于缓存像谷歌地点或时区这样的反向地理编码服务的查询结果,从而减少对这些服务的请求次数。可以通过设置内部使用的键大小动态调整缓存的精度。键大小决定了结果将被放置的瓦片大小。较大的键大小将导致较低的精度。较小的键大小将在提高精度的同时使缓存消耗更多的内存。
示例:当键大小为1000米时,请求给定纬度-经度的地址很可能会返回错误的地址,因为在此1000米半径内的任何先前针对纬度-经度组合的请求已经写入相应的瓦片。
error_reporting(E_ALL); date_default_timezone_set('Europe/Berlin'); spl_autoload_register(function ($class) { $prefix = 'ReverseGeocoderCache\\'; $base_dir = __DIR__ . '/../../src/'; $len = strlen($prefix); if (strncmp($prefix, $class, $len) !== 0) { return; } $relative_class = substr($class, $len); $file = $base_dir . str_replace('\\', '/', $relative_class) . '.php'; if (file_exists($file)) { require $file; } }); class RedisCacheMimic { protected $hash=array(); public function set($key, $value) { $this->hash[$key] = $value; } public function get($key) { return $this->hash[$key]; } } $cacheBackend = new RedisCacheMimic(); // caching googles places $placesProvider = new \ReverseGeocoderCache\Provider\GooglePlacesProvider(); $placesProvider->setLanguage('en'); $cacheFrontEnd = new \ReverseGeocoderCache\CacheFrontEnd(); $cacheFrontEnd->setKeySize(50); $cacheFrontEnd->setPrefix('PlacesCache_'); $cacheFrontEnd->setCacheBackend($cacheBackend); $placesClient = new \ReverseGeocoderCache\CacheClient(); $placesClient->setDataProvider($placesProvider); $placesClient->setCacheFrontEnd($cacheFrontEnd); echo '<html>'; echo '<meta charset="UTF-8">'; echo '<br>'; //Albuquerque, N.M echo $placesClient->get(35.05,-106.39); // caching googles timezone $timezonesProvider = new \ReverseGeocoderCache\Provider\GoogleTimezonesProvider(); $cacheFrontEnd = new \ReverseGeocoderCache\CacheFrontEnd(); $cacheFrontEnd->setKeySize(1000); $cacheFrontEnd->setPrefix('TimezonesCache_'); $cacheFrontEnd->setCacheBackend($cacheBackend); $timezonesClient = new \ReverseGeocoderCache\CacheClient(); $timezonesClient->setDataProvider($timezonesProvider); $timezonesClient->setCacheFrontEnd($cacheFrontEnd); echo '<html>'; echo '<meta charset="UTF-8">'; echo '<br>'; //Albuquerque, N.M echo $timezonesClient->get(35.05,-106.39);