comparison 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
comparison
equal deleted inserted replaced
1092:63ed5f6bd4eb 1093:eda7a1c6532a
17 # You should have received a copy of the GNU Affero General Public License 17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 from zope.interface import Interface, Attribute, implements 19 from zope.interface import Interface, Attribute, implements
20 from sat.tools.common import data_objects 20 from sat.tools.common import data_objects
21 from libervia.server.constants import Const as C 21 from libervia.server.constants import Const as C
22 from collections import OrderedDict
22 import os.path 23 import os.path
23 import shortuuid 24 import shortuuid
24 import time 25 import time
25 26
26 FLAGS_KEY = '_flags' 27 FLAGS_KEY = '_flags'
28 MAX_CACHE_AFFILIATIONS = 100 # number of nodes to keep in cache
27 29
28 class ISATSession(Interface): 30 class ISATSession(Interface):
29 profile = Attribute("Sat profile") 31 profile = Attribute("Sat profile")
30 jid = Attribute("JID associated with the profile") 32 jid = Attribute("JID associated with the profile")
31 uuid = Attribute("uuid associated with the profile session") 33 uuid = Attribute("uuid associated with the profile session")
43 self.backend_started = None 45 self.backend_started = None
44 self.uuid = unicode(shortuuid.uuid()) 46 self.uuid = unicode(shortuuid.uuid())
45 self.identities = data_objects.Identities() 47 self.identities = data_objects.Identities()
46 self.csrf_token = unicode(shortuuid.uuid()) 48 self.csrf_token = unicode(shortuuid.uuid())
47 self.pages_data = {} # used to keep data accross reloads (key is page instance) 49 self.pages_data = {} # used to keep data accross reloads (key is page instance)
50 self.affiliations = OrderedDict() # cache for node affiliations
48 51
49 @property 52 @property
50 def cache_dir(self): 53 def cache_dir(self):
51 if self.profile is None: 54 if self.profile is None:
52 return self.service_cache_url + u'/' 55 return self.service_cache_url + u'/'
123 del self.pages_data[page] 126 del self.pages_data[page]
124 return True 127 return True
125 else: 128 else:
126 return False 129 return False
127 130
131 def getAffiliation(self, service, node):
132 """retrieve affiliation for a pubsub node
133
134 @param service(jid.JID): pubsub service
135 @param node(unicode): pubsub node
136 @return (unicode, None): affiliation, or None if it is not in cache
137 """
138 if service.resource:
139 raise ValueError(u"Service must not have a resource")
140 if not node:
141 raise ValueError(u"node must be set")
142 try:
143 affiliation = self.affiliations.pop((service, node))
144 except KeyError:
145 return None
146 else:
147 # we replace at the top to get the most recently used on top
148 # so less recently used will be removed if cache is full
149 self.affiliations[(service, node)] = affiliation
150 return affiliation
151
152 def setAffiliation(self, service, node, affiliation):
153 """cache affiliation for a node
154
155 will empty cache when it become too big
156 @param service(jid.JID): pubsub service
157 @param node(unicode): pubsub node
158 @param affiliation(unicode): affiliation to this node
159 """
160 if service.resource:
161 raise ValueError(u"Service must not have a resource")
162 if not node:
163 raise ValueError(u"node must be set")
164 self.affiliations[(service, node)] = affiliation
165 while len(self.affiliations) > MAX_CACHE_AFFILIATIONS:
166 self.affiliations.popitem(last=False)
167
128 168
129 class ISATGuestSession(Interface): 169 class ISATGuestSession(Interface):
130 id = Attribute("UUID of the guest") 170 id = Attribute("UUID of the guest")
131 data = Attribute("data associated with the guest") 171 data = Attribute("data associated with the guest")
132 172