eXept Software AG Logo

Smalltalk/X Webserver

Documentation of class 'CacheDictionaryWithFactory':

Home

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

Class: CacheDictionaryWithFactory


Inheritance:

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

Package:
stx:libbasic2
Category:
Collections-Unordered
Version:
rev: 1.3 date: 2016/10/13 22:09:33
user: cg
file: CacheDictionaryWithFactory.st directory: libbasic2
module: stx stc-classLibrary: libbasic2
Author:
Claus Gittinger (cg@alan)

Description:


like a Dictionary, but does not grow beyond a given max. size
(i.e. only keeps size items),
It can be used as a cache, for keeping recently used objects alive.
Must be created with an initial (=maximal) size. 
In addition to the normal CacheDictionary, 
this also keeps a factoryBlock to automatically compute missing elements.


Related information:

    CacheDictionary
    Dictionary

Class protocol:

instance creation
o  new: cacheSize factory: aBlock


Instance protocol:

accessing
o  at: key

initialization
o  factory: aBlock


Examples:


caches reverse strings (stupid usage)
  |c|

  c := CacheDictionaryWithFactory
          new:100 factory:[:key | key reversed].

  c at:'hello'.
  c at:'hello'.
  1 to:1000 do:[:i | c at:i printString].
  c at:'hello'.
Test: should halt (compute 'hello') only twice.
  |c numHalts|

  numHalts := 0.

  c := CacheDictionaryWithFactory
          new:100 factory:[:key | key = 'hello' ifTrue:[numHalts := numHalts + 1]. key reversed].

  c at:'hello'.
  c at:'hello'.
  1 to:1000 do:[:i | c at:i printString].
  c at:'hello'.
  self assert:( numHalts == 2 ).


ST/X 7.1.0.0; WebServer 1.663 at exept.de:8081; Mon, 19 Nov 2018 09:09:00 GMT