1
- import { List , Map , OrderedMap , Record } from 'immutable'
1
+ import { List , Map , OrderedSet , Record } from 'immutable'
2
2
3
3
export const CacheEntry = Record ( {
4
4
value : null ,
@@ -100,11 +100,10 @@ export class BasicCache {
100
100
*/
101
101
export class LRUCache {
102
102
103
- constructor ( limit = 1000 , cache = new BasicCache ( ) , lru = OrderedMap ( ) , tick = 0 ) {
103
+ constructor ( limit = 1000 , cache = new BasicCache ( ) , lru = OrderedSet ( ) ) {
104
104
this . limit = limit ;
105
105
this . cache = cache ;
106
106
this . lru = lru ;
107
- this . tick = tick ;
108
107
}
109
108
110
109
/**
@@ -141,14 +140,12 @@ export class LRUCache {
141
140
* @return {LRUCache }
142
141
*/
143
142
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
147
144
const lru = this . cache . lookup ( item ) ?
148
- this . lru . remove ( item ) . set ( item , nextTick ) :
145
+ this . lru . remove ( item ) . add ( item ) :
149
146
this . lru ;
150
147
151
- return new LRUCache ( this . limit , this . cache , lru , nextTick )
148
+ return new LRUCache ( this . limit , this . cache , lru )
152
149
}
153
150
154
151
/**
@@ -159,24 +156,20 @@ export class LRUCache {
159
156
* @return {LRUCache }
160
157
*/
161
158
miss ( item , entry ) {
162
- const nextTick = this . tick + 1 ;
163
-
164
159
if ( this . lru . size >= this . limit ) {
165
160
// 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 ( )
167
162
168
163
return new LRUCache (
169
164
this . limit ,
170
165
this . cache . evict ( evictItem ) . miss ( item , entry ) ,
171
- this . lru . remove ( evictItem ) . set ( item , nextTick ) ,
172
- nextTick
166
+ this . lru . remove ( evictItem ) . add ( item )
173
167
)
174
168
} else {
175
169
return new LRUCache (
176
170
this . limit ,
177
171
this . cache . miss ( item , entry ) ,
178
- this . lru . set ( item , nextTick ) ,
179
- nextTick
172
+ this . lru . add ( item )
180
173
)
181
174
}
182
175
}
@@ -194,8 +187,7 @@ export class LRUCache {
194
187
return new LRUCache (
195
188
this . limit ,
196
189
this . cache . evict ( item ) ,
197
- this . lru . remove ( item ) ,
198
- this . tick + 1
190
+ this . lru . remove ( item )
199
191
)
200
192
}
201
193
}
0 commit comments