@@ -6,7 +6,7 @@ export default class Cache {
66 #cache = null ;
77 #config = {
88 ttl : 0 ,
9- maxLength : 250 ,
9+ maxLength : 0 ,
1010 interval : 0 ,
1111 intervalId : null ,
1212 enableInterval : false ,
@@ -78,6 +78,7 @@ export default class Cache {
7878 }
7979
8080 set ( key , value , options = { } ) {
81+ // Insert a new node at head
8182 if (
8283 ( typeof options . ttl !== "undefined" && typeof options . ttl !== "number" ) ||
8384 options . ttl < 0
@@ -100,10 +101,9 @@ export default class Cache {
100101 nodeValue . expiresAt = nodeValue . createdAt + nodeValue . ttl ;
101102 }
102103
103- // Insert a new node at head
104- const existingNode = this . #cache. get ( key ) ;
105104 // Update node data if node is already exists
106- if ( typeof existingNode !== "undefined" ) {
105+ if ( this . #cache. has ( key ) ) {
106+ const existingNode = this . #cache. get ( key ) ;
107107 existingNode . value = nodeValue ;
108108 // Move current node to the head
109109 this . #linkedList. setHead ( existingNode ) ;
@@ -124,13 +124,12 @@ export default class Cache {
124124 throw new TypeError ( "callback should be a function" ) ;
125125 }
126126
127- const node = this . #cache. get ( key ) ;
128-
129- if ( typeof node !== "undefined" ) {
127+ if ( this . #cache. has ( key ) ) {
128+ const node = this . #cache. get ( key ) ;
130129 // Check node is live or not
131130 if ( this . #isStale( node ) ) {
132131 this . delete ( key ) ;
133- throw new Error ( key + " Key not found" ) ;
132+ throw new Error ( key + " key not found" ) ;
134133 }
135134
136135 // Move current node to the head
@@ -143,7 +142,7 @@ export default class Cache {
143142 }
144143 }
145144
146- throw new Error ( key + " Key not found" ) ;
145+ throw new Error ( key + " key not found" ) ;
147146 } catch ( err ) {
148147 if ( callback ) {
149148 return callback ( err , undefined ) ;
@@ -154,17 +153,16 @@ export default class Cache {
154153 }
155154
156155 delete ( key ) {
157- const node = this . #cache. get ( key ) ;
158-
159- if ( typeof node !== "undefined" ) {
156+ if ( this . #cache. has ( key ) ) {
157+ const node = this . #cache. get ( key ) ;
160158 this . #linkedList. delete ( node ) ;
161159 // Delete node
162160 this . #cache. delete ( key ) ;
163161 }
164162 }
165163
166164 #evict( ) {
167- if ( this . #linkedList . tail === null ) return ;
165+ if ( this . length === 0 ) return ;
168166 if ( this . length !== this . #config. maxLength ) return ;
169167 this . delete ( this . #linkedList. tail . value . key ) ;
170168 }
@@ -200,9 +198,8 @@ export default class Cache {
200198 }
201199
202200 has ( key ) {
203- const node = this . #cache. get ( key ) ;
204-
205- if ( typeof node !== "undefined" ) {
201+ if ( this . #cache. has ( key ) ) {
202+ const node = this . #cache. get ( key ) ;
206203 // Check node is live or not
207204 if ( this . #isStale( node ) ) {
208205 this . delete ( key ) ;
@@ -246,13 +243,8 @@ export default class Cache {
246243
247244 // Iterator to iterate over cache with a 'for...of' loop
248245 * [ Symbol . iterator ] ( ) {
249- let node = this . #linkedList. head ;
250- while ( node ) {
251- let next = node . next ;
252- if ( this . has ( node . value . key ) ) {
253- yield { [ node . value . key ] : node . value . value } ;
254- }
255- node = next ;
256- }
246+ this . forEach ( function ( data ) {
247+ yield data ;
248+ } ) ;
257249 }
258250}
0 commit comments