From 0b2cabdd0d8b7fc0f15b16f0143440d712b94317 Mon Sep 17 00:00:00 2001 From: Inso <insomniak.fr@gmail.com> Date: Sat, 11 Jul 2015 15:51:20 +0200 Subject: [PATCH] Working on cache problem #176 --- src/cutecoin/core/net/api/bma/access.py | 70 ++++++++++--------------- src/cutecoin/core/registry/identity.py | 3 +- 2 files changed, 29 insertions(+), 44 deletions(-) diff --git a/src/cutecoin/core/net/api/bma/access.py b/src/cutecoin/core/net/api/bma/access.py index bf1c62de..e832b20c 100644 --- a/src/cutecoin/core/net/api/bma/access.py +++ b/src/cutecoin/core/net/api/bma/access.py @@ -8,6 +8,7 @@ import json import asyncio import random + class BmaAccess(QObject): """ This class is used to access BMA API. @@ -71,10 +72,10 @@ class BmaAccess(QObject): @staticmethod def _gen_cache_key(request, req_args, get_args): return (str(request), - str(tuple(frozenset(sorted(req_args.keys())))), - str(tuple(frozenset(sorted(req_args.values())))), - str(tuple(frozenset(sorted(get_args.keys())))), - str(tuple(frozenset(sorted(get_args.values()))))) + str(tuple(frozenset(sorted(req_args.keys())))), + str(tuple(frozenset(sorted(req_args.values())))), + str(tuple(frozenset(sorted(get_args.keys())))), + str(tuple(frozenset(sorted(get_args.values()))))) def _compare_json(self, first, second): """ @@ -84,40 +85,14 @@ class BmaAccess(QObject): :return: True if the json dicts are the same :rtype: bool """ - if first is not None: - if not isinstance(first, type(second)): - return False - if isinstance(first, dict): - for key in first: - if isinstance(second, dict): - if key in second: - sec = second[key] - else: - # there are key in the first, that is not presented in the second - return False - # recursive call - return self._compare_json(first[key], sec) - else: - # second is not dict - return False - # if object is list, loop over it and check. - elif isinstance(first, list): - for (index, item) in enumerate(first): - # try to get the same index from second - sec = None - if second is not None: - try: - sec = second[index] - except (IndexError, KeyError): - # goes to difference - return False - # recursive call - return self._compare_json(first[index], sec) - # not list, not dict. check for equality - elif first != second: - return False - else: - return True + def ordered(obj): + if isinstance(obj, dict): + return sorted((k, ordered(v)) for k, v in obj.items()) + if isinstance(obj, list): + return sorted(ordered(x) for x in obj) + else: + return obj + return ordered(first) == ordered(second) def _get_from_cache(self, request, req_args, get_args): """ @@ -126,17 +101,22 @@ class BmaAccess(QObject): :param cache_key: The key :return: """ + if request == blockchain.UD: + pass + cache_key = BmaAccess._gen_cache_key(request, req_args, get_args) if cache_key in self._data.keys(): cached_data = self._data[cache_key] need_reload = False if 'metadata' in cached_data: - if str(request) not in BmaAccess.__saved_requests \ - and cached_data['metadata']['block'] < self._network.latest_block: + if 'block' not in cached_data['metadata']: + need_reload = False + elif str(request) not in BmaAccess.__saved_requests \ + and cached_data['metadata']['block'] < self._network.latest_block: need_reload = True else: need_reload = True - ret_data = self._data[cache_key]['value'] + ret_data = cached_data['value'] else: need_reload = True ret_data = request.null_value @@ -181,14 +161,15 @@ class BmaAccess(QObject): :return: The cached data :rtype: dict """ + data = self._get_from_cache(request, req_args, get_args) need_reload = data[0] ret_data = data[1] cache_key = BmaAccess._gen_cache_key(request, req_args, get_args) if need_reload: - #Move to network nstead of community - #after removing qthreads + # Move to network nstead of community + # after removing qthreads if cache_key in self._pending_requests: if caller not in self._pending_requests[cache_key]: logging.debug("New caller".format(caller)) @@ -203,6 +184,8 @@ class BmaAccess(QObject): @pyqtSlot(int, dict, dict, int) def handle_reply(self, request, req_args, get_args, tries): + if request == blockchain.UD: + pass reply = self.sender() logging.debug("Handling QtNetworkReply for {0}".format(str(request))) cache_key = BmaAccess._gen_cache_key(request, req_args, get_args) @@ -238,6 +221,7 @@ class BmaAccess(QObject): :return: The future data :rtype: dict """ + def handle_future_reply(reply): if reply.error() == QNetworkReply.NoError: strdata = bytes(reply.readAll()).decode('utf-8') diff --git a/src/cutecoin/core/registry/identity.py b/src/cutecoin/core/registry/identity.py index 746d9779..f0179cea 100644 --- a/src/cutecoin/core/registry/identity.py +++ b/src/cutecoin/core/registry/identity.py @@ -193,6 +193,7 @@ class Identity(QObject): :return: The list of the certifiers of this community in BMA json format """ certifiers = community.bma_access.get(self, qtbma.wot.CertifiersOf, {'search': self.pubkey}) + if certifiers == qtbma.wot.CertifiersOf.null_value: logging.debug('bma.wot.CertifiersOf request error') data = community.bma_access.get(self, qtbma.wot.Lookup, {'search': self.pubkey}) @@ -218,8 +219,8 @@ class Identity(QObject): certifier['cert_time']['medianTime'] = community.get_block( certifier_data['meta']['block_number'])['medianTime'] certifiers.append(certifier) - return certifiers + return certifiers['certifications'] def unique_valid_certifiers_of(self, community): -- GitLab