author | Claus Gittinger <cg@exept.de> |
Wed, 18 Mar 2020 10:11:21 +0100 | |
changeset 5468 | e9da15c1a36c |
parent 4761 | 6e945a9ab49d |
permissions | -rw-r--r-- |
4759
847dd7b8f285
#DOCUMENTATION by cgexept.de
Claus Gittinger <cg@exept.de>
parents:
4758
diff
changeset
|
1 |
"{ Encoding: utf8 }" |
847dd7b8f285
#DOCUMENTATION by cgexept.de
Claus Gittinger <cg@exept.de>
parents:
4758
diff
changeset
|
2 |
|
2835 | 3 |
" |
4 |
COPYRIGHT (c) 2012 by eXept Software AG |
|
5 |
All Rights Reserved |
|
6 |
||
7 |
This software is furnished under a license and may be used |
|
8 |
only in accordance with the terms of that license and with the |
|
9 |
inclusion of the above copyright notice. This software may not |
|
10 |
be provided or otherwise made available to, or used by, any |
|
11 |
other person. No title to or ownership of the software is |
|
12 |
hereby transferred. |
|
13 |
" |
|
14 |
"{ Package: 'stx:libbasic2' }" |
|
15 |
||
4758 | 16 |
"{ NameSpace: Smalltalk }" |
17 |
||
2835 | 18 |
Object subclass:#CachedValue |
19 |
instanceVariableNames:'value expirationTime computation validityDuration' |
|
20 |
classVariableNames:'' |
|
21 |
poolDictionaries:'' |
|
22 |
category:'Kernel-Processes' |
|
23 |
! |
|
24 |
||
25 |
NotFoundError subclass:#ValueExpiredException |
|
26 |
instanceVariableNames:'' |
|
27 |
classVariableNames:'' |
|
28 |
poolDictionaries:'' |
|
29 |
privateIn:CachedValue |
|
30 |
! |
|
31 |
||
32 |
!CachedValue class methodsFor:'documentation'! |
|
33 |
||
34 |
copyright |
|
35 |
" |
|
36 |
COPYRIGHT (c) 2012 by eXept Software AG |
|
37 |
All Rights Reserved |
|
38 |
||
39 |
This software is furnished under a license and may be used |
|
40 |
only in accordance with the terms of that license and with the |
|
41 |
inclusion of the above copyright notice. This software may not |
|
42 |
be provided or otherwise made available to, or used by, any |
|
43 |
other person. No title to or ownership of the software is |
|
44 |
hereby transferred. |
|
45 |
" |
|
46 |
! |
|
47 |
||
48 |
documentation |
|
49 |
" |
|
50 |
Instances of CachedValue can be used for values which are costly to evaluate, |
|
51 |
but which can be cached for some time. |
|
52 |
For example, when asking a source code repository for the set of symbolic names, |
|
53 |
this query takes a few seconds (a CVS roundtrip). However, this information can easily |
|
54 |
be cached and remembered for some time (say 30seconds or so), to speed up followup |
|
55 |
repository operations. |
|
56 |
You may find more similar uses of this class. |
|
57 |
||
58 |
[author:] |
|
59 |
Claus Gittinger |
|
60 |
||
61 |
[instance variables:] |
|
62 |
value ..................... the computed (cached value) |
|
63 |
expirationTime ............ timeStamp, when this value becomes obsolete |
|
64 |
validDuration ............. timeDuration, how long a computed value remains valid |
|
65 |
computation ............... a computation block, to recompute the value. |
|
66 |
||
67 |
[class variables:] |
|
68 |
||
69 |
[see also:] |
|
70 |
||
71 |
" |
|
72 |
! |
|
73 |
||
74 |
examples |
|
75 |
" |
|
76 |
[exBegin] |
|
77 |
|cv exceptionRaised didCompute| |
|
78 |
||
79 |
didCompute := false. |
|
80 |
cv := CachedValue compute:[didCompute := true. Date today dayOfWeek] validityDuration:(2 seconds). |
|
81 |
self assert:(cv isValid not). |
|
82 |
self assert:(didCompute not). |
|
83 |
self assert:(cv value = Date today dayOfWeek). |
|
84 |
self assert:(cv isValid). |
|
85 |
self assert:(didCompute). |
|
86 |
||
87 |
Delay waitForSeconds:3. |
|
88 |
didCompute := false. |
|
89 |
self assert:(cv isValid not). |
|
90 |
self assert:(didCompute not). |
|
91 |
self assert:(cv value = Date today dayOfWeek). |
|
92 |
self assert:(cv isValid). |
|
93 |
self assert:(didCompute). |
|
94 |
[exEnd] |
|
95 |
[exBegin] |
|
96 |
|cv exceptionRaised| |
|
97 |
||
98 |
cv := CachedValue value:123 expirationTime:(Timestamp now + 2 seconds). |
|
99 |
self assert:(cv isValid). |
|
100 |
Delay waitForSeconds:3. |
|
101 |
self assert:(cv isValid not). |
|
102 |
||
103 |
exceptionRaised := false. |
|
104 |
ValueExpiredException handle:[:ex | |
|
105 |
exceptionRaised := true. |
|
106 |
] do:[ |
|
107 |
cv value |
|
108 |
]. |
|
109 |
self assert:exceptionRaised. |
|
110 |
[exEnd] |
|
111 |
" |
|
112 |
! ! |
|
113 |
||
114 |
!CachedValue class methodsFor:'instance creation'! |
|
115 |
||
116 |
compute:actionBlock validityDuration:aTimeduration |
|
117 |
"return a 'self computing' cachedvalue, which ceases to be valid aTimeduration after |
|
118 |
it has computed its value. |
|
119 |
If asked again for a value, it will automatically recompute the value." |
|
120 |
||
121 |
^ self new |
|
122 |
compute:actionBlock validityDuration:aTimeduration |
|
123 |
! |
|
124 |
||
125 |
value:valueArg expirationTime:aTimestamp |
|
126 |
"return a 'one shot' cachedValue, which ceases to be valid at expirationTime. |
|
127 |
If asked for the value after that, an exception is raised |
|
128 |
(i.e. it does not automatically recompute a value)" |
|
129 |
||
130 |
^ self new |
|
131 |
value:valueArg expirationTime:aTimestamp |
|
132 |
! ! |
|
133 |
||
134 |
!CachedValue methodsFor:'accessing'! |
|
135 |
||
136 |
compute:computationBlock validityDuration:aTimeduration |
|
137 |
computation := computationBlock. |
|
138 |
validityDuration := aTimeduration |
|
139 |
! |
|
140 |
||
141 |
expirationTime:something |
|
142 |
expirationTime := something. |
|
143 |
! |
|
144 |
||
4758 | 145 |
forceInvalid |
4761 | 146 |
expirationTime := nil |
147 |
||
148 |
"Modified: / 05-11-2018 / 12:48:00 / Claus Gittinger" |
|
4758 | 149 |
! |
150 |
||
2835 | 151 |
value |
152 |
"return my value. If not yet computed or expired, recompute using the computation block. |
|
153 |
Otherwise return the cacehdValue" |
|
154 |
||
155 |
self isValid ifFalse:[ |
|
156 |
validityDuration isNil ifTrue:[ |
|
157 |
ValueExpiredException raiseRequestWith:self. |
|
158 |
^ nil |
|
159 |
]. |
|
2848 | 160 |
self computeCachedValue. |
2835 | 161 |
]. |
2848 | 162 |
^ value |
2835 | 163 |
! |
164 |
||
165 |
value:valueArg expirationTime:aTimestamp |
|
166 |
value := valueArg. |
|
167 |
expirationTime := aTimestamp |
|
2847 | 168 |
! |
169 |
||
170 |
valueOrIfInvalid:exceptionalValue |
|
171 |
"return my value, if valid. Otherwise, return the value from exceptionalValue." |
|
172 |
||
173 |
self isValid ifFalse:[^ exceptionalValue value]. |
|
2848 | 174 |
^ value |
2835 | 175 |
! ! |
176 |
||
177 |
!CachedValue methodsFor:'private'! |
|
178 |
||
179 |
computeCachedValue |
|
180 |
expirationTime := Timestamp now + validityDuration. |
|
181 |
value := computation value. |
|
182 |
! ! |
|
183 |
||
184 |
!CachedValue methodsFor:'queries'! |
|
185 |
||
186 |
isValid |
|
187 |
"true if the cached value is still valid" |
|
188 |
||
189 |
expirationTime notNil ifTrue:[ |
|
4759
847dd7b8f285
#DOCUMENTATION by cgexept.de
Claus Gittinger <cg@exept.de>
parents:
4758
diff
changeset
|
190 |
Timestamp now < expirationTime ifTrue:[ |
2835 | 191 |
^ true |
192 |
]. |
|
193 |
expirationTime := nil. |
|
194 |
]. |
|
195 |
^ false |
|
196 |
! ! |
|
197 |
||
198 |
!CachedValue::ValueExpiredException class methodsFor:'documentation'! |
|
199 |
||
200 |
documentation |
|
201 |
" |
|
202 |
raised when a cachedValue has expired |
|
203 |
" |
|
204 |
! ! |
|
205 |
||
206 |
!CachedValue::ValueExpiredException class methodsFor:'initialization'! |
|
207 |
||
208 |
initialize |
|
209 |
NotifierString := 'value expired'. |
|
210 |
! ! |
|
211 |
||
212 |
!CachedValue class methodsFor:'documentation'! |
|
213 |
||
214 |
version |
|
4758 | 215 |
^ '$Header$' |
2835 | 216 |
! |
217 |
||
218 |
version_CVS |
|
4758 | 219 |
^ '$Header$' |
2835 | 220 |
! ! |
221 |
||
4758 | 222 |
|
2835 | 223 |
CachedValue::ValueExpiredException initialize! |