Efficiently filter expired templates in database
We right now iterate over all the cached entries to figure out one of them is expired. While this part of the code doesn't have to be fast, it can be made better.
Something like this can potentially be used to do a better job:
from sqlalchemy import Date, cast
from datetime import date
my_date = (session.query(MyObject)
.filter(cast(MyObject.date_time, Date) == date.today())
.all())
See models/cache.py#L143 and L155.