I am using a lib to get user location by their IPs.
main code:
...
datfile = os.path.join(os.path.dirname(__file__), "ipdb.dat")
class IPv4Database(object):
def __init__(self, filename=None, use_mmap=True):
print 'IPv4Database init'
if filename is None:
filename = datfile
with open(filename, 'rb') as f:
if use_mmap and mmap is not None:
...
I know read a file would cost a lot,so I want ipdb = IPv4Database()
just call once, then use ipdb
in the whole django project.
First thing I thougnt is to set ipdb
as a globla variable,but the only way I know is puting ipdb = IPv4Database()
to settings
.Is it Ok to initiate a object in settings?
Another way ---- cache :
Save the file to cache seems not good, __init__
still need to load this file.
So as pickle
, pickle ipdb
and save to cache, then unpickle every time I need it.I afraid it is not good if the file is large.
What should I do?