1 # -*- coding: utf-8 -*-
3 __author__
= 'Daniel Greenfeld'
4 __email__
= 'pydanny@gmail.com'
12 class cached_property(object):
14 A property that is only computed once per instance and then replaces itself
15 with an ordinary attribute. Deleting the attribute resets the property.
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(object):
32 A cached_property version for use in environments where multiple threads
33 might concurrently try to access the property.
36 def __init__(self
, func
):
37 self
.__doc
__ = getattr(func
, '__doc__')
39 self
.lock
= threading
.RLock()
41 def __get__(self
, obj
, cls
):
45 obj_dict
= obj
.__dict
__
46 name
= self
.func
.__name
__
49 # check if the value was computed before the lock was acquired
52 # if not, do the calculation and release the lock
53 return obj_dict
.setdefault(name
, self
.func(obj
))
56 class cached_property_with_ttl(object):
58 A property that is only computed once per instance and then replaces itself
59 with an ordinary attribute. Setting the ttl to a number expresses how long
60 the property will last before being timed out.
63 def __init__(self
, ttl
=None):
66 if callable(ttl_or_func
):
67 self
.prepare_func(ttl_or_func
)
69 self
.ttl
= ttl_or_func
71 def prepare_func(self
, func
, doc
=None):
72 '''Prepare to cache object method.'''
74 self
.__doc
__ = doc
or func
.__doc
__
75 self
.__name
__ = func
.__name
__
76 self
.__module
__ = func
.__module
__
78 def __call__(self
, func
, doc
=None):
79 self
.prepare_func(func
, doc
)
82 def __get__(self
, obj
, cls
):
88 value
, last_update
= obj
._cache
[self
.__name
__]
89 if self
.ttl
and self
.ttl
> 0 and now
- last_update
> self
.ttl
:
91 except (KeyError, AttributeError):
92 value
= self
.func(obj
)
95 except AttributeError:
96 cache
= obj
._cache
= {}
97 cache
[self
.__name
__] = (value
, now
)
101 def __delete__(self
, obj
):
103 del obj
._cache
[self
.__name
__]
104 except (KeyError, AttributeError):
107 # Aliases to make cached_property_with_ttl easier to use
108 cached_property_ttl
= cached_property_with_ttl
109 timed_cached_property
= cached_property_with_ttl
112 class threaded_cached_property_with_ttl(cached_property_with_ttl
):
114 A cached_property version for use in environments where multiple threads
115 might concurrently try to access the property.
118 def __init__(self
, ttl
=None):
119 super(threaded_cached_property_with_ttl
, self
).__init
__(ttl
)
120 self
.lock
= threading
.RLock()
122 def __get__(self
, obj
, cls
):
124 # Double check if the value was computed before the lock was
126 prop_name
= self
.__name
__
127 if hasattr(obj
, '_cache') and prop_name
in obj
._cache
:
128 return obj
._cache
[prop_name
][0]
130 # If not, do the calculation and release the lock.
131 return super(threaded_cached_property_with_ttl
, self
).__get
__(obj
,
134 # Alias to make threaded_cached_property_with_ttl easier to use
135 threaded_cached_property_ttl
= threaded_cached_property_with_ttl
136 timed_threaded_cached_property
= threaded_cached_property_with_ttl