diff src/memory/disco.py @ 2342:f047d5410040

core (memory/disco): added use_cache parameter to discoInfos/discoItems (set to False to ignore cache)
author Goffi <goffi@goffi.org>
date Tue, 22 Aug 2017 22:12:57 +0200
parents 91347fe95384
children cf9b276f4a08
line wrap: on
line diff
--- a/src/memory/disco.py	Sun Aug 20 17:37:43 2017 +0200
+++ b/src/memory/disco.py	Tue Aug 22 22:12:57 2017 +0200
@@ -147,17 +147,21 @@
         if identity is not None and identity not in disco_infos.identities:
             raise failure.Failure(exceptions.FeatureNotFound())
 
-    def getInfos(self, client, jid_=None, node=u''):
+    def getInfos(self, client, jid_=None, node=u'', use_cache=True):
         """get disco infos from jid_, filling capability hash if needed
 
         @param jid_: jid of the target, or None for profile's server
         @param node(unicode): optional node to use for disco request
+        @param use_cache(bool): if True, use cached data if available
         @return: a Deferred which fire disco.DiscoInfo
         """
         if jid_ is None:
             jid_ = jid.JID(client.jid.host)
         try:
             cap_hash = self.host.memory.getEntityData(jid_, [C.ENTITY_CAP_HASH], client.profile)[C.ENTITY_CAP_HASH]
+            if not use_cache:
+                # we ignore cache, so we pretend we haven't found it
+                raise KeyError
         except (KeyError, exceptions.UnknownEntityError):
             # capability hash is not available, we'll compute one
             def infosCb(disco_infos):
@@ -186,11 +190,12 @@
             return defer.succeed(disco_infos)
 
     @defer.inlineCallbacks
-    def getItems(self, client, jid_=None, node=u''):
+    def getItems(self, client, jid_=None, node=u'', use_cache=True):
         """get disco items from jid_, cache them for our own server
 
         @param jid_(jid.JID): jid of the target, or None for profile's server
         @param node(unicode): optional node to use for disco request
+        @param use_cache(bool): if True, use cached data if available
         @return: a Deferred which fire disco.DiscoItems
         """
         if jid_ is None:
@@ -199,6 +204,9 @@
             try:
                 items = self.host.memory.getEntityData(jid_, ["DISCO_ITEMS"], client.profile)["DISCO_ITEMS"]
                 log.debug(u"[%s] disco items are in cache" % jid_.full())
+                if not use_cache:
+                    # we ignore cache, so we pretend we haven't found it
+                    raise KeyError
             except (KeyError, exceptions.UnknownEntityError):
                 log.debug(u"Caching [%s] disco items" % jid_.full())
                 items = yield client.disco.requestItems(jid_, nodeIdentifier=node)
@@ -308,16 +316,17 @@
         return cap_hash
 
     @defer.inlineCallbacks
-    def _discoInfos(self, entity_jid_s, node=u'', profile_key=C.PROF_KEY_NONE):
+    def _discoInfos(self, entity_jid_s, node=u'', use_cache=True, profile_key=C.PROF_KEY_NONE):
         """ Discovery method for the bridge
         @param entity_jid_s: entity we want to discover
+        @param use_cache(bool): if True, use cached data if available
         @param node(unicode): optional node to use
 
         @return: list of tuples
         """
         client = self.host.getClient(profile_key)
         entity = jid.JID(entity_jid_s)
-        disco_infos = yield self.getInfos(client, entity, node)
+        disco_infos = yield self.getInfos(client, entity, node, use_cache)
         extensions = {}
         for form_type, form in disco_infos.extensions.items():
             fields = []
@@ -350,14 +359,15 @@
             yield (item.entity.full(), item.nodeIdentifier or '', item.name or '')
 
     @defer.inlineCallbacks
-    def _discoItems(self, entity_jid_s, node=u'', profile_key=C.PROF_KEY_NONE):
+    def _discoItems(self, entity_jid_s, node=u'', use_cache=True, profile_key=C.PROF_KEY_NONE):
         """ Discovery method for the bridge
 
         @param entity_jid_s: entity we want to discover
         @param node(unicode): optional node to use
+        @param use_cache(bool): if True, use cached data if available
         @return: list of tuples"""
         client = self.host.getClient(profile_key)
         entity = jid.JID(entity_jid_s)
-        disco_items = yield self.getItems(client, entity, node)
+        disco_items = yield self.getItems(client, entity, node, use_cache)
         ret = list(self.items2tuples(disco_items))
         defer.returnValue(ret)