summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVictor Shyba <victor.shyba@gmail.com>2015-09-06 05:39:05 -0300
committerVictor Shyba <victor.shyba@gmail.com>2015-09-24 19:39:56 -0300
commitf9faa007bc0d5d681f85aa246ea05ec5fe35ccc5 (patch)
treea1f276edd0ddc2a5a8a39bce77060f3c0065baea
parentd39b408e92bb2fa6a2dac3835e2f5209d2eee94c (diff)
[feat] adds caching for other gen and trans id
There are two functions in couch.py used to save and retrieve the last know gen and trans id for the syncing replica. The get function is called very often, but is only set on one point. Added a simple caching to avoid queying couch for a value that we already have. If cache is empty, it just query as usual and fills it.
-rw-r--r--common/src/leap/soledad/common/couch.py11
-rw-r--r--server/src/leap/soledad/server/__init__.py4
2 files changed, 12 insertions, 3 deletions
diff --git a/common/src/leap/soledad/common/couch.py b/common/src/leap/soledad/common/couch.py
index 421fbac1..3b120cd1 100644
--- a/common/src/leap/soledad/common/couch.py
+++ b/common/src/leap/soledad/common/couch.py
@@ -434,6 +434,7 @@ class CouchDatabase(CommonBackend):
self._set_replica_uid(replica_uid)
if ensure_ddocs:
self.ensure_ddocs_on_db()
+ self.cache = {}
def ensure_ddocs_on_db(self):
"""
@@ -1047,6 +1048,8 @@ class CouchDatabase(CommonBackend):
synchronized with the replica, this is (0, '').
:rtype: (int, str)
"""
+ if other_replica_uid in self.cache:
+ return self.cache[other_replica_uid]
# query a couch view
result = self._database.view('syncs/log')
if len(result[other_replica_uid].rows) == 0:
@@ -1129,6 +1132,7 @@ class CouchDatabase(CommonBackend):
design document for an yet
unknown reason.
"""
+ self.cache[other_replica_uid] = (other_generation, other_transaction_id)
# query a couch update function
ddoc_path = ['_design', 'syncs', '_update', 'put', 'u1db_sync_log']
res = self._database.resource(*ddoc_path)
@@ -1362,7 +1366,7 @@ class CouchServerState(ServerState):
Inteface of the WSGI server with the CouchDB backend.
"""
- def __init__(self, couch_url):
+ def __init__(self, couch_url, cache=None):
"""
Initialize the couch server state.
@@ -1370,6 +1374,7 @@ class CouchServerState(ServerState):
:type couch_url: str
"""
self.couch_url = couch_url
+ self.cache = cache or {}
def open_database(self, dbname):
"""
@@ -1381,10 +1386,12 @@ class CouchServerState(ServerState):
:return: The CouchDatabase object.
:rtype: CouchDatabase
"""
- return CouchDatabase(
+ db = CouchDatabase(
self.couch_url,
dbname,
ensure_ddocs=False)
+ db.cache = self.cache
+ return db
def ensure_database(self, dbname):
"""
diff --git a/server/src/leap/soledad/server/__init__.py b/server/src/leap/soledad/server/__init__.py
index 1b795016..58f2b0b1 100644
--- a/server/src/leap/soledad/server/__init__.py
+++ b/server/src/leap/soledad/server/__init__.py
@@ -111,6 +111,7 @@ from leap.soledad.server.sync import (
from leap.soledad.common import SHARED_DB_NAME
from leap.soledad.common.couch import CouchServerState
+from leap.soledad.server.caching import get_cache_for
old_tsafe = tsafe
@@ -303,7 +304,8 @@ def load_configuration(file_path):
def application(environ, start_response):
conf = load_configuration('/etc/leap/soledad-server.conf')
- state = CouchServerState(conf['couch_url'])
+ cache = get_cache_for('replica_cache')
+ state = CouchServerState(conf['couch_url'], cache=cache)
# WSGI application that may be used by `twistd -web`
application = GzipMiddleware(
SoledadTokenAuthMiddleware(SoledadApp(state)))