Added a redis cache for the template usage stats.

Cache expires every 10 minutes, but will help with the every 2 second query, especially when a job is running.
There is some clean up and qa to do for this yet
This commit is contained in:
Rebecca Law
2017-02-13 18:47:29 +00:00
parent b2267ae5fc
commit 458adefcb8
9 changed files with 351 additions and 218 deletions

View File

@@ -1,6 +1,7 @@
import uuid
from sqlalchemy import (asc, desc)
import sqlalchemy
from sqlalchemy import (desc, cast, String, text)
from app import db
from app.models import (Template, TemplateHistory)
@@ -56,3 +57,39 @@ def dao_get_template_versions(service_id, template_id):
).order_by(
desc(TemplateHistory.version)
).all()
# def dao_get_templates_by_for_cache(cache):
# if not cache or len(cache) == 0:
# return []
# # First create a subquery that is a union select of the cache values
# # Then join templates to the subquery
# cache_queries = [
# db.session.query(sqlalchemy.sql.expression.bindparam("template_id" + str(i),
# template_id).label('template_id'),
# sqlalchemy.sql.expression.bindparam("count" + str(i), count).label('count'))
# for i, (template_id, count) in enumerate(cache)]
# cache_subq = cache_queries[0].union(*cache_queries[1:]).subquery()
# query = db.session.query(Template.id.label('template_id'),
# Template.template_type,
# Template.name,
# cache_subq.c.count.label('count')
# ).join(cache_subq,
# cast(Template.id, String) == cast(cache_subq.c.template_id, String)
# ).order_by(Template.name)
#
# return query.all()
def dao_get_templates_by_for_cache(cache):
if not cache or len(cache) == 0:
return []
txt = "( " + " Union all ".join(
"select '{}'::text as template_id, {} as count".format(x.decode(),
y.decode()) for x, y in cache) + " ) as cache"
txt = "Select t.id as template_id, t.template_type, t.name, cache.count from templates t, " + \
txt + " where t.id::text = cache.template_id order by t.name"
stmt = text(txt)
return db.session.execute(stmt).fetchall()