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 1 commit
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
5 changes: 5 additions & 0 deletions osf/models/preprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,8 @@ def create(cls, provider, title, creator, description):
)
versioned_guid.save()

preprint._id = None # trigger _id cache update

cslzchen marked this conversation as resolved.
Show resolved Hide resolved
return preprint

def get_last_not_rejected_version(self):
Expand Down Expand Up @@ -424,6 +426,8 @@ def create_version(cls, create_from_guid, auth):
)
guid_version.save()

preprint._id = None # trigger _id cache update
cslzchen marked this conversation as resolved.
Show resolved Hide resolved

# Add contributors
for contributor_info in latest_version.contributor_set.exclude(user=latest_version.creator).values('visible', 'user_id', '_order'):
preprint.contributor_set.create(**{**contributor_info, 'preprint_id': preprint.id})
Expand All @@ -438,6 +442,7 @@ def create_version(cls, create_from_guid, auth):
guid_obj.object_id = preprint.pk
guid_obj.content_type = ContentType.objects.get_for_model(preprint)
guid_obj.save()
preprint._id = None # trigger _id cache update
cslzchen marked this conversation as resolved.
Show resolved Hide resolved

return preprint, data_to_update

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
1 change: 1 addition & 0 deletions osf_tests/management_commands/test_check_crossref_dois.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def stuck_preprint(self):
guid._id = 'guid0'
guid.save()

preprint._id = None
cslzchen marked this conversation as resolved.
Show resolved Hide resolved
preprint.save()
return preprint

Expand Down
1 change: 1 addition & 0 deletions osf_tests/test_guid.py
Original file line number Diff line number Diff line change
Expand Up @@ -565,5 +565,6 @@ def test_versioned_preprint_id_property(self, creator, preprint_provider):
assert preprint._id == expected_guid

GuidVersionsThrough.objects.filter(guid=preprint_guid).delete()
preprint._id = None
cslzchen marked this conversation as resolved.
Show resolved Hide resolved

assert preprint._id is None
Loading