| |
@@ -19,6 +19,7 @@
|
| |
|
| |
from copr_common.enums import FailTypeEnum, StatusEnum
|
| |
from coprs import app
|
| |
+ from coprs import cache
|
| |
from coprs import db
|
| |
from coprs import models
|
| |
from coprs import helpers
|
| |
@@ -66,7 +67,8 @@
|
| |
return result
|
| |
|
| |
@classmethod
|
| |
- def get_recent_tasks(cls, user=None, limit=100, period_days=2):
|
| |
+ @cache.memoize(timeout=2*60)
|
| |
+ def get_recent_task_ids(cls, user=None, limit=100, period_days=2):
|
| |
query_args = (
|
| |
models.BuildChroot.build_id,
|
| |
func.max(models.BuildChroot.ended_on).label('max_ended_on'),
|
| |
@@ -94,6 +96,12 @@
|
| |
subquery = subquery.order_by(desc('max_ended_on')).limit(limit).subquery()
|
| |
|
| |
query = models.Build.query.join(subquery, subquery.c.build_id == models.Build.id)
|
| |
+ return [i.id for i in query.all()]
|
| |
+
|
| |
+ @classmethod
|
| |
+ def get_recent_tasks(cls, *args, **kwargs):
|
| |
+ task_ids = cls.get_recent_task_ids(*args, **kwargs)
|
| |
+ query = models.Build.query.filter(models.Build.id.in_(task_ids))
|
| |
return list(query.all())
|
| |
|
| |
@classmethod
|
| |
So as part of this commit, initialize the flask_cache mechanism, using
the already running redis database (we use it for session management).
The 'cache' object logically belongs to init.py, though it needs to
be placed on a pretty weird place (that's because there are
bi-directional dependencies among our modules, this is a problem but to
be solved in a different PR).
Also, instead of caching get_recent_tasks() directly, I created
get_recent_task_ids() which is safe for caching. That's because the
contents is just a list of integers, not sqlalchemy objects.
The problem of caching sqlalchemy objects is that they allow
lazy-loading later in templates, and if loaded from cache - when the
object isn't bound to any session - the lazy load attempt would fail.
Therefore we cache just the ides, and before passing the objects down to
the template we perform another (but way cheaper) query.