1- import { List , Map , OrderedMap , Record } from 'immutable'
1+ import { List , Map , OrderedSet , Record } from 'immutable'
22
33export const CacheEntry = Record ( {
44 value : null ,
@@ -100,11 +100,10 @@ export class BasicCache {
100100 */
101101export class LRUCache {
102102
103- constructor ( limit = 1000 , cache = new BasicCache ( ) , lru = OrderedMap ( ) , tick = 0 ) {
103+ constructor ( limit = 1000 , cache = new BasicCache ( ) , lru = OrderedSet ( ) ) {
104104 this . limit = limit ;
105105 this . cache = cache ;
106106 this . lru = lru ;
107- this . tick = tick ;
108107 }
109108
110109 /**
@@ -141,14 +140,12 @@ export class LRUCache {
141140 * @return {LRUCache }
142141 */
143142 hit ( item ) {
144- const nextTick = this . tick + 1 ;
145-
146- // if item exists, remove it first to reorder in lru OrderedMap
143+ // if item exists, remove it first to reorder in lru OrderedSet
147144 const lru = this . cache . lookup ( item ) ?
148- this . lru . remove ( item ) . set ( item , nextTick ) :
145+ this . lru . remove ( item ) . add ( item ) :
149146 this . lru ;
150147
151- return new LRUCache ( this . limit , this . cache , lru , nextTick )
148+ return new LRUCache ( this . limit , this . cache , lru )
152149 }
153150
154151 /**
@@ -159,24 +156,20 @@ export class LRUCache {
159156 * @return {LRUCache }
160157 */
161158 miss ( item , entry ) {
162- const nextTick = this . tick + 1 ;
163-
164159 if ( this . lru . size >= this . limit ) {
165160 // TODO add options to clear multiple items at once
166- const evictItem = this . has ( item ) ? item : this . lru . keySeq ( ) . first ( )
161+ const evictItem = this . has ( item ) ? item : this . lru . first ( )
167162
168163 return new LRUCache (
169164 this . limit ,
170165 this . cache . evict ( evictItem ) . miss ( item , entry ) ,
171- this . lru . remove ( evictItem ) . set ( item , nextTick ) ,
172- nextTick
166+ this . lru . remove ( evictItem ) . add ( item )
173167 )
174168 } else {
175169 return new LRUCache (
176170 this . limit ,
177171 this . cache . miss ( item , entry ) ,
178- this . lru . set ( item , nextTick ) ,
179- nextTick
172+ this . lru . add ( item )
180173 )
181174 }
182175 }
@@ -194,8 +187,7 @@ export class LRUCache {
194187 return new LRUCache (
195188 this . limit ,
196189 this . cache . evict ( item ) ,
197- this . lru . remove ( item ) ,
198- this . tick + 1
190+ this . lru . remove ( item )
199191 )
200192 }
201193}
0 commit comments