Update JID cache to do extra memoization and locking.
Passing cache_lock=True to JID() will insert the JID into the cache and prevent it from being dropped from the cache.
This commit is contained in:
parent
2a4e435228
commit
d0666a5eb6
1 changed files with 37 additions and 23 deletions
|
@ -16,6 +16,7 @@ from __future__ import unicode_literals
|
|||
import re
|
||||
import socket
|
||||
import stringprep
|
||||
import threading
|
||||
import encodings.idna
|
||||
|
||||
from sleekxmpp.util import stringprep_profiles
|
||||
|
@ -65,6 +66,7 @@ JID_UNESCAPE_TRANSFORMATIONS = {'\\20': ' ',
|
|||
'\\5c': '\\'}
|
||||
|
||||
JID_CACHE = OrderedDict()
|
||||
JID_CACHE_LOCK = threading.Lock()
|
||||
JID_CACHE_MAX_SIZE = 1024
|
||||
|
||||
|
||||
|
@ -416,21 +418,27 @@ class JID(object):
|
|||
|
||||
# pylint: disable=W0212
|
||||
def __init__(self, jid=None, **kwargs):
|
||||
self._jid = (None, None, None)
|
||||
jid_data = (jid, kwargs.get('local', None),
|
||||
kwargs.get('domain', None),
|
||||
kwargs.get('resource', None))
|
||||
|
||||
if jid is None or jid == '':
|
||||
locked = kwargs.get('cache_lock', False)
|
||||
|
||||
if jid_data in JID_CACHE:
|
||||
parsed_jid, locked = JID_CACHE[jid_data]
|
||||
self._jid = parsed_jid
|
||||
else:
|
||||
if jid is None:
|
||||
jid = ''
|
||||
|
||||
if not jid:
|
||||
jid = (None, None, None)
|
||||
elif jid in JID_CACHE:
|
||||
jid = JID_CACHE[jid]
|
||||
parsed_jid = (None, None, None)
|
||||
elif not isinstance(jid, JID):
|
||||
jid = _parse_jid(jid)
|
||||
parsed_jid = _parse_jid(jid)
|
||||
else:
|
||||
jid = jid._jid
|
||||
parsed_jid = jid._jid
|
||||
|
||||
local, domain, resource = jid
|
||||
local, domain, resource = parsed_jid
|
||||
|
||||
local = kwargs.get('local', local)
|
||||
domain = kwargs.get('domain', domain)
|
||||
|
@ -444,9 +452,15 @@ class JID(object):
|
|||
resource = _validate_resource(resource)
|
||||
|
||||
self._jid = (local, domain, resource)
|
||||
JID_CACHE[_format_jid(*self._jid)] = self._jid
|
||||
|
||||
JID_CACHE[jid_data] = (self._jid, locked)
|
||||
if len(JID_CACHE) > JID_CACHE_MAX_SIZE:
|
||||
JID_CACHE.popitem(False)
|
||||
with JID_CACHE_LOCK:
|
||||
key, item = JID_CACHE.popitem(False)
|
||||
if item[1]:
|
||||
# Need to reinsert locked JIDs
|
||||
JID_CACHE[key] = item
|
||||
|
||||
|
||||
def unescape(self):
|
||||
"""Return an unescaped JID object.
|
||||
|
|
Loading…
Reference in a new issue