secondary-cache
Advanced tools
+17
-17
@@ -54,6 +54,6 @@ import { ILRUCacheOptions, LRUCache } from './lru-cache'; | ||
| */ | ||
| hasFixed(id): boolean; | ||
| hasLRU(id): boolean; | ||
| deleteLRU(id, isInternal?: boolean): boolean; | ||
| delLRU(id, isInternal?: boolean): boolean; | ||
| hasFixed(id: any): boolean; | ||
| hasLRU(id: any): boolean; | ||
| deleteLRU(id: any, isInternal?: boolean): boolean; | ||
| delLRU(id: any, isInternal?: boolean): boolean; | ||
| /** | ||
@@ -66,3 +66,3 @@ * Removes an item from the fixed capacity cache. | ||
| */ | ||
| deleteFixed(id): boolean; | ||
| deleteFixed(id: any): boolean; | ||
| /** | ||
@@ -75,3 +75,3 @@ * Alias of deleteFixed | ||
| */ | ||
| delFixed(id): boolean; | ||
| delFixed(id: any): boolean; | ||
@@ -85,4 +85,4 @@ /** | ||
| */ | ||
| delete(id): boolean; | ||
| del(id): boolean; | ||
| delete(id: any): boolean; | ||
| del(id: any): boolean; | ||
@@ -96,5 +96,5 @@ /** | ||
| */ | ||
| getFixed(id): any; | ||
| getLRU(id): any; | ||
| peekLRU(id): any; | ||
| getFixed(id: any): any; | ||
| getLRU(id: any): any; | ||
| peekLRU(id: any): any; | ||
@@ -109,4 +109,4 @@ /** | ||
| */ | ||
| setFixed(id, value); | ||
| setLRU(id, value, expires?: number); | ||
| setFixed(id: any, value: any): void; | ||
| setLRU(id: any, value: any, expires?: number): void; | ||
@@ -124,3 +124,3 @@ /** | ||
| */ | ||
| set(id, value, options?: ICacheSetOptions|number); | ||
| set(id: any, value: any, options?: ICacheSetOptions|number): void; | ||
@@ -132,6 +132,6 @@ /** | ||
| */ | ||
| freeLRU(); | ||
| freeLRU(): void; | ||
| forEachFixed(callback: (value: any, id: any, thisArg: any)=>void, thisArg?: any): void; | ||
| reset(options?: ICacheOptions|number): Cache; | ||
| setDefaultOptionsLRU(options?: ILRUCacheOptions|number); | ||
| setDefaultOptionsLRU(options?: ILRUCacheOptions|number): void; | ||
@@ -143,3 +143,3 @@ /** | ||
| */ | ||
| setDefaultOptions(options?: ICacheOptions|number); | ||
| setDefaultOptions(options?: ICacheOptions|number): void; | ||
| /** | ||
@@ -146,0 +146,0 @@ * Get the number of items in the cache. |
@@ -65,4 +65,7 @@ import {LRUQueue} from './lru-queue'; | ||
| /** @internal */ | ||
| _cacheLRU: Object | ||
| /** @internal */ | ||
| _lruQueue: LRUQueue | ||
| /** @internal */ | ||
| _totalWeight: number; | ||
@@ -185,3 +188,3 @@ | ||
| */ | ||
| set(id: any, value: any, expires?: number); | ||
| set(id: any, value: any, expires?: number): any; | ||
@@ -199,7 +202,7 @@ /** | ||
| * Frees up the memory used by the cache. | ||
| * @returns {number} - Returns the timestamp of the last cleanup operation. | ||
| * @returns {void} - Returns nothing. | ||
| * @example | ||
| * cache.free(); // frees up the memory used by the cache | ||
| */ | ||
| free(); | ||
| free(): void; | ||
@@ -221,3 +224,3 @@ /** | ||
| */ | ||
| clearExpires(); | ||
| clearExpires(): void; | ||
@@ -229,3 +232,3 @@ /** | ||
| */ | ||
| setDefaultOptions(options?: ILRUCacheOptions|number); | ||
| setDefaultOptions(options?: ILRUCacheOptions|number): void; | ||
| /** | ||
@@ -232,0 +235,0 @@ * Get the number of items in the cache. |
@@ -88,2 +88,3 @@ /** | ||
| * @param callback - The function to call for each item in the queue. | ||
| * The second parameter will be `thisArg` if provided, otherwise the queue itself. | ||
| * @param [thisArg=this] - The value of `this` to use when calling the `callback` function. | ||
@@ -90,0 +91,0 @@ */ |
+1
-1
| { | ||
| "name": "secondary-cache", | ||
| "version": "2.1.0", | ||
| "version": "2.1.1", | ||
| "description": "support secondary cache mechanism. the first level cache is fixed memory-resident always with the highest priority. the second level is the LRU cache.", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/snowyu/secondary-cache.js", |
+105
-6
| ## Secondary Cache [](https://npmjs.org/package/secondary-cache) [](https://npmjs.org/package/secondary-cache) [](https://npmjs.org/package/secondary-cache) | ||
| It can support secondary cache mechanism. the first level cache is fixed memory-resident always with the highest priority. | ||
@@ -52,6 +51,80 @@ the second level is the LRU cache. | ||
| ### LRUCache API | ||
| * LRUCache(options|capacity): the LRU cache Class with expires supports. | ||
| * constructor(options?): creates a new LRUCache instance. | ||
| * options object: | ||
| * capacity: the LRU cache max capacity size, defaults to 1024. | ||
| * maxWeight: the maximum total weight of all items in cache. | ||
| * weightOf: a function(value, id) to calculate the weight of a value. | ||
| * expires: the default expires time (millisecond). | ||
| * cleanInterval: clean up expired item with a specified interval(seconds). | ||
| * lruCache.set(id, value, [expires]) | ||
| * add/update a key-value pair. returns the cache instance. | ||
| * lruCache.get(id) | ||
| * get value by id, updates "recently used"-ness. | ||
| * lruCache.peek(id) | ||
| * get value by id, does NOT update "recently used"-ness. | ||
| * lruCache.has(id) | ||
| * aliases: isExist, isExists | ||
| * check if id exists in cache (and not expired). | ||
| * lruCache.delete(id) | ||
| * alias: del | ||
| * delete a key from cache, returns true if deleted. | ||
| * lruCache.clear() | ||
| * clear all items, returns the cache instance. | ||
| * lruCache.free() | ||
| * free all memory used by the cache. | ||
| * lruCache.forEach(callback[, thisArg]) | ||
| * iterate over each item. callback receives (value, id, cache). | ||
| * lruCache.reset(options|capacity) | ||
| * clear cache and apply new options, returns the cache instance. | ||
| * lruCache.length() | ||
| * return the number of items in cache. | ||
| * lruCache.isExpired(item) | ||
| * check if item is expired, removes it if expired. returns boolean. | ||
| * lruCache.clearExpires() | ||
| * delete all expired items from cache. | ||
| * lruCache.weightOf(value, id) | ||
| * calculate weight of a value. defaults to return 1 (count-based). | ||
| * override to implement custom weight calculation (e.g., byte size). | ||
| * lruCache.on(event, listener) | ||
| * lruCache.off(event, listener) | ||
| * lruCache.delListener(event, listener) | ||
| * add/remove event listeners. | ||
| * lruCache.totalWeight (readonly property) | ||
| * the total weight of all items in cache (only meaningful when maxWeight > 0). | ||
| ### LRUQueue API | ||
| * LRUQueue(capacity): the LRU queue class for object queue item. | ||
| * add(id): add the id object to the queue. | ||
| * add(id): add the id object to the queue. Returns the removed item if capacity exceeded. | ||
| * push(id): alias of add. | ||
| * pop(): removes and returns the least recently used item from the queue. | ||
| * use(id): make the id object in the queue as most recently used items first. | ||
| * hit(value): check if value exists in queue, add if not, or move to most recently used. | ||
| * del(id): del the id object from the queue. | ||
| * delete(id): alias of del. | ||
| * clear(): removes all items from the queue. | ||
| * shiftLU(): shifts the least recently used position to the first used position. | ||
| * forEach(callback[, thisArg]): iterates over each item in the queue. | ||
| * callback receives (item, thisArg). If thisArg is provided, it's passed as second param; otherwise the queue itself is passed. | ||
| * Properties: | ||
| * maxCapacity: the maximum capacity of the queue. | ||
| * length: the current number of items in the queue. | ||
| * note: it used the `'lu'` property of the object queue item. | ||
@@ -87,3 +160,3 @@ | ||
| //or only use LRU Cache | ||
| // or only use LRU Cache | ||
| cache = new LRUCache(1000) | ||
@@ -182,9 +255,12 @@ | ||
| Deletes a key out of the cache. | ||
| Deletes a key out of the cache (searches both fixed and LRU caches). | ||
| ### cache.delLRU(key) | ||
| alias: deleteLRU | ||
| deletes a key from the secondary level LRU cache. | ||
| ### cache.delFixed(key) | ||
| alias: deleteFixed | ||
| deletes a key from the first level fixed cache. | ||
| ### cache.reset(options|capacity) | ||
@@ -219,3 +295,3 @@ | ||
| ### cache.setDefaultOptions(options: ICacheOptions|capacity); | ||
| ### cache.setDefaultOptions(options: ICacheOptions|capacity) | ||
@@ -227,1 +303,24 @@ Sets the default options for Cache. | ||
| return the number of items in the FixedCache and LRUCache. | ||
| ### cache.hasFixed(key) | ||
| check if key exists in the first level fixed cache. | ||
| ### cache.hasLRU(key) | ||
| check if key exists in the secondary level LRU cache. | ||
| ### cache.deleteLRU(key) | ||
| alias: delLRU | ||
| delete a key from the secondary level LRU cache. | ||
| ### cache.deleteFixed(key) | ||
| alias: delFixed | ||
| delete a key from the first level fixed cache. | ||
| ### cache.freeLRU() | ||
| free the secondary level LRU cache. | ||
| ### cache.setDefaultOptions(options|capacity) | ||
| sets the default options for the cache. | ||
| ### cache.setDefaultOptionsLRU(options|capacity) | ||
| sets the default options for the secondary level LRU cache. |
+17
-17
@@ -54,6 +54,6 @@ import { ILRUCacheOptions, LRUCache } from './lru-cache'; | ||
| */ | ||
| hasFixed(id): boolean; | ||
| hasLRU(id): boolean; | ||
| deleteLRU(id, isInternal?: boolean): boolean; | ||
| delLRU(id, isInternal?: boolean): boolean; | ||
| hasFixed(id: any): boolean; | ||
| hasLRU(id: any): boolean; | ||
| deleteLRU(id: any, isInternal?: boolean): boolean; | ||
| delLRU(id: any, isInternal?: boolean): boolean; | ||
| /** | ||
@@ -66,3 +66,3 @@ * Removes an item from the fixed capacity cache. | ||
| */ | ||
| deleteFixed(id): boolean; | ||
| deleteFixed(id: any): boolean; | ||
| /** | ||
@@ -75,3 +75,3 @@ * Alias of deleteFixed | ||
| */ | ||
| delFixed(id): boolean; | ||
| delFixed(id: any): boolean; | ||
@@ -85,4 +85,4 @@ /** | ||
| */ | ||
| delete(id): boolean; | ||
| del(id): boolean; | ||
| delete(id: any): boolean; | ||
| del(id: any): boolean; | ||
@@ -96,5 +96,5 @@ /** | ||
| */ | ||
| getFixed(id): any; | ||
| getLRU(id): any; | ||
| peekLRU(id): any; | ||
| getFixed(id: any): any; | ||
| getLRU(id: any): any; | ||
| peekLRU(id: any): any; | ||
@@ -109,4 +109,4 @@ /** | ||
| */ | ||
| setFixed(id, value); | ||
| setLRU(id, value, expires?: number); | ||
| setFixed(id: any, value: any): void; | ||
| setLRU(id: any, value: any, expires?: number): void; | ||
@@ -124,3 +124,3 @@ /** | ||
| */ | ||
| set(id, value, options?: ICacheSetOptions|number); | ||
| set(id: any, value: any, options?: ICacheSetOptions|number): void; | ||
@@ -132,6 +132,6 @@ /** | ||
| */ | ||
| freeLRU(); | ||
| freeLRU(): void; | ||
| forEachFixed(callback: (value: any, id: any, thisArg: any)=>void, thisArg?: any): void; | ||
| reset(options?: ICacheOptions|number): Cache; | ||
| setDefaultOptionsLRU(options?: ILRUCacheOptions|number); | ||
| setDefaultOptionsLRU(options?: ILRUCacheOptions|number): void; | ||
@@ -143,3 +143,3 @@ /** | ||
| */ | ||
| setDefaultOptions(options?: ICacheOptions|number); | ||
| setDefaultOptions(options?: ICacheOptions|number): void; | ||
| /** | ||
@@ -146,0 +146,0 @@ * Get the number of items in the cache. |
@@ -65,4 +65,7 @@ import {LRUQueue} from './lru-queue'; | ||
| /** @internal */ | ||
| _cacheLRU: Object | ||
| /** @internal */ | ||
| _lruQueue: LRUQueue | ||
| /** @internal */ | ||
| _totalWeight: number; | ||
@@ -185,3 +188,3 @@ | ||
| */ | ||
| set(id: any, value: any, expires?: number); | ||
| set(id: any, value: any, expires?: number): any; | ||
@@ -199,7 +202,7 @@ /** | ||
| * Frees up the memory used by the cache. | ||
| * @returns {number} - Returns the timestamp of the last cleanup operation. | ||
| * @returns {void} - Returns nothing. | ||
| * @example | ||
| * cache.free(); // frees up the memory used by the cache | ||
| */ | ||
| free(); | ||
| free(): void; | ||
@@ -221,3 +224,3 @@ /** | ||
| */ | ||
| clearExpires(); | ||
| clearExpires(): void; | ||
@@ -229,3 +232,3 @@ /** | ||
| */ | ||
| setDefaultOptions(options?: ILRUCacheOptions|number); | ||
| setDefaultOptions(options?: ILRUCacheOptions|number): void; | ||
| /** | ||
@@ -232,0 +235,0 @@ * Get the number of items in the cache. |
@@ -88,2 +88,3 @@ /** | ||
| * @param callback - The function to call for each item in the queue. | ||
| * The second parameter will be `thisArg` if provided, otherwise the queue itself. | ||
| * @param [thisArg=this] - The value of `this` to use when calling the `callback` function. | ||
@@ -90,0 +91,0 @@ */ |
117482
3.53%2979
0.27%322
44.39%