My friends and I are making a project to study programming. It consists of several microservices. For our main microservice we use Redis to store auth tokens and it works great.
But we needed another FastAPI microservice which would be extremely small (basically auth and a couple API endpoints). Very very simple. This is why I though that adding Redis as a dependency to it would be overkill. We still need database, so we have Postgres.
Now, I need to store access tokens somewhere, and they have to expire automatically after an hour. I can either make a trigger to delete expired tokens (which is advised against because triggers are hard to test and maintain) or make a cron-job.
I didn't like those options so I decided to store tokens in shelve. I also made a wrapper that deletes token when trying to retrieve it if it expired.
import shelve
from dal.utils import timezone_aware_now
class ShelveDriver:
def __init__(self):
self._db = shelve.open("shelve_store", "c")
def get(self, key):
token = self._db.get(key, None)
if token is None:
return None
if token["expire"] < timezone_aware_now():
del self._db[key]
return self.get(key)
return token
def update(self, key, value):
self._db[key] = value
return True
def delete(self, key):
del self._db[key]
return True
def close(self):
self._db.close()
cache_driver = ShelveDriver()
It works, but I strongly feel that this is wrong approach. Please, point out any flaws you can find in this. Thank you!