Skip to content

Commit

Permalink
feat(290): Cache previous/next/etc. in VersionClassBase
Browse files Browse the repository at this point in the history
Cache some properties in VersionClassBase as it won't really change
and can improve performance significantly by avoiding extra queries

Tested this in my own application and also in a shell:
>>> class A:
...     @cached_property
...     def abc(self):
...         print("test")
...         return 1
...
>>> a = A()
>>> a.abc
test
1
>>> a.abc
1
  • Loading branch information
AbdealiLoKo committed Aug 28, 2022
1 parent 38cef84 commit 39b57fe
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ def get_version():
platforms='any',
install_requires=[
'SQLAlchemy>=1.0.8',
'SQLAlchemy-Utils>=0.30.12'
'SQLAlchemy-Utils>=0.30.12',
'cached-property'
],
extras_require=extras_require,
classifiers=[
Expand Down
9 changes: 5 additions & 4 deletions sqlalchemy_continuum/version.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import sqlalchemy as sa
from cached_property import cached_property

from .reverter import Reverter
from .utils import get_versioning_manager, is_internal_column, parent_class
from .utils import get_versioning_manager, is_internal_column, memoized_property, parent_class


class VersionClassBase(object):
@property
@cached_property
def previous(self):
"""
Returns the previous version relative to this version in the version
Expand All @@ -18,7 +19,7 @@ def previous(self):
.previous(self)
)

@property
@cached_property
def next(self):
"""
Returns the next version relative to this version in the version
Expand All @@ -31,7 +32,7 @@ def next(self):
.next(self)
)

@property
@cached_property
def index(self):
"""
Return the index of this version in the version history.
Expand Down

0 comments on commit 39b57fe

Please sign in to comment.