eXept Software AG Logo

Smalltalk/X Webserver

Documentation of class 'CacheDictionaryWithLimitedLifetime':

Home

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

Class: CacheDictionaryWithLimitedLifetime


Inheritance:

   Object
   |
   +--Collection
      |
      +--Set
         |
         +--Dictionary
            |
            +--CacheDictionary
               |
               +--CacheDictionaryWithLimitedLifetime

Package:
stx:libbasic2
Category:
Collections-Unordered
Version:
rev: 1.5 date: 2022/10/10 10:24:44
user: cg
file: CacheDictionaryWithLimitedLifetime.st directory: libbasic2
module: stx stc-classLibrary: libbasic2
Author:
cg

Description:


this is like a cacheDictionary (in that it keeps only a predefined, limited
numer of entries) but it also 'forgets' entries after some time period.
This is useful to temporarily remember eg. mime types of files or other
possibly changing information which is otherwise expensive to compute.

Warning:
    this class only supports a limited subset of the dictionary protocol;
    only use: at:put / at: / at:ifAbsent: / includesKey: / size


[example:]
    |d|

    d := CacheDictionaryWithLimitedLifetime new:30 liveTime:3 seconds.
    d at:'hello' put:'world'.
    self assert:(d includesKey:'hello').
    self assert:(d at:'hello' ifAbsent:nil) = 'world'.
    self assert:(d size == 1).
    Delay waitForSeconds:1.
    self assert:(d size == 1).
    self assert:(d at:'hello' ifAbsent:nil) = 'world'.
    Delay waitForSeconds:1.
    self assert:(d size == 1).
    self assert:(d at:'hello' ifAbsent:nil) = 'world'.
    Delay waitForSeconds:2.
    self assert:(d includesKey:'hello') not.
    self assert:(d at:'hello' ifAbsent:nil) isNil.
    self assert:(d size == 0).
    d inspect

    |d|

    d := CacheDictionaryWithLimitedLifetime new:30 liveTime:3 seconds.
    d reanimateOnRead:true.
    d at:'hello' put:'world'.
    self assert:(d includesKey:'hello').
    self assert:(d at:'hello' ifAbsent:nil) = 'world'.
    self assert:(d size == 1).
    Delay waitForSeconds:2.
    self assert:(d size == 1).
    self assert:(d at:'hello' ifAbsent:nil) = 'world'.
    Delay waitForSeconds:2.
    self assert:(d size == 1).
    self assert:(d at:'hello' ifAbsent:nil) = 'world'.
    Delay waitForSeconds:2.
    self assert:(d size == 1).
    self assert:(d at:'hello' ifAbsent:nil) = 'world'.
    Delay waitForSeconds:4.
    self assert:(d includesKey:'hello') not.
    self assert:(d at:'hello' ifAbsent:nil) isNil.
    self assert:(d size == 0).
    d inspect


Class protocol:

instance creation
o  new: size
self new:30 liveTime:2 seconds

Usage example(s):

     self new:30 liveTime:2 seconds

o  new: size liveTime: liveTime
return an instance which caches at most size elements,
for a max. of liveTime

Usage example(s):

     |d|
     
     d := self new:30 liveTime:2 seconds.
     d at:'foo' put:'fooValue'.
     self assert:(d at:'foo' ifAbsent:nil) = 'fooValue'.
     Delay waitForSeconds:1.
     self assert:(d at:'foo' ifAbsent:nil) = 'fooValue'.
     Delay waitForSeconds:2.
     self assert:(d at:'foo' ifAbsent:nil) isNil.


Instance protocol:

accessing
o  liveTime
how long will entries be alive

o  liveTime: aTimeDuration
how long will entries be alive

o  reanimateOnRead: aBoolean
If reanimateOnRead is set, elements will get another liveTime
whenever read from the array.
By default, reanimateOnRead is false,
so elements loose their live after the liveTime even if accessed in the meantime

private
o  find: key ifAbsent: aBlock
(comment from inherited method)
Look for the key in the receiver. If it is found, return
the index of the slot containing the key, otherwise
return the value of evaluating aBlock.

o  valueContainerOfSize: n
return a container for values of size n.
Extracted to make life of weak subclasses easier ...


Private classes:

    ArrayWithLimitedLiveTime


ST/X 7.7.0.0; WebServer 1.702 at 20f6060372b9.unknown:8081; Sat, 21 Dec 2024 16:43:58 GMT