11/**
22 * A FIFO cache to hold key-pair mappings. Its capacity will be at least the initialCapacity
3- * supplied on creation, which you can increase by increasing the "capacity" property.
4- *
3+ * supplied on creation, which you can increase by increasing the "capacity" property.
4+ *
55 * One extra element beyond the set capacity will be stored which can be fetched by calling "recycle()".
6- * This allows the oldest value to be removed in order to be reused, instead of leaving it to be collected
6+ * This allows the oldest value to be removed in order to be reused, instead of leaving it to be collected
77 * by the garbage collector.
8- *
8+ *
99 * Element age is determined by the time it was added or last get()'d from the cache.
1010 */
1111export function FIFOCache ( initialCapacity ) {
12- //Private:
13- var
14- queue = [ ] ,
15- items = { } ;
12+ //Private:
13+ let queue = [ ] ,
14+ items = { } ;
1615
17- function removeFromQueue ( key ) {
18- for ( var i = 0 ; i < queue . length ; i ++ ) {
19- if ( queue [ i ] == key ) {
20- //Assume there's only one copy to remove:
21- for ( var j = i ; j < queue . length - 1 ; j ++ ) {
22- queue [ j ] = queue [ j + 1 ] ;
23- }
24-
25- queue . length -- ;
26- break ;
27- }
16+ function removeFromQueue ( key ) {
17+ for ( let i = 0 ; i < queue . length ; i ++ ) {
18+ if ( queue [ i ] == key ) {
19+ //Assume there's only one copy to remove:
20+ for ( let j = i ; j < queue . length - 1 ; j ++ ) {
21+ queue [ j ] = queue [ j + 1 ] ;
2822 }
23+
24+ queue . length -- ;
25+ break ;
26+ }
2927 }
30-
31- //Public:
32- this . capacity = initialCapacity ;
33-
34- /**
35- * Remove and return the oldest value from the cache to be reused, or null if the cache wasn't full.
36- */
37- this . recycle = function ( ) {
38- if ( queue . length > this . capacity ) {
39- var
40- key = queue . shift ( ) ,
41- result = items [ key ] ;
42-
43- delete items [ key ] ;
44-
45- return result ;
46- }
47-
48- return null ;
49- } ;
50-
51- /**
52- * Add a mapping for the given key to the cache. If an existing value with that key was
53- * present, it will be overwritten.
54- */
55- this . add = function ( key , value ) {
56- // Was this already cached? Bump it back up to the end of the queue
57- if ( items [ key ] !== undefined )
58- removeFromQueue ( key ) ;
59-
60- queue . push ( key ) ;
61-
62- items [ key ] = value ;
63-
64- while ( queue . length > this . capacity + 1 ) {
65- delete items [ queue . shift ( ) ] ;
66- }
67- } ;
68-
69- /**
70- * Return the value in the cache that corresponds to the given key, or undefined if it has
71- * expired or had never been stored.
72- */
73- this . get = function ( key ) {
74- var item = items [ key ] ;
75-
76- if ( item ) {
77- removeFromQueue ( key ) ;
78- queue . push ( key ) ;
79- }
80-
81- return item ;
82- } ;
83-
84- /**
85- * Erase the entire content of the cache
86- */
87- this . clear = function ( ) {
88- queue = [ ] ;
89- items = { } ;
90- } ;
91- }
28+ }
29+
30+ //Public:
31+ this . capacity = initialCapacity ;
32+
33+ /**
34+ * Remove and return the oldest value from the cache to be reused, or null if the cache wasn't full.
35+ */
36+ this . recycle = function ( ) {
37+ if ( queue . length > this . capacity ) {
38+ let key = queue . shift ( ) ,
39+ result = items [ key ] ;
40+
41+ delete items [ key ] ;
42+
43+ return result ;
44+ }
45+
46+ return null ;
47+ } ;
48+
49+ /**
50+ * Add a mapping for the given key to the cache. If an existing value with that key was
51+ * present, it will be overwritten.
52+ */
53+ this . add = function ( key , value ) {
54+ // Was this already cached? Bump it back up to the end of the queue
55+ if ( items [ key ] !== undefined ) removeFromQueue ( key ) ;
56+
57+ queue . push ( key ) ;
58+
59+ items [ key ] = value ;
60+
61+ while ( queue . length > this . capacity + 1 ) {
62+ delete items [ queue . shift ( ) ] ;
63+ }
64+ } ;
65+
66+ /**
67+ * Return the value in the cache that corresponds to the given key, or undefined if it has
68+ * expired or had never been stored.
69+ */
70+ this . get = function ( key ) {
71+ let item = items [ key ] ;
72+
73+ if ( item ) {
74+ removeFromQueue ( key ) ;
75+ queue . push ( key ) ;
76+ }
77+
78+ return item ;
79+ } ;
80+
81+ /**
82+ * Erase the entire content of the cache
83+ */
84+ this . clear = function ( ) {
85+ queue = [ ] ;
86+ items = { } ;
87+ } ;
88+ }
0 commit comments