Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

A decorator for caching properties in classes.

License

NotificationsYou must be signed in to change notification settings

pydanny/cached-property

Repository files navigation

Github Actions statusPyPICode style: ruff

A decorator for caching properties in classes.

Why?

  • 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.)

How to use it

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!

Invalidating the Cache

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

Working with Threads

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)

Working with async/await

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.

Timing out the cache

Sometimes you want the price of things to reset after a time. Use thettlversions 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.

Credits

  • 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 thecached_property decorator to me.
  • My awesome wife@audreyfeldroy who createdcookiecutter, 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

Stars

Watchers

Forks

Sponsor this project

  •  

Packages

No packages published

Contributors22


[8]ページ先頭

©2009-2025 Movatter.jp