2009-02-10 03:45:43 -05:00
|
|
|
! Copyright (C) 2009 Slava Pestov.
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2013-03-06 12:17:35 -05:00
|
|
|
USING: accessors assocs continuations destructors fry kernel
|
|
|
|
make math sequences ;
|
2009-02-10 03:45:43 -05:00
|
|
|
IN: cache
|
|
|
|
|
2009-08-24 03:26:13 -04:00
|
|
|
TUPLE: cache-assoc < disposable assoc max-age ;
|
2009-02-10 03:45:43 -05:00
|
|
|
|
|
|
|
: <cache-assoc> ( -- cache )
|
2009-08-24 03:26:13 -04:00
|
|
|
cache-assoc new-disposable H{ } clone >>assoc 10 >>max-age ;
|
2009-02-10 03:45:43 -05:00
|
|
|
|
2009-02-10 23:05:13 -05:00
|
|
|
<PRIVATE
|
|
|
|
|
|
|
|
TUPLE: cache-entry value age ;
|
|
|
|
|
|
|
|
: <cache-entry> ( value -- entry ) 0 cache-entry boa ; inline
|
|
|
|
|
|
|
|
M: cache-entry dispose value>> dispose ;
|
|
|
|
|
2009-02-10 03:45:43 -05:00
|
|
|
M: cache-assoc assoc-size assoc>> assoc-size ;
|
|
|
|
|
2009-02-10 23:05:13 -05:00
|
|
|
M: cache-assoc at* assoc>> at* [ dup [ 0 >>age value>> ] when ] dip ;
|
2009-02-10 03:45:43 -05:00
|
|
|
|
2009-02-10 23:05:13 -05:00
|
|
|
M: cache-assoc set-at
|
2014-11-21 11:19:05 -05:00
|
|
|
check-disposed
|
2009-02-10 23:05:13 -05:00
|
|
|
[ <cache-entry> ] 2dip
|
|
|
|
assoc>> set-at ;
|
2009-02-10 03:45:43 -05:00
|
|
|
|
2010-10-26 00:52:45 -04:00
|
|
|
M: cache-assoc clear-assoc
|
2013-03-06 12:17:35 -05:00
|
|
|
assoc>> [ values dispose-each ] [ clear-assoc ] bi ;
|
2009-02-10 03:45:43 -05:00
|
|
|
|
2009-02-10 23:05:13 -05:00
|
|
|
M: cache-assoc >alist assoc>> [ value>> ] { } assoc-map-as ;
|
2009-02-10 03:45:43 -05:00
|
|
|
|
|
|
|
INSTANCE: cache-assoc assoc
|
|
|
|
|
2010-10-26 00:52:45 -04:00
|
|
|
M: cache-assoc dispose* clear-assoc ;
|
2009-02-10 23:05:13 -05:00
|
|
|
|
|
|
|
PRIVATE>
|
|
|
|
|
2009-02-10 03:45:43 -05:00
|
|
|
: purge-cache ( cache -- )
|
2013-05-20 19:43:00 -04:00
|
|
|
[ assoc>> ] [ max-age>> ] bi V{ } clone [
|
|
|
|
'[
|
2013-03-06 12:17:35 -05:00
|
|
|
nip dup age>> 1 + [ >>age ] keep
|
2013-05-20 19:43:00 -04:00
|
|
|
_ < [ drop t ] [ _ dispose-to f ] if
|
2013-03-06 12:17:35 -05:00
|
|
|
] assoc-filter! drop
|
2013-05-20 19:43:00 -04:00
|
|
|
] keep [ last rethrow ] unless-empty ;
|