Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a sketch for static keys cache #683

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion multidict/_multidict_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,11 @@ class CIMultiDict(MultiDict):
"""Dictionary with the support for duplicate case-insensitive keys."""

def _title(self, key):
return key.title()
cached = _static_cache.get(key)
if cached is not None:
return cached
else:
return key.title()


class _Iter:
Expand Down Expand Up @@ -521,3 +525,20 @@ def __repr__(self):
lst.append("{!r}".format(item[1]))
body = ", ".join(lst)
return "{}({})".format(self.__class__.__name__, body)


_static_cache = {}


def get_static_cache():
return types.MappingProxyType(_static_cache)


def add_static_cache(from_, to):
if not isinstance(to, istr):
raise TypeError(f"Cache target should have 'istr' type, got {type(to)}")
_static_cache[from_] = to


def reset_static_cache():
_static_cache.clear()