-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcache.py
More file actions
35 lines (24 loc) · 934 Bytes
/
cache.py
File metadata and controls
35 lines (24 loc) · 934 Bytes
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
from datetime import datetime
from elasticsearch import Elasticsearch
from elasticsearch.exceptions import NotFoundError
from settings import ELASTIC_CACHE_INDEX, ELASTIC_HOST, ELASTIC_AUTH
class ElasticsearchBackend:
def __init__(self):
self.client = Elasticsearch([ELASTIC_HOST], http_auth=ELASTIC_AUTH)
self.index = ELASTIC_CACHE_INDEX
def get(self, id_):
try:
return self.client.get_source(index=self.index, id=id_)
except NotFoundError:
return
def set(self, id_, body):
body['created'] = datetime.now().isoformat()
return self.client.index(index=self.index, id=id_, body=body)
class BaseCache:
def __init__(self, backend):
self.backend = backend
def get(self, id_):
return self.backend.get(id_)
def set(self, id_, body):
return self.backend.set(id_, body)
Cache = BaseCache(ElasticsearchBackend())