Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork77
A decorator for caching properties in classes.
License
pydanny/cached-property
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
A decorator for caching properties in classes.
- Makes caching of time or computational expensive properties quick and easy.
- Because I got tired of copy/pasting this code from non-web project to non-web project.
- I needed something really simple that worked in Python 2 and 3.(Python 3.8 added a version of this decorator as
@functools.cached_property
.)
Let's define a class with an expensive property. Every time you stay there theprice goes up by $50!
classMonopoly:def__init__(self):self.boardwalk_price=500@propertydefboardwalk(self):# In reality, this might represent a database call or time# intensive task like calling a third-party API.self.boardwalk_price+=50returnself.boardwalk_price
Now run it:
>>>monopoly=Monopoly()>>>monopoly.boardwalk550>>>monopoly.boardwalk600
Let's convert the boardwalk property into acached_property
.
fromcached_propertyimportcached_propertyclassMonopoly(object):def__init__(self):self.boardwalk_price=500@cached_propertydefboardwalk(self):# Again, this is a silly example. Don't worry about it, this is# just an example for clarity.self.boardwalk_price+=50returnself.boardwalk_price
Now when we run it the price stays at $550.
>>>monopoly=Monopoly()>>>monopoly.boardwalk550>>>monopoly.boardwalk550>>>monopoly.boardwalk550
Why doesn't the value ofmonopoly.boardwalk
change? Because it's acached property!
Results of cached functions can be invalidated by outside forces. Let's demonstrate how to force the cache to invalidate:
>>>monopoly=Monopoly()>>>monopoly.boardwalk550>>>monopoly.boardwalk550>>># invalidate the cache>>>delmonopoly.__dict__['boardwalk']>>># request the boardwalk property again>>>monopoly.boardwalk600>>>monopoly.boardwalk600
What if a whole bunch of people want to stay at Boardwalk all at once? This means using threads, whichunfortunately causes problems with the standardcached_property
. In this case, switch to using thethreaded_cached_property
:
fromcached_propertyimportthreaded_cached_propertyclassMonopoly:def__init__(self):self.boardwalk_price=500@threaded_cached_propertydefboardwalk(self):"""threaded_cached_property is really nice for when no one waits for other people to finish their turn and rudely start rolling dice and moving their pieces."""sleep(1)self.boardwalk_price+=50returnself.boardwalk_price
Now use it:
>>>fromthreadingimportThread>>>frommonopolyimportMonopoly>>>monopoly=Monopoly()>>>threads= []>>>forxinrange(10):>>>thread=Thread(target=lambda:monopoly.boardwalk)>>>thread.start()>>>threads.append(thread)>>>forthreadinthreads:>>>thread.join()>>>self.assertEqual(m.boardwalk,550)
The cached property can be async, in which case you have to use awaitas usual to get the value. Because of the caching, the value is onlycomputed once and then cached:
fromcached_propertyimportcached_propertyclassMonopoly:def__init__(self):self.boardwalk_price=500@cached_propertyasyncdefboardwalk(self):self.boardwalk_price+=50returnself.boardwalk_price
Now use it:
>>>asyncdefprint_boardwalk():...monopoly=Monopoly()...print(awaitmonopoly.boardwalk)...print(awaitmonopoly.boardwalk)...print(awaitmonopoly.boardwalk)>>>importasyncio>>>asyncio.get_event_loop().run_until_complete(print_boardwalk())550550550
Note that this does not work with threading either, most asyncioobjects are not thread-safe. And if you run separate event loops ineach thread, the cached version will most likely have the wrong eventloop. To summarize, either use cooperative multitasking (event loop)or threading, but not both at the same time.
Sometimes you want the price of things to reset after a time. Use thettl
versions ofcached_property
andthreaded_cached_property
.
importrandomfromcached_propertyimportcached_property_with_ttlclassMonopoly(object):@cached_property_with_ttl(ttl=5)# cache invalidates after 5 secondsdefdice(self):# I dare the reader to implement a game using this method of 'rolling dice'.returnrandom.randint(2,12)
Now use it:
>>>monopoly=Monopoly()>>>monopoly.dice10>>>monopoly.dice10>>>fromtimeimportsleep>>>sleep(6)# Sleeps long enough to expire the cache>>>monopoly.dice3>>>monopoly.dice3
Note: Thettl
tools do not reliably allow the clearing of the cache. Thisis why they are broken out into seperate tools. See#16.
- Pip, Django, Werkzeug, Bottle, Pyramid, and Zope for having their own implementations. This package originally used an implementation that matched the Bottle version.
- Reinout Van Rees for pointing out the
cached_property
decorator to me. - My awesome wife@audreyfeldroy who created
cookiecutter
, which meant rolling this out took me just 15 minutes. - @tinche for pointing out the threading issue and providing a solution.
- @bcho for providing the time-to-expire feature
About
A decorator for caching properties in classes.
Resources
License
Contributing
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Packages0
Uh oh!
There was an error while loading.Please reload this page.