in database-caching/example.py [0:0]
def planet(id):
"""Retrieve a record from the cache, or else from the database."""
key = f"planet:{id}"
res = Cache.hgetall(key)
if res:
return res
sql = "SELECT `id`, `name` FROM `planet` WHERE `id`=%s"
res = Database.record(sql, (id,))
if res:
Cache.hmset(key, res)
Cache.expire(key, TTL)
return res