-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcache.py
More file actions
31 lines (25 loc) · 1.2 KB
/
cache.py
File metadata and controls
31 lines (25 loc) · 1.2 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
from .base import GraphQLBackend
# from .utils import get_unique_document_id, get_unique_schema_id
class GraphQLCachedBackend(GraphQLBackend):
def __init__(self, backend, cache_map=None):
assert isinstance(
backend, GraphQLBackend
), "Provided backend must be an instance of GraphQLBackend"
if cache_map is None:
cache_map = {}
self.backend = backend
self.cache_map = cache_map
def get_key_for_schema_and_document_string(self, schema, request_string):
"""This method returns a unique key given a schema and a request_string"""
return hash((schema, request_string))
# schema_id = get_unique_schema_id(schema)
# document_id = get_unique_document_id(request_string)
# return (schema_id, document_id)
def document_from_string(self, schema, request_string):
"""This method returns a GraphQLQuery (from cache if present)"""
key = self.get_key_for_schema_and_document_string(schema, request_string)
if key not in self.cache_map:
self.cache_map[key] = self.backend.document_from_string(
schema, request_string
)
return self.cache_map[key]