CachedValue.st
changeset 2835 88ddcdf93432
child 2847 7a563655c3b6
equal deleted inserted replaced
2834:6b92fee1b8af 2835:88ddcdf93432
       
     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         ].
       
   150         self computeCachedValue
       
   151     ].
       
   152     ^ value
       
   153 !
       
   154 
       
   155 value:valueArg expirationTime:aTimestamp
       
   156     value := valueArg.
       
   157     expirationTime := aTimestamp
       
   158 ! !
       
   159 
       
   160 !CachedValue methodsFor:'private'!
       
   161 
       
   162 computeCachedValue
       
   163     expirationTime := Timestamp now + validityDuration.
       
   164     value := computation value.
       
   165 ! !
       
   166 
       
   167 !CachedValue methodsFor:'queries'!
       
   168 
       
   169 isValid
       
   170     "true if the cached value is still valid"
       
   171 
       
   172     expirationTime notNil ifTrue:[
       
   173         expirationTime > Timestamp now ifTrue:[
       
   174             ^ true
       
   175         ].
       
   176         expirationTime := nil.
       
   177     ].
       
   178     ^ false
       
   179 ! !
       
   180 
       
   181 !CachedValue::ValueExpiredException class methodsFor:'documentation'!
       
   182 
       
   183 documentation
       
   184 "
       
   185     raised when a cachedValue has expired
       
   186 "
       
   187 ! !
       
   188 
       
   189 !CachedValue::ValueExpiredException class methodsFor:'initialization'!
       
   190 
       
   191 initialize
       
   192     NotifierString := 'value expired'.
       
   193 ! !
       
   194 
       
   195 !CachedValue class methodsFor:'documentation'!
       
   196 
       
   197 version
       
   198     ^ '$Header: /cvs/stx/stx/libbasic2/CachedValue.st,v 1.1 2012-10-31 11:59:16 cg Exp $'
       
   199 !
       
   200 
       
   201 version_CVS
       
   202     ^ '$Header: /cvs/stx/stx/libbasic2/CachedValue.st,v 1.1 2012-10-31 11:59:16 cg Exp $'
       
   203 ! !
       
   204 
       
   205 CachedValue::ValueExpiredException initialize!