Add primitive cache on the fileservice is_review, halves the time of loading the list on the primary dashboard

This commit is contained in:
Kelly McDonald 2021-03-23 10:17:48 -04:00
parent 6ab98c1dee
commit f1ca44a0e1
3 changed files with 50 additions and 1 deletions

View File

@ -0,0 +1,47 @@
import time
cache_store = {}
import time
def timeit(f):
def timed(*args, **kw):
ts = time.time()
result = f(*args, **kw)
te = time.time()
print('func:%r args:[%r, %r] took: %2.4f sec' % (f.__name__, args, kw, te-ts))
return result
return timed
# first pass - meant to be down and dirty
def purge_cache(now):
dellist = []
for key in cache_store.keys():
if cache_store[key]['timeout'] < now:
dellist.append(key)
for key in dellist:
del cache_store[key]
def cache(f,timeout=60):
"""Cache the values for function for x minutes
we still have work to do to make a optional kw argument
to set the length of time to cache
"""
def cached(*args, **kw):
now = time.time()
purge_cache(now)
key =f.__name__+str(args)+str(kw)
if key in cache_store.keys():
return cache_store[key]['value']
else:
newtime = now+timeout*60
result = f(*args, **kw)
cache_store[key] ={}
cache_store[key]['value'] = result
cache_store[key]['timeout'] = newtime
return result
return cached

View File

@ -16,7 +16,7 @@ from crc import session, app
from crc.api.common import ApiError
from crc.models.file import FileType, FileDataModel, FileModel, LookupFileModel, LookupDataModel
from crc.models.workflow import WorkflowSpecModel, WorkflowModel, WorkflowSpecDependencyFile
from crc.services.cache_service import cache
class FileService(object):
"""Provides consistent management and rules for storing, retrieving and processing files."""
@ -58,6 +58,7 @@ class FileService(object):
return code in doc_dict
@staticmethod
@cache
def is_workflow_review(workflow_spec_id):
files = session.query(FileModel).filter(FileModel.workflow_spec_id==workflow_spec_id).all()
review = any([f.is_review for f in files])

View File

@ -23,6 +23,7 @@ from crc.services.ldap_service import LdapService
from crc.services.protocol_builder import ProtocolBuilderService
from crc.services.workflow_processor import WorkflowProcessor
from SpiffWorkflow import Task as SpiffTask
from crc.services.cache_service import timeit
class StudyService(object):
"""Provides common tools for working with a Study"""