-
Notifications
You must be signed in to change notification settings - Fork 79
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
Create generic DB_CACHE
functions for db.Model
classes
#1011
Merged
Merged
Changes from 25 commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
8afeb80
Simpsons
michplunkett c1f95e9
Update test_functional.py
michplunkett afb805a
Update sort.html
michplunkett 383525d
Create cache_client.py
michplunkett 4660f78
Update cache_client.py
michplunkett 70b9bf9
Update database.py
michplunkett 3227fc7
Update db_cache.py
michplunkett 364b5d2
Update database.py
michplunkett 1892b3e
Update db_cache.py
michplunkett 6dfe278
Update database.py
michplunkett 70fae44
Update constants.py
michplunkett 7b98372
Update database.py
michplunkett c662150
Update db_cache.py
michplunkett dd85fdb
Update database.py
michplunkett 3dc0747
Update views.py
michplunkett dfd7a75
Update model_view.py
michplunkett 97d9431
Delete db_cache.py
michplunkett f93fd98
Create database_cache.py
michplunkett f218ea3
Update database.py
michplunkett c8e31b0
Update database_cache.py
michplunkett d5a938c
Create test_cache.py
michplunkett d7571c7
Update test_cache.py
michplunkett 64bb6c4
Update test_cache.py
michplunkett b9cac0e
Update test_cache.py
michplunkett 7ee9038
Update test_cache.py
michplunkett fa18b16
Update database_cache.py
michplunkett 5854925
Update database.py
michplunkett 194e0c3
Update test_cache.py
michplunkett 11119c3
Update database_cache.py
michplunkett 7854415
Update views.py
michplunkett db9dd2a
Update model_view.py
michplunkett 16e62c8
Update database_cache.py
michplunkett 26ae057
Update model_view.py
michplunkett b76bd11
Update views.py
michplunkett 037ec06
Update test_cache.py
michplunkett 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
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,44 @@ | ||
from cachetools import TTLCache | ||
from cachetools.keys import hashkey | ||
|
||
from OpenOversight.app.utils.constants import HOUR | ||
|
||
|
||
DB_CACHE = TTLCache(maxsize=1024, ttl=12 * HOUR) | ||
|
||
|
||
def _department_key(department_id: str, update_type: str): | ||
"""Create unique department key.""" | ||
return hashkey(department_id, update_type, "Department") | ||
|
||
|
||
def department_statistics_cache_key(update_type: str): | ||
"""Return a key function to calculate the cache key for Department | ||
methods using the department id and a given update type. | ||
|
||
Department.id is used instead of a Department obj because the default Python | ||
__hash__ is unique per obj instance, meaning multiple instances of the same | ||
department will have different hashes. | ||
|
||
Update type is used in the hash to differentiate between the update types we compute | ||
per department. | ||
""" | ||
|
||
def _cache_key(department): | ||
return _department_key(department.id, update_type) | ||
|
||
return _cache_key | ||
|
||
|
||
def has_department_cache_entry(department_id: str, update_type: str) -> bool: | ||
"""Department key exists in cache.""" | ||
key = _department_key(department_id, update_type) | ||
return key in DB_CACHE.keys() | ||
|
||
|
||
def remove_department_cache_entry(department_id: str, update_type: str) -> None: | ||
"""Remove department key from cache if it exists.""" | ||
|
||
key = _department_key(department_id, update_type) | ||
if key in DB_CACHE.keys(): | ||
del DB_CACHE[key] |
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
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
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.
@abandoned-prototype: #998 (comment)