eXept Software AG Logo

Smalltalk/X Webserver

Documentation of class 'CachedValue':

Home

Documentation
www.exept.de
Everywhere
for:
[back]

Class: CachedValue


Inheritance:

   Object
   |
   +--CachedValue

Package:
stx:libbasic2
Category:
Kernel-Processes
Version:
rev: 1.10 date: 2023/05/30 13:48:43
user: matilk
file: CachedValue.st directory: libbasic2
module: stx stc-classLibrary: libbasic2

Description:


Instances of CachedValue can be used for values which are costly to evaluate,
but which can be cached for some time. 
For example, when asking a source code repository for the set of symbolic names,
this query takes a few seconds (a CVS roundtrip). However, this information can easily
be cached and remembered for some time (say 30seconds or so), to speed up followup
repository operations.
You may find more similar uses of this class.


[instance variables:]
    value ..................... the computed (cached value)
    expirationTime ............ timeStamp, when this value becomes obsolete
    validDuration ............. timeDuration, how long a computed value remains valid
    computation ............... a computation block, to recompute the value.

[class variables:]

copyright

COPYRIGHT (c) 2012 by eXept Software AG All Rights Reserved This software is furnished under a license and may be used only in accordance with the terms of that license and with the inclusion of the above copyright notice. This software may not be provided or otherwise made available to, or used by, any other person. No title to or ownership of the software is hereby transferred.

Class protocol:

instance creation
o  compute: actionBlock validityDuration: aTimeduration
return a 'self computing' cachedvalue, which ceases to be valid aTimeduration after
it has computed its value.
If asked again for a value, it will automatically recompute the value.

o  initialize
(comment from inherited method)
called only once - initialize signals

o  value: valueArg expirationTime: aTimestamp
return a 'one shot' cachedValue, which ceases to be valid at expirationTime.
If asked for the value after that, an exception is raised
(i.e. it does not automatically recompute a value)


Instance protocol:

accessing
o  compute: computationBlock validityDuration: aTimeduration

o  expirationTime: something

o  forceInvalid

o  value
return my value. If not yet computed or expired, recompute using the computation block.
Otherwise return the cacehdValue

o  value: valueArg expirationTime: aTimestamp

o  valueOrIfInvalid: exceptionalValue
return my value, if valid. Otherwise, return the value from exceptionalValue.

private
o  computeCachedValue

queries
o  isValid
true if the cached value is still valid


Private classes:

    ValueExpiredException

Examples:


        |cv exceptionRaised didCompute|

        didCompute := false.
        cv := CachedValue compute:[didCompute := true. Date today dayOfWeek] validityDuration:(2 seconds).
        self assert:(cv isValid not).
        self assert:(didCompute not).
        self assert:(cv value = Date today dayOfWeek).
        self assert:(cv isValid).
        self assert:(didCompute).

        Delay waitForSeconds:3.
        didCompute := false.
        self assert:(cv isValid not).
        self assert:(didCompute not).
        self assert:(cv value = Date today dayOfWeek).
        self assert:(cv isValid).
        self assert:(didCompute).
        |cv exceptionRaised|

        cv := CachedValue value:123 expirationTime:(Timestamp now + 2 seconds).
        self assert:(cv isValid).
        Delay waitForSeconds:3.
        self assert:(cv isValid not).

        exceptionRaised := false.
        ValueExpiredException handle:[:ex |
            exceptionRaised := true.
        ] do:[
            cv value
        ].
        self assert:exceptionRaised.


ST/X 7.7.0.0; WebServer 1.702 at 20f6060372b9.unknown:8081; Mon, 18 Nov 2024 04:33:07 GMT