1 # -*- coding: utf-8 -*-
3 __author__
= 'Daniel Greenfeld'
4 __email__
= 'pydanny@gmail.com'
12 class cached_property(object):
13 """ A property that is only computed once per instance and then replaces
14 itself with an ordinary attribute. Deleting the attribute resets the
16 Source: https://github.com/bottlepy/bottle/commit/fa7733e075da0d790d809aa3d2f53071897e6f76
19 def __init__(self
, func
):
20 self
.__doc
__ = getattr(func
, '__doc__')
23 def __get__(self
, obj
, cls
):
26 value
= obj
.__dict
__[self
.func
.__name
__] = self
.func(obj
)
30 class threaded_cached_property(cached_property
):
31 """ A cached_property version for use in environments where multiple
32 threads might concurrently try to access the property.
34 def __init__(self
, func
):
35 super(threaded_cached_property
, self
).__init
__(func
)
36 self
.lock
= threading
.RLock()
38 def __get__(self
, obj
, cls
):
40 # Double check if the value was computed before the lock was
42 prop_name
= self
.func
.__name
__
43 if prop_name
in obj
.__dict
__:
44 return obj
.__dict
__[prop_name
]
46 # If not, do the calculation and release the lock.
47 return super(threaded_cached_property
, self
).__get
__(obj
, cls
)
50 class cached_property_with_ttl(object):
51 """ A property that is only computed once per instance and then replaces
52 itself with an ordinary attribute. Setting the ttl to a number expresses
53 how long the property will last before being timed out.
56 def __init__(self
, ttl
=None):
59 if callable(ttl_or_func
):
60 self
.prepare_func(ttl_or_func
)
62 self
.ttl
= ttl_or_func
64 def prepare_func(self
, func
, doc
=None):
65 '''Prepare to cache object method.'''
67 self
.__doc
__ = doc
or func
.__doc
__
68 self
.__name
__ = func
.__name
__
69 self
.__module
__ = func
.__module
__
71 def __call__(self
, func
, doc
=None):
72 self
.prepare_func(func
, doc
)
75 def __get__(self
, obj
, cls
):
81 value
, last_update
= obj
._cache
[self
.__name
__]
82 if self
.ttl
and self
.ttl
> 0 and now
- last_update
> self
.ttl
:
84 except (KeyError, AttributeError):
85 value
= self
.func(obj
)
88 except AttributeError:
89 cache
= obj
._cache
= {}
90 cache
[self
.__name
__] = (value
, now
)
94 # Aliases to make cached_property_with_ttl easier to use
95 cached_property_ttl
= cached_property_with_ttl
96 timed_cached_property
= cached_property_with_ttl
99 class threaded_cached_property_with_ttl(cached_property_with_ttl
):
100 """ A cached_property version for use in environments where multiple
101 threads might concurrently try to access the property.
103 def __init__(self
, ttl
=None):
104 super(threaded_cached_property_with_ttl
, self
).__init
__(ttl
)
105 self
.lock
= threading
.RLock()
107 def __get__(self
, obj
, cls
):
109 # Double check if the value was computed before the lock was
111 prop_name
= self
.__name
__
112 if hasattr(obj
, '_cache') and prop_name
in obj
._cache
:
113 return obj
._cache
[prop_name
][0]
115 # If not, do the calculation and release the lock.
116 return super(threaded_cached_property_with_ttl
, self
).__get
__(obj
, cls
)
118 # Alias to make threaded_cached_property_with_ttl easier to use
119 threaded_cached_property_ttl
= threaded_cached_property_with_ttl
120 timed_threaded_cached_property
= threaded_cached_property_with_ttl