Mercurial > libervia-web
diff src/server/session_iface.py @ 1093:eda7a1c6532a
server: new getAffiliation method:
getAffiliation allows to retrieve affiliation to a pubsub node for logged user. The method handle cache in user's session, avoiding requesting pubsub service when possible.
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 01 Jun 2018 13:03:52 +0200 |
parents | 9c41b7e91172 |
children | 8a270f32de81 |
line wrap: on
line diff
--- a/src/server/session_iface.py Fri Jun 01 12:58:20 2018 +0200 +++ b/src/server/session_iface.py Fri Jun 01 13:03:52 2018 +0200 @@ -19,11 +19,13 @@ from zope.interface import Interface, Attribute, implements from sat.tools.common import data_objects from libervia.server.constants import Const as C +from collections import OrderedDict import os.path import shortuuid import time FLAGS_KEY = '_flags' +MAX_CACHE_AFFILIATIONS = 100 # number of nodes to keep in cache class ISATSession(Interface): profile = Attribute("Sat profile") @@ -45,6 +47,7 @@ self.identities = data_objects.Identities() self.csrf_token = unicode(shortuuid.uuid()) self.pages_data = {} # used to keep data accross reloads (key is page instance) + self.affiliations = OrderedDict() # cache for node affiliations @property def cache_dir(self): @@ -125,6 +128,43 @@ else: return False + def getAffiliation(self, service, node): + """retrieve affiliation for a pubsub node + + @param service(jid.JID): pubsub service + @param node(unicode): pubsub node + @return (unicode, None): affiliation, or None if it is not in cache + """ + if service.resource: + raise ValueError(u"Service must not have a resource") + if not node: + raise ValueError(u"node must be set") + try: + affiliation = self.affiliations.pop((service, node)) + except KeyError: + return None + else: + # we replace at the top to get the most recently used on top + # so less recently used will be removed if cache is full + self.affiliations[(service, node)] = affiliation + return affiliation + + def setAffiliation(self, service, node, affiliation): + """cache affiliation for a node + + will empty cache when it become too big + @param service(jid.JID): pubsub service + @param node(unicode): pubsub node + @param affiliation(unicode): affiliation to this node + """ + if service.resource: + raise ValueError(u"Service must not have a resource") + if not node: + raise ValueError(u"node must be set") + self.affiliations[(service, node)] = affiliation + while len(self.affiliations) > MAX_CACHE_AFFILIATIONS: + self.affiliations.popitem(last=False) + class ISATGuestSession(Interface): id = Attribute("UUID of the guest")