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