Skip to content

Commit 982d29a

Browse files
authored
fix caching issue (#13)
* fix: cache[value] instead of cache[id(value)] (#12)
1 parent f211e4b commit 982d29a

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

README.md

+10-2
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,18 @@
1212

1313
Make `mmdb` format ip library file which can be read by [`maxmind` official language reader](https://dev.maxmind.com/geoip/geoip2/downloadable/)
1414

15-
[The official perl writer](https://github.com/maxmind/MaxMind-DB-Writer-perl) was written in perl, which was difficult to customize. So I implemented the `MaxmindDB format` ip library in python language
15+
~~[The official perl writer](https://github.com/maxmind/MaxMind-DB-Writer-perl) was written in perl,
16+
which was difficult to customize.
17+
So I implemented the `MaxmindDB format` ip library in python language.~~
18+
19+
MaxMind has now released an official Go version of the MMDB writer.
20+
If you prefer using Go, you can check out the official Go implementation [mmdbwriter](https://github.com/maxmind/mmdbwriter).
21+
This project still provides a Python alternative for those who need it.
22+
1623
## Install
24+
1725
```shell script
18-
pip install -U git+https://github.com/VimT/MaxMind-DB-Writer-python
26+
pip install -U mmdb_writer
1927
```
2028

2129
## Usage

mmdb_writer.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = "0.2.2"
1+
__version__ = "0.2.3"
22

33
import logging
44
import math
@@ -355,6 +355,13 @@ def python_type_id(self, value):
355355
return MMDBTypeID.DOUBLE
356356
raise TypeError(f"unknown type {value_type}")
357357

358+
def _freeze(self, value):
359+
if isinstance(value, dict):
360+
return tuple((k, self._freeze(v)) for k, v in value.items())
361+
elif isinstance(value, list):
362+
return tuple(self._freeze(v) for v in value)
363+
return value
364+
358365
def encode_meta(self, meta):
359366
res = self._make_header(MMDBTypeID.MAP, len(meta))
360367
meta_type = {
@@ -373,8 +380,9 @@ def encode_meta(self, meta):
373380

374381
def encode(self, value, type_id=None):
375382
if self.cache:
383+
cache_key = self._freeze(value)
376384
try:
377-
return self.data_cache[id(value)]
385+
return self.data_cache[cache_key]
378386
except KeyError:
379387
pass
380388

@@ -401,7 +409,7 @@ def encode(self, value, type_id=None):
401409
pointer_position = self.data_pointer
402410
self.data_pointer += len(res)
403411
pointer = self.encode(pointer_position, 1)
404-
self.data_cache[id(value)] = pointer
412+
self.data_cache[cache_key] = pointer
405413
return pointer
406414
return res
407415

0 commit comments

Comments
 (0)