-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.py
More file actions
54 lines (42 loc) · 1.89 KB
/
cache.py
File metadata and controls
54 lines (42 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env python3
"""
Created by: Lee Bergstrand
Description: Caching functions for micromeda-server.
Requirements: - Refer to README
"""
import uuid
from pygenprop.results import GenomePropertiesResultsWithMatches, load_results_from_serialization
def cache_result(result: GenomePropertiesResultsWithMatches, redis_cache, cache_ttl=3600):
"""
Takes a GenomePropertiesResultsWithMatches, serializes it, and stores it in a redis cache.
:param cache_ttl: Number of seconds that the result should stay in the cache in seconds.
:param result: A GenomePropertiesResultsWithMatches object
:param redis_cache: A object representing a Redis cache
:return: The hexadecimal key used to identify the serialized results in the cache
"""
key = uuid.uuid4().hex
data = result.to_serialization()
redis_cache.set(key, data, ex=cache_ttl)
return key
def get_result_cached_or_default(redis_cache, properties_tree, results_key=None, default_results=None):
"""
Retrieved a cached and serialized GenomePropertiesResultsWithMatches from the cache and reconstitutes it into a
GenomePropertiesResultsWithMatches that can be used.
:param redis_cache: A object representing a Redis cache
:param properties_tree: A GenomePropertiesTree object
:param results_key: The hexadecimal key used to identify the serialized results in the cache
:param default_results: The servers default GenomePropertiesResultsWithMatches object
:return: A GenomePropertiesResultsWithMatches object
"""
if results_key:
cached_results = redis_cache.get(results_key)
if cached_results is not None:
result = load_results_from_serialization(cached_results, properties_tree)
else:
result = None
else:
result = None
if default_results is not None:
if result is None:
result = default_results
return result