Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
36624ae87a | ||
|
|
08697f2e7b | ||
|
|
2f9746c32b |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,6 +1,7 @@
|
||||
_site
|
||||
.gh-pages
|
||||
.idea
|
||||
GeoLite2-City.mmdb
|
||||
build
|
||||
composer.lock
|
||||
composer.phar
|
||||
|
||||
@@ -1,6 +1,13 @@
|
||||
CHANGELOG
|
||||
=========
|
||||
|
||||
0.8.1 (2014-09-12)
|
||||
------------------
|
||||
|
||||
* The check added to the `GeoIP2\Database\Reader` lookup methods in 0.8.0 did
|
||||
not work with the GeoIP2 City Database Subset by Continent with World
|
||||
Countries databases. This has been fixed. Fixes GitHub issue #23.
|
||||
|
||||
0.8.0 (2014-09-10)
|
||||
------------------
|
||||
|
||||
|
||||
@@ -26,7 +26,7 @@ You should now have the file `composer.phar` in your project directory.
|
||||
Run in your project root:
|
||||
|
||||
```
|
||||
php composer.phar require geoip2/geoip2:~0.8.0
|
||||
php composer.phar require geoip2/geoip2:~0.8.1
|
||||
```
|
||||
|
||||
You should now have the files `composer.json` and `composer.lock` as well as
|
||||
|
||||
26
bin/benchmark.php
Executable file
26
bin/benchmark.php
Executable file
@@ -0,0 +1,26 @@
|
||||
#!/usr/bin/env php
|
||||
|
||||
<?php
|
||||
|
||||
require_once '../vendor/autoload.php';
|
||||
|
||||
use GeoIp2\Database\Reader;
|
||||
use GeoIp2\Exception\AddressNotFoundException;
|
||||
|
||||
$reader = new Reader('GeoLite2-City.mmdb');
|
||||
$count = 40000;
|
||||
$startTime = microtime(true);
|
||||
for ($i = 0; $i < $count; $i++) {
|
||||
$ip = long2ip(rand(0, pow(2, 32) -1));
|
||||
try {
|
||||
$t = $reader->city($ip);
|
||||
} catch (AddressNotFoundException $e) {
|
||||
}
|
||||
if ($i % 1000 == 0) {
|
||||
print($i . ' ' . $ip . "\n");
|
||||
}
|
||||
}
|
||||
$endTime = microtime(true);
|
||||
|
||||
$duration = $endTime - $startTime;
|
||||
print('Requests per second: ' . $count / $duration . "\n");
|
||||
@@ -71,11 +71,7 @@ class Reader implements ProviderInterface
|
||||
*/
|
||||
public function city($ipAddress)
|
||||
{
|
||||
return $this->modelFor(
|
||||
'City',
|
||||
array('GeoLite2-City', 'GeoIP2-City'),
|
||||
$ipAddress
|
||||
);
|
||||
return $this->modelFor('City', 'City', $ipAddress);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -92,11 +88,7 @@ class Reader implements ProviderInterface
|
||||
*/
|
||||
public function country($ipAddress)
|
||||
{
|
||||
return $this->modelFor(
|
||||
'Country',
|
||||
array('GeoLite2-Country', 'GeoIP2-Country'),
|
||||
$ipAddress
|
||||
);
|
||||
return $this->modelFor('Country', 'Country', $ipAddress);
|
||||
}
|
||||
|
||||
public function connectionType($ipAddress)
|
||||
@@ -126,16 +118,9 @@ class Reader implements ProviderInterface
|
||||
);
|
||||
}
|
||||
|
||||
private function modelFor($class, $types, $ipAddress)
|
||||
private function modelFor($class, $type, $ipAddress)
|
||||
{
|
||||
if (!in_array($this->metadata()->databaseType, $types)) {
|
||||
$method = lcfirst($class);
|
||||
throw new \BadMethodCallException(
|
||||
"The $method method cannot be used to open a "
|
||||
. $this->metadata()->databaseType . " database"
|
||||
);
|
||||
}
|
||||
$record = $this->getRecord($ipAddress);
|
||||
$record = $this->getRecord($class, $type, $ipAddress);
|
||||
|
||||
$record['traits']['ip_address'] = $ipAddress;
|
||||
$class = "GeoIp2\\Model\\" . $class;
|
||||
@@ -145,15 +130,7 @@ class Reader implements ProviderInterface
|
||||
|
||||
private function flatModelFor($class, $type, $ipAddress)
|
||||
{
|
||||
if ($this->metadata()->databaseType !== $type) {
|
||||
$method = lcfirst($class);
|
||||
throw new \BadMethodCallException(
|
||||
"The $method method cannot be used to open a "
|
||||
. $this->metadata()->databaseType . " database"
|
||||
);
|
||||
}
|
||||
|
||||
$record = $this->getRecord($ipAddress);
|
||||
$record = $this->getRecord($class, $type, $ipAddress);
|
||||
|
||||
$record['ip_address'] = $ipAddress;
|
||||
$class = "GeoIp2\\Model\\" . $class;
|
||||
@@ -161,8 +138,15 @@ class Reader implements ProviderInterface
|
||||
return new $class($record);
|
||||
}
|
||||
|
||||
private function getRecord($ipAddress)
|
||||
private function getRecord($class, $type, $ipAddress)
|
||||
{
|
||||
if (strpos($this->metadata()->databaseType, $type) === false) {
|
||||
$method = lcfirst($class);
|
||||
throw new \BadMethodCallException(
|
||||
"The $method method cannot be used to open a "
|
||||
. $this->metadata()->databaseType . " database"
|
||||
);
|
||||
}
|
||||
$record = $this->dbReader->get($ipAddress);
|
||||
if ($record === null) {
|
||||
throw new AddressNotFoundException(
|
||||
|
||||
Reference in New Issue
Block a user