Skip to content

Commit

Permalink
Fix plone.memoize.view to support unhashable types in function argume…
Browse files Browse the repository at this point in the history
…nts.

Fixes: #36
  • Loading branch information
thet committed Jul 6, 2023
1 parent b52b961 commit 8fbaaf1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
3 changes: 3 additions & 0 deletions news/36.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix plone.memoize.view to support unhashable types in function arguments.

Fixes: #36
6 changes: 4 additions & 2 deletions plone/memoize/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from zope.annotation.interfaces import IAnnotations
from zope.globalrequest import getRequest

import json


class ViewMemo:
key = "plone.memoize"
Expand Down Expand Up @@ -44,7 +46,7 @@ def memogetter(*args, **kwargs):
instance.__class__.__name__,
func.__name__,
args[1:],
frozenset(kwargs.items()),
frozenset(json.dumps(kwargs)),
)
if key not in cache:
cache[key] = func(*args, **kwargs)
Expand Down Expand Up @@ -73,7 +75,7 @@ def memogetter(*args, **kwargs):
instance.__class__.__name__,
func.__name__,
args[1:],
frozenset(kwargs.items()),
frozenset(json.dumps(kwargs)),
)
if key not in cache:
cache[key] = func(*args, **kwargs)
Expand Down
12 changes: 11 additions & 1 deletion plone/memoize/view.rst
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,24 @@ Even though we've twiddled txt1, txt2 is not recalculated::
>>> msg.txt2
'hello world'

We support memoization of multiple signatures as long as all signature values are hashable::
We support memoization of multiple signatures.
The signature values just need to be able to be converted into a JSON string::

>>> print(msg.getMsg('Ernest'))
Ernest: goodbye cruel world!

>>> print(msg.getMsg('J.D.', **{'raise':'roofbeams'}))
J.D.: goodbye cruel world! raise--roofbeams

We also support unhashable types like lists and dicts::

>>> print(msg.getMsg('J.D.', lower=['shields', 'engines']))
J.D.: goodbye cruel world! lower--['shields', 'engines']

>>> print(msg.getMsg('J.D.', actions={'open': ['gates']}))
J.D.: goodbye cruel world! actions--{'open': ['gates']}


We can alter data underneath, but nothing changes::

>>> msg.txt1 = 'sound and fury'
Expand Down

0 comments on commit 8fbaaf1

Please sign in to comment.