Object built-in object

ObjectLruCache

LruCache (Least Recently Used Cache) is a class used to maintain the LRU cache. It can store a certain number of values ​​and maintain the size of the container. When new data comes in, if the container has not reached the size limit, the new data can be added directly to the container. If the container is full, the container will evict the least recently used data

We can use it in the following ways:

1 2
const util = require('util') const c = new util.LruCache(10, 100) // create a LruCache instance with size 10 and timeout 100ms

Among them, set() is the interface for setting key-value pairs:

1
LruCache.set(String name, Value value);

The name parameter specifies the key value to be set, and the value parameter specifies the value to be set.

LruCacheThe getmethod can update cached data using a callback function:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
var c = new util.LruCache(10, 1000); // create a LruCache instance with size 10 and timeout 1000ms function get_data(name) { // return data from backend // ... var data = { 'name': name, 'value': Math.random() }; console.log('update data: ' + JSON.stringify(data)); // output infomation to console return data; } console.log(c.get('a', get_data)); console.log(c.get('a', get_data));

Results of the:

1 2 3
update data: {"name":"a","value":0.4019124971556616} {"name":"a","value":0.4019124971556616} // updater will be called to update cache data when cache is empty {"name":"a","value":0.4019124971556616} // updater will not be called when cache is not empty

When using LruCache specifically, developers are recommended to follow the following best practices:

  • You can use LruCache to optimize application performance by using a specific time point as the fresh point during back-end data processing and setting the expiration time.
  • Analyze business decisions under corresponding scenarios. For example, more frequent data updates need to be configured with a shorter expiration time, while cached data that is updated less frequently does not need to set a too short expiration time.
  • In different usage scenarios, consider different data access modes and use appropriate LruCache class instances.

inheritance relationship

Constructor

LruCache

LruCache object constructor

1 2
new LruCache(Integer size, Integer timeout = 0);

Call parameters:

  • size: Integer, maximum cache size
  • timeout: Integer, element expiration time, unit is ms, if it is less than or equal to 0, it will not expire. The default is 0.

static properties

defaultMaxListeners

Integer, the default global maximum number of listeners

1
static Integer LruCache.defaultMaxListeners;

member properties

size

Integer, query the number of values ​​in the container

1
readonly Integer LruCache.size;

timeout

Integer, query and set the expiration time of elements in the container, the unit is ms, if it is less than or equal to 0, it will not expire.

1
Integer LruCache.timeout;

onexpire

Function, query and bind data timeout events, equivalent to on("expire", func);

1
Function LruCache.onexpire;

member function

clear

Clear container data

1
LruCache.clear();

has

Check whether the data with the specified key value exists in the container

1
Boolean LruCache.has(String name);

Call parameters:

  • name: String, specifies the key value to be checked

Return results:

  • Boolean, returns whether the key value exists

get

Query the value of the specified key value

1
Value LruCache.get(String name);

Call parameters:

  • name: String, specify the key value to be queried

Return results:

  • Value, returns the value corresponding to the key value, if it does not exist, returns undefined

Query the value of the specified key value. If it does not exist or expires, call the callback function to update the data.

1 2
Value LruCache.get(String name, Function updater);

Call parameters:

  • name: String, specify the key value to be queried
  • updater: Function, specify the update function

Return results:

  • Value, returns the value corresponding to the key value

set

Set a key value data. If the key value does not exist, insert a new piece of data.

1 2
LruCache.set(String name, Value value);

Call parameters:

  • name: String, specify the key value to be set
  • value: Value, specify the data to be set

Set a key value data. If the key value does not exist, insert new data.

1
LruCache.set(Object map);

Call parameters:

  • map: Object, specifies the key-value data dictionary to be set

remove

Delete all values ​​of the specified key

1
LruCache.remove(String name);

Call parameters:

  • name: String, specifies the key value to be deleted

isEmpty

Check if container is empty

1
Boolean LruCache.isEmpty();

Return results:

  • Boolean, returns true if there is no value in the container

on

Bind an event handler to the object

1 2
Object LruCache.on(String ev, Function func);

Call parameters:

  • ev: String, specifies the name of the event
  • func: Function, specify the event processing function

Return results:

  • Object, returns the event object itself to facilitate chain calls

Bind an event handler to the object

1
Object LruCache.on(Object map);

Call parameters:

  • map: Object, specifies the event mapping relationship, the object attribute name will be used as the event name, and the value of the attribute will be used as the event processing function

Return results:

  • Object, returns the event object itself to facilitate chain calls

addListener

Bind an event handler to the object

1 2
Object LruCache.addListener(String ev, Function func);

Call parameters:

  • ev: String, specifies the name of the event
  • func: Function, specify the event processing function

Return results:

  • Object, returns the event object itself to facilitate chain calls

Bind an event handler to the object

1
Object LruCache.addListener(Object map);

Call parameters:

  • map: Object, specifies the event mapping relationship, the object attribute name will be used as the event name, and the value of the attribute will be used as the event processing function

Return results:

  • Object, returns the event object itself to facilitate chain calls

prependListener

Bind an event handler to the object's origin

1 2
Object LruCache.prependListener(String ev, Function func);

Call parameters:

  • ev: String, specifies the name of the event
  • func: Function, specify the event processing function

Return results:

  • Object, returns the event object itself to facilitate chain calls

Bind an event handler to the object's origin

1
Object LruCache.prependListener(Object map);

