-
Notifications
You must be signed in to change notification settings - Fork 62
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
Implement "in memory" backend #39
Merged
shaypal5
merged 8 commits into
python-cachier:inmemory
from
cthoyt:implement-memory-backend
Nov 17, 2020
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
fd39e0f
Make code more abstract to pick from many future possible backends
cthoyt e024f64
Add stubs for redis and memory
cthoyt 27e0ca6
Make default compatible with previous interface
cthoyt 31560c6
Update docs
cthoyt ae487b7
Add tests
cthoyt 019e07a
Add "in memory" backend
cthoyt 7fa9962
Update memory_core.py
cthoyt 2e115ea
Add tests for memory core
cthoyt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
"""A memory-based caching core for cachier.""" | ||
|
||
from collections import defaultdict | ||
from datetime import datetime | ||
|
||
from .base_core import _BaseCore | ||
|
||
|
||
class _MemoryCore(_BaseCore): | ||
"""The pickle core class for cachier. | ||
|
||
Parameters | ||
---------- | ||
stale_after : datetime.timedelta, optional | ||
See :class:`_BaseCore` documentation. | ||
next_time : bool, optional | ||
See :class:`_BaseCore` documentation. | ||
""" | ||
|
||
def __init__(self, stale_after, next_time): | ||
super().__init__(stale_after=stale_after, next_time=next_time) | ||
self.cache = {} | ||
|
||
def get_entry_by_key(self, key, reload=False): # pylint: disable=W0221 | ||
return key, self.cache.get(key, None) | ||
|
||
def get_entry(self, args, kwds, hash_params): | ||
key = args + tuple(sorted(kwds.items())) if hash_params is None else hash_params(args, kwds) | ||
return self.get_entry_by_key(key) | ||
|
||
def set_entry(self, key, func_res): | ||
self.cache[key] = { | ||
'value': func_res, | ||
'time': datetime.now(), | ||
'stale': False, | ||
'being_calculated': False, | ||
} | ||
|
||
def mark_entry_being_calculated(self, key): | ||
try: | ||
self.cache[key]['being_calculated'] = True | ||
except KeyError: | ||
self.cache[key] = { | ||
'value': None, | ||
'time': datetime.now(), | ||
'stale': False, | ||
'being_calculated': True, | ||
} | ||
|
||
def mark_entry_not_calculated(self, key): | ||
try: | ||
self.cache[key]['being_calculated'] = False | ||
except KeyError: | ||
pass # that's ok, we don't need an entry in that case | ||
|
||
def wait_on_entry_calc(self, key): | ||
entry = self.cache[key] | ||
# I don't think waiting is necessary for this one | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it turns out this assumption isn't at all correct 😆 |
||
# if not entry['being_calculated']: | ||
return entry['value'] | ||
|
||
def clear_cache(self): | ||
self.cache.clear() | ||
|
||
def clear_being_calculated(self): | ||
for value in self.cache.values(): | ||
value['being_calculated'] = False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
"""Testing the MongoDB core of cachier.""" | ||
|
||
from cachier import cachier | ||
from cachier.core import MissingMongetter | ||
|
||
|
||
def test_bad_name(): | ||
"""Test that the appropriate exception is thrown when an invalid backend is given.""" | ||
name = 'nope' | ||
try: | ||
@cachier(backend=name) | ||
def func(): | ||
pass | ||
except ValueError as e: | ||
assert name in e.args[0] | ||
else: | ||
assert False | ||
|
||
|
||
def test_missing_mongetter(): | ||
"""Test that the appropriate exception is thrown when forgetting to specify the mongetter.""" | ||
try: | ||
@cachier(backend='mongo', mongetter=None) | ||
def func(): | ||
pass | ||
except MissingMongetter: | ||
assert True | ||
else: | ||
assert False |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add 'memory' as a valid option.