Skip to content

Commit 3221f8a

Browse files
committed
added caching to lookup function
1 parent f220c04 commit 3221f8a

2 files changed

Lines changed: 23 additions & 9 deletions

File tree

geolite/README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ Below are the fields that will be set for each database type:
3131
* uint32 `country_geoname_id`
3232
* uint32 `is_in_european_union`
3333
* string `city_name`
34-
* float `latitude`
35-
* float `longitude`
34+
* float `latitude`
35+
* float `longitude`
3636
* uint32 `accuracy_radius`
3737

3838
* `asn`
@@ -61,6 +61,10 @@ module takes the following parameters:
6161

6262
* Specify the type of GeoLite database. The default value is `country`.
6363

64+
* `-c` `--cache` number
65+
66+
* Specify the number of lookup calls that will be cached. Set to `0` to disable caching. The default value is `128`.
67+
6468

6569
## Example
6670
The following command :

geolite/geolite.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737

3838
import argparse
39+
from functools import lru_cache
3940
import pytrap
4041
import geoip2.database
4142

@@ -58,6 +59,10 @@
5859
help="type of GeoLite database",
5960
choices=['country', 'city', 'asn'],
6061
default="country")
62+
parser.add_argument('-c', "--cache",
63+
type=int,
64+
help="number of cached lookups. If set to 0, caching is disabled.",
65+
default=128)
6166

6267
# parse command line arguments
6368
args = parser.parse_args()
@@ -88,9 +93,19 @@
8893
trap.setRequiredFmt(0, fmttype, fmtspec)
8994
rec = pytrap.UnirecTemplate(fmtspec)
9095

91-
9296
# open GeoLite2 database reader
9397
with geoip2.database.Reader(args.db) as reader:
98+
@lru_cache(maxsize=args.cache)
99+
def lookup_in_database(lookup_ip):
100+
#Function to look up the IP address in the GeoLite2 database.
101+
if args.type == 'city':
102+
return reader.city(lookup_ip)
103+
elif args.type == 'country':
104+
return reader.country(lookup_ip)
105+
elif args.type == 'asn':
106+
return reader.asn(lookup_ip)
107+
108+
94109
# main loop
95110
while True:
96111
try:
@@ -114,12 +129,7 @@
114129
ip = rec.get(data, field)
115130
output.ip = ip
116131
try:
117-
if args.type == 'city':
118-
geolocation = reader.city(str(ip))
119-
elif args.type == 'country':
120-
geolocation = reader.country(str(ip))
121-
elif args.type == 'asn':
122-
geolocation = reader.asn(str(ip))
132+
geolocation = lookup_in_database(str(ip))
123133
except:
124134
continue
125135
if geolocation is None:

0 commit comments

Comments
 (0)