Commit dc3cb333 authored by dmitry.mashoshin's avatar dmitry.mashoshin
Browse files

Merge branch 'bugfix/add-cache-disabling-env-var' into 'master'

build: add cache disabling env variable

See merge request !182
parents 7a2f1ca3 81e2843c
......@@ -40,6 +40,8 @@ CLIENT_PROTOCOL = {
'simple': client.SocketMirrorClientSimpleProtocol
}
# TODO: refactor env variables to Env class
CACHE_ENABLED = bool(os.environ.get('CACHE_ENABLED', False))
CACHE_FILE_PATH = os.environ.get('CACHE_FILE_PATH', 'serialize_cache')
CACHE_FILE = None
CACHE_SIZE_LIMIT = 1024 ** 2 * 100 # 100 Mb
......@@ -152,22 +154,25 @@ class MongoDBMirror:
raise ex
async def _get_serialized_document(self, data, collection, timestamp):
_obj_id = data.get('_id', '')
cache_key = f"{timestamp.time}_{timestamp.inc}_{_obj_id}"
loop = asyncio.get_running_loop()
if CACHE_ENABLED:
_obj_id = data.get('_id', '')
cache_key = f"{_obj_id}_{timestamp.time}_{timestamp.inc}"
loop = asyncio.get_running_loop()
try:
obj_data = await loop.run_in_executor(None, self._cache_file.get, cache_key)
except Exception as ex:
LOG.warning(f'Failed to get object data from cache. {str(ex)}')
obj_data = None
if not obj_data:
obj_data = self._serialize_document(data, collection)
try:
await loop.run_in_executor(None, self._cache_file.set, cache_key, obj_data)
obj_data = await loop.run_in_executor(None, self._cache_file.get, cache_key)
except Exception as ex:
LOG.warning(f'Failed to set object data to cache. {str(ex)}')
LOG.warning(f'Failed to get object data from cache. {str(ex)}')
obj_data = None
if not obj_data:
obj_data = self._serialize_document(data, collection)
try:
await loop.run_in_executor(None, self._cache_file.set, cache_key, obj_data)
except Exception as ex:
LOG.warning(f'Failed to set object data to cache. {str(ex)}')
else:
obj_data = self._serialize_document(data, collection)
return obj_data
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment