Cache
cache.js A simple LRU cache implementation.
Performance Notes: Since LRU queue is implemented by JavaScript Array internally, it's enough for small cache limits. Large cache limits may require linklist implementation.
Author: harttleyangjun14@baidu.com
- Cache
- .create(name, options) ⇒
Namespace - .using(name) ⇒
Object - .set(name, key, value) ⇒
any - .get(name, key) ⇒
any - .rename(name, before, after) ⇒
any - .remove(name, key) ⇒
any - .clear(name) ⇒
undefined|Namespace - .contains(name, key) ⇒
boolean
- .create(name, options) ⇒
Cache.create(name, options) ⇒ Namespace
Create a namespaced cache instance
Kind: static method of Cache
Returns: Namespace - the namespace object created
| Param | Type | Description |
|---|---|---|
| name | string |
The namespace identifier |
| options | Object |
The options object used to create the namespace |
Cache.using(name) ⇒ Object
Using a specific namespace
Kind: static method of Cache
Returns: Object - The scoped cache object
| Param | Type | Description |
|---|---|---|
| name | string |
The namespace identifier |
Cache.set(name, key, value) ⇒ any
Set a cache item
Kind: static method of Cache
Returns: any - The return value of corresponding Namespace#set
| Param | Type | Description |
|---|---|---|
| name | string |
The namespace for your cache item |
| key | string |
The key for your cache item |
| value | any |
The value for your cache item |
Cache.get(name, key) ⇒ any
Get a cache item
Kind: static method of Cache
Returns: any - The value for your cache item, or undefined if the specified item does not exist.
| Param | Type | Description |
|---|---|---|
| name | string |
The namespace for your cache item |
| key | string |
The key for your cache item |
Cache.rename(name, before, after) ⇒ any
Rename a cache item
Kind: static method of Cache
Returns: any - The return value of corresponding Namespace#rename
| Param | Type | Description |
|---|---|---|
| name | string |
The namespace for your cache item |
| before | string |
The source key for your cache item |
| after | string |
The destination key for your cache item |
Cache.remove(name, key) ⇒ any
Remove a specific key in namespace name
Kind: static method of Cache
Returns: any - The return value of corresponding Namespace#remove
| Param | Type | Description |
|---|---|---|
| name | string |
The namespace identifier |
| key | string |
The key to remove |
Cache.clear(name) ⇒ undefined | Namespace
Clear the given namespace, or all namespaces if name not set.
Kind: static method of Cache
Returns: undefined | Namespace - Return the Namespace object if name is given, undefined otherwise.
| Param | Type | Description |
|---|---|---|
| name | string |
The namespace to clear. |
Cache.contains(name, key) ⇒ boolean
Check whether the given key exists within the namespace, or whether the namespace exists if key not set.
Kind: static method of Cache
Returns: boolean - Return if the namespace specified by name contains key
| Param | Type | Description |
|---|---|---|
| name | string |
The namespace to check |
| key | string |
The key to check with |