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

[ENG-6801] Preprint _id caching #10881

Closed
Show file tree
Hide file tree
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
5 changes: 2 additions & 3 deletions osf/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

from framework import sentry
from osf.exceptions import ValidationError
from osf.utils.caching import cached_property
from osf.utils.caching import cached_property, cached_not_none_property
from osf.utils.fields import LowercaseCharField, NonNaiveDateTimeField
from website import settings as website_settings

Expand Down Expand Up @@ -521,9 +521,8 @@ class Meta:

versioned_guids = GenericRelation('GuidVersionsThrough', related_name='referent', related_query_name='referents')

@property
@cached_not_none_property
def _id(self):
# TODO: maybe a cached property?
try:
versioned_guid = self.versioned_guids
if not versioned_guid.exists():
Expand Down
17 changes: 17 additions & 0 deletions osf/utils/caching.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,23 @@ def do_fdel(obj):

return do_fdel

class _CachedNotNoneProperty(_CachedProperty):

def _wrap_fget(self, fget):
@wraps(fget)
def do_fget(obj):
if hasattr(obj, self._cache_name):
if getattr(obj, self._cache_name):
cslzchen marked this conversation as resolved.
Show resolved Hide resolved
return getattr(obj, self._cache_name)
# Generate the value to cache.
value = fget(obj)
if value:
cslzchen marked this conversation as resolved.
Show resolved Hide resolved
setattr(obj, self._cache_name, value)
return value

return do_fget


# Public name for the cached property decorator. Using a class as a decorator just looks plain ugly. :P
cached_property = _CachedProperty
cached_not_none_property = _CachedNotNoneProperty
Loading