There are times when you need to persist a simple key value data on to a disk, that could serve as a fast cache. Several options exist like plain text, json, pickle, sqlite etc.
But I was looking for a way to do it with minimal work and found there is a standard library called dbm, which satisfied all my requirements of being fast,simple and without additional dependencies.
python dbm has a limitation that it could only store strings.
dbm is not just a python module, it is a Unix database format and there are several variants of it. Most common ones are ndbm, GNU’s gdbm, Oracle’s Berkeley DB etc.
Python dbm is a generic interface that has out of the box support for ndbm and gdbm(which needs additional shared library and not installed by default), and then there is a pure python implementation called dumb. Python attempts to guess the correct dbm format for the file if it is not explicitly specified.
Using dbm is pretty simple and once the object is loaded it could be used like a python dict. Though it will convert the string you store into a bytes object, when you retrieve it and you need to be take care of that.
keys and values will always be stored and retrieved as bytes.
If data is passed as string it will be implicitly converted to bytes using default encoding.
Sample code below shows how to use dbm for persistently storing key value pairs.
import dbm
my_dict = {"foo":"bar", "f":"foobar"}
#create if not already present
with dbm.open('mycache','c') as db:
for k,v in my_dict.items():
db[k] = v
#open for reading
with dbm.open('mycache','r') as db:
for k in db.keys():
value = db.get(k, b'NA')
print("{}={}".format(k.decode('utf8'),value.decode('utf8')))
#Above program will output
#foo=bar
#f=foobar
For more details refer to the official python documentation on dbm module.
References: