core (memory/disco): added use_cache parameter to discoInfos/discoItems (set to False to ignore cache)

This commit is contained in:
Goffi 2017-08-22 22:12:57 +02:00
commit 9f30634f14

View file

@ -147,17 +147,21 @@ class Discovery(object):
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 @@ class Discovery(object):
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 @@ class Discovery(object):
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 @@ class Discovery(object):
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 @@ class Discovery(object):
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)