From 8e9063a0fc9189ea07834508ba8e6c80c47cb55c Mon Sep 17 00:00:00 2001 From: Daniel Greenfeld Date: Fri, 13 Feb 2015 10:54:47 -0800 Subject: [PATCH] Update README.rst --- README.rst | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/README.rst b/README.rst index cac64d3..bbdb552 100644 --- a/README.rst +++ b/README.rst @@ -103,6 +103,37 @@ Results of cached functions can be invalidated by outside forces. Let's demonstr 600 >>> monopoly.boardwalk 600 + +Timing out the cache +-------------------- + +Sometimes you want the price of things to reset after a time. + +.. code-block:: python + + import random + from cached_property import cached_property + + class Monopoly(object): + + @cached_property(ttl=5) # cache invalidates after 10 seconds + def dice(self): + # I dare the reader to implement a game using this method of 'rolling dice'. + return random.randint(2,12) + +.. code-block:: python + + >>> monopoly = Monopoly() + >>> monopoly.dice + 10 + >>> monopoly.dice + 10 + >>> from time import sleep + >>> sleep(6) # Sleeps long enough to expire the cache + >>> monopoly.dice + 3 + >>> monopoly.dice + 3 Working with Threads --------------------- -- 2.30.2