memory-cache-node
Advanced tools
Comparing version 1.3.0 to 1.4.0
@@ -25,6 +25,5 @@ export declare type ValueWrapper<V> = { | ||
exportItemsToJson(): string; | ||
importPermanentItemsFrom(json: string): void; | ||
importExpiringItemsFrom(json: string, timeToLiveInSecs: number): void; | ||
importItemsFrom(json: string): void; | ||
private readonly deleteExpiredItems; | ||
private deleteExpiredItemsFromBatch; | ||
} |
@@ -72,18 +72,22 @@ "use strict"; | ||
exportItemsToJson() { | ||
const itemsObject = Array.from(this.itemKeyToValueWrapperMap.entries()).reduce((object, [key, { value }]) => ({ | ||
...object, | ||
[key]: value, | ||
const itemsObject = Array.from(this.itemKeyToValueWrapperMap.entries()).reduce((itemsObject, [key, valueWrapper]) => ({ | ||
...itemsObject, | ||
[key]: valueWrapper, | ||
}), {}); | ||
return JSON.stringify(itemsObject); | ||
} | ||
importPermanentItemsFrom(json) { | ||
Object.entries(JSON.parse(json)).forEach(([key, value]) => { | ||
this.storePermanentItem(key, value); | ||
importItemsFrom(json) { | ||
Object.entries(JSON.parse(json)).forEach(([key, { value, expirationTimestampInMillis }]) => { | ||
if (this.timer === null) { | ||
throw new Error('Cache is destroyed. Cannot store items anymore.'); | ||
} | ||
if (this.itemCount < this.maxItemCount) { | ||
this.itemKeyToValueWrapperMap.set(key, { | ||
value, | ||
expirationTimestampInMillis | ||
}); | ||
this.itemCount++; | ||
} | ||
}); | ||
} | ||
importExpiringItemsFrom(json, timeToLiveInSecs) { | ||
Object.entries(JSON.parse(json)).forEach(([key, value]) => { | ||
this.storeExpiringItem(key, value, timeToLiveInSecs); | ||
}); | ||
} | ||
deleteExpiredItemsFromBatch(iterator, currentTimestampInMillisSinceEpoch) { | ||
@@ -90,0 +94,0 @@ for (let i = 0; i < MemoryCache.EXPIRATION_PROCESSING_ITEM_BATCH_SIZE; i++) { |
@@ -151,3 +151,6 @@ "use strict"; | ||
memoryCache.storePermanentItem('key2', 2); | ||
expect(JSON.parse(memoryCache.exportItemsToJson())).toEqual({ key: 1, key2: 2 }); | ||
expect(JSON.parse(memoryCache.exportItemsToJson())).toEqual({ | ||
key: { value: 1 }, | ||
key2: { value: 2 }, | ||
}); | ||
}); | ||
@@ -158,21 +161,14 @@ }); | ||
memoryCache = new MemoryCache_1.default(1, 10); | ||
memoryCache.importPermanentItemsFrom('{ "key": 1, "key2": 2 }'); | ||
const itemsObject = { | ||
key: { value: 1, expirationTimestampInMillis: 1 }, | ||
key2: { value: 2, expirationTimestampInMillis: 2 }, | ||
}; | ||
memoryCache.importItemsFrom(JSON.stringify(itemsObject)); | ||
expect(memoryCache.getItemCount()).toBe(2); | ||
expect(memoryCache.retrieveItemValue('key')).toEqual(1); | ||
expect(memoryCache.retrieveItemValue('key2')).toEqual(2); | ||
expect(memoryCache.getItemExpirationTimestampInMillisSinceEpoch('key')).toBeUndefined(); | ||
expect(memoryCache.getItemExpirationTimestampInMillisSinceEpoch('key2')).toBeUndefined(); | ||
expect(memoryCache.retrieveItemValue('key')).toBe(1); | ||
expect(memoryCache.retrieveItemValue('key2')).toBe(2); | ||
expect(memoryCache.getItemExpirationTimestampInMillisSinceEpoch('key')).toBe(1); | ||
expect(memoryCache.getItemExpirationTimestampInMillisSinceEpoch('key2')).toBe(2); | ||
}); | ||
}); | ||
describe('importExpiringItemsFrom', () => { | ||
it('should add items from JSON to the cache', () => { | ||
memoryCache = new MemoryCache_1.default(1, 10); | ||
memoryCache.importExpiringItemsFrom('{ "key": 1, "key2": 2 }', 10); | ||
expect(memoryCache.getItemCount()).toBe(2); | ||
expect(memoryCache.retrieveItemValue('key')).toEqual(1); | ||
expect(memoryCache.retrieveItemValue('key2')).toEqual(2); | ||
expect(memoryCache.getItemExpirationTimestampInMillisSinceEpoch('key')).toBeDefined(); | ||
expect(memoryCache.getItemExpirationTimestampInMillisSinceEpoch('key2')).toBeDefined(); | ||
}); | ||
}); | ||
describe('clear', () => { | ||
@@ -179,0 +175,0 @@ it('it should clear the cache', () => { |
{ | ||
"name": "memory-cache-node", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"description": "Fast, modern and event loop non-blocking memory cache for Node.js and browse", | ||
@@ -5,0 +5,0 @@ "author": { |
@@ -157,5 +157,4 @@ # memory-cache-node | ||
// Use below two functions only with JSON output from exportItemsToJson method | ||
importPermanentItemsFrom(json: string): void; // Can throw if JSON is invalid | ||
importExpiringItemsFrom(json: string, timeToLiveInSecs: number): void; // Can throw if JSON is invalid | ||
// Use below function only with JSON output from exportItemsToJson method | ||
importItemsFrom(json: string): void; // Can throw if JSON is invalid | ||
@@ -162,0 +161,0 @@ clear(): void; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
60839
394
177