Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
prozorro-sale
Mirror
Commits
dc3cb333
Commit
dc3cb333
authored
Nov 30, 2021
by
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
Changes
1
Hide whitespace changes
Inline
Side-by-side
src/mongodb_mirror/mirror.py
View file @
dc3cb333
...
...
@@ -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
.
s
et
,
cache_key
,
obj_data
)
obj_data
=
await
loop
.
run_in_executor
(
None
,
self
.
_cache_file
.
g
et
,
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
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment