Skip to content

Commit

Permalink
optimize cache #110
Browse files Browse the repository at this point in the history
  • Loading branch information
jensens committed Feb 9, 2022
1 parent faadd31 commit b1cb48b
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions src/pas/plugins/ldap/cache.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import re
from .interfaces import ICacheSettingsRecordProvider
from .interfaces import ILDAPPlugin
from .interfaces import IPluginCacheHandler
Expand Down Expand Up @@ -109,26 +110,29 @@ def set(self, value):
class RequestPluginCache:
def __init__(self, context):
self.context = context
self._key = f"_v_ldap_ugm_{self.context.getId()}_"

def _key(self):
return f"_v_ldap_ugm_{self.context.getId()}_"
def getRootRequest(self):
def parent_request(current_request):
preq = current_request.get("PARENT_REQUEST", None)
if preq:
return parent_request(preq)
return current_request

return parent_request(getRequest())

def get(self):
request = getRequest()
rcachekey = self._key()
return (request or {}).get(rcachekey, VALUE_NOT_CACHED)
return (self.getRootRequest() or {}).get(self._key, VALUE_NOT_CACHED)

def set(self, value):
request = getRequest()
request = self.getRootRequest()
if request is not None:
rcachekey = self._key()
request[rcachekey] = value
request[self._key] = value

def invalidate(self):
request = getRequest()
rcachekey = self._key()
if request and rcachekey in list(request.keys()):
del request[rcachekey]
request = self.getRootRequest()
if request and self._key in list(request.keys()):
del request[self._key]


VOLATILE_CACHE_MAXAGE = 10 # 10s default maxage on volatile
Expand All @@ -138,18 +142,18 @@ def invalidate(self):
class VolatilePluginCache(RequestPluginCache):
def get(self):
try:
cachetime, value = getattr(self.context, self._key())
cachetime, value = getattr(self.context, self._key)
except AttributeError:
return VALUE_NOT_CACHED
if time.time() - cachetime > VOLATILE_CACHE_MAXAGE:
return VALUE_NOT_CACHED
return value

def set(self, value):
setattr(self.context, self._key(), (time.time(), value))
setattr(self.context, self._key, (time.time(), value))

def invalidate(self):
try:
delattr(self.context, self._key())
delattr(self.context, self._key)
except AttributeError:
pass

0 comments on commit b1cb48b

Please sign in to comment.