Call parameters:

  • map: Object, specifies the event mapping relationship, the object attribute name will be used as the event name, and the value of the attribute will be used as the event processing function

Return results:

  • Object, returns the event object itself to facilitate chain calls

once

Bind a one-time event handler to the object. The one-time handler will only be triggered once.

1 2
Object LruCache.once(String ev, Function func);

Call parameters:

  • ev: String, specifies the name of the event
  • func: Function, specify the event processing function

Return results:

  • Object, returns the event object itself to facilitate chain calls

Bind a one-time event handler to the object. The one-time handler will only be triggered once.

1
Object LruCache.once(Object map);

Call parameters:

  • map: Object, specifies the event mapping relationship, the object attribute name will be used as the event name, and the value of the attribute will be used as the event processing function

Return results:

  • Object, returns the event object itself to facilitate chain calls

prependOnceListener

Bind an event handler to the object's origin

1 2
Object LruCache.prependOnceListener(String ev, Function func);

Call parameters:

  • ev: String, specifies the name of the event
  • func: Function, specify the event processing function

Return results:

  • Object, returns the event object itself to facilitate chain calls

Bind an event handler to the object's origin

1
Object LruCache.prependOnceListener(Object map);

Call parameters:

  • map: Object, specifies the event mapping relationship, the object attribute name will be used as the event name, and the value of the attribute will be used as the event processing function

Return results:

  • Object, returns the event object itself to facilitate chain calls

off

Unassign a function from the object processing queue

1 2
Object LruCache.off(String ev, Function func);

Call parameters:

  • ev: String, specifies the name of the event
  • func: Function, specify the event processing function

Return results:

  • Object, returns the event object itself to facilitate chain calls

Cancel all functions in the object processing queue

1
Object LruCache.off(String ev);

Call parameters:

  • ev: String, specifies the name of the event

Return results:

  • Object, returns the event object itself to facilitate chain calls

Unassign a function from the object processing queue

1
Object LruCache.off(Object map);

Call parameters:

  • map: Object, specifies the event mapping relationship, the object attribute name is used as the event name, and the value of the attribute is used as the event processing function

Return results:

  • Object, returns the event object itself to facilitate chain calls

removeListener

Unassign a function from the object processing queue

1 2
Object LruCache.removeListener(String ev, Function func);

Call parameters:

  • ev: String, specifies the name of the event
  • func: Function, specify the event processing function

Return results:

  • Object, returns the event object itself to facilitate chain calls

Cancel all functions in the object processing queue

1
Object LruCache.removeListener(String ev);

Call parameters:

  • ev: String, specifies the name of the event

Return results:

  • Object, returns the event object itself to facilitate chain calls

Unassign a function from the object processing queue

1
Object LruCache.removeListener(Object map);

Call parameters:

  • map: Object, specifies the event mapping relationship, the object attribute name is used as the event name, and the value of the attribute is used as the event processing function

Return results:

  • Object, returns the event object itself to facilitate chain calls

removeAllListeners

Cancels all listeners for all events from the object's processing queue. If an event is specified, removes all listeners for the specified event.

1
Object LruCache.removeAllListeners(String ev);

Call parameters:

  • ev: String, specifies the name of the event

Return results:

  • Object, returns the event object itself to facilitate chain calls

Cancels all listeners for all events from the object's processing queue. If an event is specified, removes all listeners for the specified event.

1
Object LruCache.removeAllListeners(Array evs = []);

Call parameters:

  • evs: Array, specify the name of the event

Return results:

  • Object, returns the event object itself to facilitate chain calls

setMaxListeners

The default limit on the number of listeners, for compatibility only

1
LruCache.setMaxListeners(Integer n);

Call parameters:

  • n: Integer, specify the number of events

getMaxListeners

Gets the default limit number of listeners, for compatibility only

1
Integer LruCache.getMaxListeners();

Return results:

  • Integer, returns the default limit quantity

listeners

Query the listener array for the specified event of the object

1
Array LruCache.listeners(String ev);

Call parameters:

  • ev: String, specifies the name of the event

Return results:

  • Array, returns the listener array for the specified event

listenerCount

Query the number of listeners for the specified event of the object

1
Integer LruCache.listenerCount(String ev);

Call parameters:

  • ev: String, specifies the name of the event

Return results:

  • Integer, returns the number of listeners for the specified event

Query the number of listeners for the specified event of the object

1 2
Integer LruCache.listenerCount(Value o, String ev);

Call parameters:

  • o: Value, specifies the object of the query
  • ev: String, specifies the name of the event

Return results:

  • Integer, returns the number of listeners for the specified event

eventNames

Query listener event name

1
Array LruCache.eventNames();

Return results:

  • Array, returns an array of event names

emit

Actively trigger an event

1 2
Boolean LruCache.emit(String ev, ...args);

Call parameters:

  • ev: String, event name
  • args: ..., event parameters will be passed to the event processing function

Return results:

  • Boolean, returns the event trigger status, returns true if there is a response event, otherwise returns false

toString

Returns the string representation of the object. Generally, "[Native Object]" is returned. The object can be re-implemented according to its own characteristics.

1
String LruCache.toString();

Return results:

  • String, returns the string representation of the object

toJSON

Returns a JSON format representation of the object, generally returning a collection of readable properties defined by the object.

1
Value LruCache.toJSON(String key = "");

Call parameters:

  • key: String, not used

Return results:

  • Value, returns a value containing JSON serializable