Skip to content

Commit

Permalink
Merge pull request #22 from plone/17-use-global-request
Browse files Browse the repository at this point in the history
Use the global request
  • Loading branch information
ale-rt authored Apr 11, 2020
2 parents de09635 + 1c29639 commit f378dc6
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 6 deletions.
1 change: 1 addition & 0 deletions news/17.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Use the zope global request if available as a fallback if the context does not have it [ale-rt]
29 changes: 24 additions & 5 deletions plone/memoize/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@
from zope.annotation.interfaces import IAnnotations


try:
from zope.globalrequest import getRequest
except ImportError:

def getRequest():
return None


class ViewMemo(object):

key = "plone.memoize"
Expand All @@ -17,9 +25,12 @@ def memogetter(*args, **kwargs):
instance = args[0]

context = getattr(instance, "context", None)
request = getattr(instance, "request", None)
try:
request = instance.request
except AttributeError:
request = getRequest()

annotations = IAnnotations(request)
annotations = IAnnotations(request, {})
if self.key not in annotations:
annotations[self.key] = dict()
cache = annotations[self.key]
Expand Down Expand Up @@ -52,10 +63,18 @@ def memogetter(*args, **kwargs):

def memoize_contextless(self, func):
def memogetter(*args, **kwargs):
instance = args[0]
request = getattr(instance, "request", None)

annotations = IAnnotations(request)
if args:
instance = args[0]
else:
instance = None

try:
request = instance.request
except AttributeError:
request = getRequest()

annotations = IAnnotations(request, {})
if self.key not in annotations:
annotations[self.key] = dict()
cache = annotations[self.key]
Expand Down
69 changes: 69 additions & 0 deletions plone/memoize/view.rst
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,72 @@ based on parameters, but not on context::
>>> print(msg2.getAnotherMsg('J.D.', **{'raise':'roofbeams'}))
J.D.: so long, cruel world& raise--roofbeams

There is also support for using a global request
if zope.globalrequest is available.
With that you can cache also functions.

If the global request is missing nothing changes:

>>> a = "foo"
>>> @view.memoize_contextless
... def memoized_function():
... return a
>>> memoized_function()
'foo'
>>> a = "bar"
>>> memoized_function()
'bar'

Now we provide a global request which supports annotations:

>>> from zope.globalrequest import setRequest
>>> from zope.interface import alsoProvides
>>> from zope.annotation import IAttributeAnnotatable
>>> global_request = TestRequest()
>>> alsoProvides(global_request, IAttributeAnnotatable)
>>> setRequest(global_request)

With that in place the results are cached:
>>> a = "foo"
>>> memoized_function()
'foo'
>>> a = "bar"
>>> memoized_function()
'foo'


The same is true for an adapter:

>>> class Adapter(object):
...
... msg = "foo"
...
... def __init__(self, context):
... self.context = context
...
... @view.memoize
... def context_aware_function(self):
... return self.msg
...
... @view.memoize_contextless
... def context_unaware_function(self):
... return self.msg

We now instatiate two objects:
>>> instance1 = Adapter(Dummy())
>>> instance2 = Adapter(Dummy())
>>> instance1.context_aware_function()
'foo'
>>> instance1.context_unaware_function()
'foo'

Let's verify that the cache depends on the context:
>>> Adapter.msg = "bar"
>>> instance2.context_aware_function()
'bar'
>>> instance1.context_unaware_function()
'foo'

Still instance1 is not aware of the change:
>>> instance1.context_aware_function()
'foo'
4 changes: 3 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ def read(*rnames):
include_package_data=True,
zip_safe=False,
test_suite="plone.memoize.tests.test_suite",
extras_require=dict(test=["zope.configuration", "zope.publisher",]),
extras_require=dict(
test=["zope.configuration", "zope.globalrequest", "zope.publisher",]
),
install_requires=[
"setuptools",
"six",
Expand Down

0 comments on commit f378dc6

Please sign in to comment.