has.setStorageSync(string key, string | object | number | boolean | null | array data)
Saves data in the local cache based on the specified key. This API returns the result synchronously. The original content corresponding to the key will be overwritten.
The data remains available until the user deletes it.
The maximum size of the data corresponding to a single key is 1 MB, and the maximum size of all data is 10 MB.
Since: 1.0.0
Parameters
Name | Type | Mandatory | Description |
|---|---|---|---|
key | string | Yes | Key of the data to save. |
data | string | object | number | boolean | null | array | Yes | Data to save. Only native-type objects and objects that can be serialized using JSON.stringify are supported. |
Example
has.setStorageSync('key', 'value'); has.setStorage(Object object)
Saves data in the local cache based on the specified key. This API uses an asynchronous callback to return the result. The original content corresponding to the key will be overwritten.
The data remains available until the user deletes it.
The maximum size of the data corresponding to a single key is 1 MB, and the maximum size of all data is 10 MB.
Since: 1.0.0
Parameters
The input parameter is an object containing the following fields.
Name | Type | Mandatory | Description |
|---|---|---|---|
key | string | Yes | Key of the data to save. |
data | string | object | number | boolean | null | array | Yes | Data to save. Only native-type objects and objects that can be serialized using JSON.stringify are supported. |
success | function | No | Callback to be invoked when the API call is successful. |
fail | function | No | Callback to be invoked when the API call fails. |
complete | function | No | Callback to be invoked when the API call is complete, regardless of success or failure. |
Example
has.setStorage({
key: 'key',
data: 'test',
success: res => {
console.info('setStorage success');
},
fail: res => {
console.info('setStorage failed');
}
}); has.removeStorageSync(string key)
Removes data of the specified key. This API returns the result synchronously.
Since: 1.0.0
Parameters
Name | Type | Mandatory | Description |
|---|---|---|---|
key | string | Yes | Key of the data to remove. |
Example
has.removeStorageSync('key'); has.removeStorage(Object object)
Removes data of the specified key. This API uses an asynchronous callback to return the result.
Since: 1.0.0
Parameters
The input parameter is an object containing the following fields.
Name | Type | Mandatory | Description |
|---|---|---|---|
key | string | Yes | Key of the data to remove. |
success | function | No | Callback to be invoked when the API call is successful. |
fail | function | No | Callback to be invoked when the API call fails. |
complete | function | No | Callback to be invoked when the API call is complete, regardless of success or failure. |
Example
has.removeStorage({
key: 'key',
success: res => {
console.info('setStorage success');
},
fail: res => {
console.info('setStorage failed');
}
}); has.getStorageSync(string key): Object
Obtains the value of the specified key. This API returns the result synchronously.
Since: 1.0.0
Parameters
Name | Type | Mandatory | Description |
|---|---|---|---|
key | string | Yes | Key of the value to obtain. |
Return value
An object containing the following fields.
Name | Type | Description |
|---|---|---|
data | any | Value obtained. |
Example
has.getStorageSync('key'); has.getStorageInfoSync(): Object
Obtains information about the data in the local cache. This API returns the result synchronously.
Since: 1.0.0
Return value
An object containing the following fields.
Name | Type | Description |
|---|---|---|
keys | Array<string> | All the keys in the local cache. |
currentSize | number | Size of the data in the local cache, in KB. |
limitSize | number | Maximum data size allowed, in KB. |
Example
const storageInfo = has.getStorageInfoSync(); console.log(storageInfo.keys); console.log(storageInfo.currentSize); console.log(storageInfo.limitSize);
has.getStorageInfo(Object object)
Obtains information about the data in the local cache. This API uses an asynchronous callback to return the result.
Since: 1.0.0
Parameters
The input parameter is an object containing the following fields.
Name | Type | Mandatory | Description |
|---|---|---|---|
success | function | No | Callback to be invoked when the API call is successful. |
fail | function | No | Callback to be invoked when the API call fails. |
complete | function | No | Callback to be invoked when the API call is complete, regardless of success or failure. |
Return value of success
Name | Type | Description |
|---|---|---|
keys | Array<string> | All the keys in the local cache. |
currentSize | number | Size of the data in the local cache, in KB. |
limitSize | number | Maximum data size allowed, in KB. |
Example
has.getStorageInfo({
success: function (storageInfo) {
console.log(storageInfo);
},
fail: function () {
console.log('get storage info fail');
},
complete: function (storageInfo) {
console.log(storageInfo);
}
}); has.getStorage(Object object)
Obtains the value of the specified key. This API uses an asynchronous callback to return the result.
Since: 1.0.0
Parameters
The input parameter is an object containing the following fields.
Name | Type | Mandatory | Description |
|---|---|---|---|
key | string | Yes | Key of the value to obtain. |
success | function | No | Callback to be invoked when the API call is successful. |
fail | function | No | Callback to be invoked when the API call fails. |
complete | function | No | Callback to be invoked when the API call is complete, regardless of success or failure. |
Return value of success
Name | Type | Description |
|---|---|---|
data | any | Value obtained. |
Example
has.getStorage({
key: 'key',
success: function (ret) {
console.log(ret);
},
fail: function () {
console.log('set storage fail');
},
complete: function (ret) {
console.log(ret);
}
}); has.clearStorage(Object object)
Clears the local cache. This API uses an asynchronous callback to return the result.
Since: 1.0.0
Parameters
The input parameter is an object containing the following fields.
Name | Type | Mandatory | Description |
|---|---|---|---|
success | function | No | Callback to be invoked when the API call is successful. |
fail | function | No | Callback to be invoked when the API call fails. |
complete | function | No | Callback to be invoked when the API call is complete, regardless of success or failure. |
Example
has.clearStorage({
success: function () {
console.log('clear storage success');
},
fail: function () {
console.log('clear storage fail');
},
complete: function () {
console.log('clear storage complete');
}
}); has.clearStorageSync()
Clears the local cache. This API returns the result synchronously.
Since: 1.0.0
Example
has.clearStorageSync();
has.batchSetStorageSync(Array<KVPair> kvList): void
Stores a batch of data in the local cache based on the specified key.
The original content corresponding to the key is overwritten. The data remains available until the user deletes it or the system clears it due to insufficient storage space.
The maximum size of the data corresponding to a single key is 1 MB, and the maximum size of all data is 10 MB.
Since: 1.0.12
Parameters
Name | Type | Default Value | Mandatory | Description |
|---|---|---|---|---|
kvList | Array<KVPair> | - | Yes | List of data to be stored in batches in the local cache. |
KVPair
Name | Type | Default Value | Mandatory | Description |
|---|---|---|---|---|
key | string | - | Yes | Specified key in the local cache. |
value | any | - | Yes | Data to store. Only native-type objects, date-type objects, and objects that can be serialized using JSON.stringify are supported. |
Example
try {
const kvList = [{ key: 'key1', value: 'value1' },{ key: 'key2', value: 'value2' }];
has.batchSetStorageSync(kvList);
} catch (e) {
}; has.batchSetStorage(Object object): void
Stores a batch of data in the local cache based on the specified key. This API uses an asynchronous callback to return the result.
The original content corresponding to the key is overwritten. The data remains available until the user deletes it or the system clears it due to insufficient storage space.
The maximum size of the data corresponding to a single key is 1 MB, and the maximum size of all data is 10 MB.
Since: 1.0.12
Parameters
The input parameter is an object containing the following fields.
Name | Type | Default Value | Mandatory | Description |
|---|---|---|---|---|
kvList | Array<KVPair> | - | Yes | KVPair contains the following parameters: key: key specified in the local cache. value: content to be stored. Only native-type objects, date-type objects, and objects that can be serialized using JSON.stringify are supported. |
success | function | - | No | Callback to be invoked when the API call is successful. |
fail | function | - | No | Callback to be invoked when the API call fails. |
complete | function | - | No | Callback to be invoked when the API call is complete, regardless of success or failure. |
Example
const kvList = [{ key: 'key1', value: 'value1' }, { key: 'key2', value: 'value2' }];
has.batchSetStorage({
kvList: kvList,
success: (res) => {
console.log('batchSetStorage success:', res);
},
fail: (err) => {
console.log('batchSetStorage fail:', err);
},
complete: (res) => {
console.log('batchSetStorage complete:', res);
}
}); has.batchGetStorageSync(Array<string> keyList): Array<any>
Obtains a batch of data in a specified key from the local cache. This API returns the result synchronously.
Since: 1.0.12
Parameters
Name | Type | Default Value | Mandatory | Description |
|---|---|---|---|---|
keyList | Array<string> | - | Yes | Key array in the local cache. |
Return value
Data array obtained, whose type is Array<any>.
Example
try {
var valueList = has.batchGetStorageSync(['key1','key2']);
if (valueList) {
// Array of the data corresponding to the key: ['value1', 'value2']
}
} catch (e) {
}; has.batchGetStorage(Object object): void
Obtains a batch of data in a specified key from the local cache. This API uses an asynchronous callback to return the result.
Since: 1.0.12
Parameters
The input parameter is an object containing the following fields.
Name | Type | Default Value | Mandatory | Description |
|---|---|---|---|---|
keyList | Array<string> | - | Yes | Key array in the local cache. |
success | function | - | No | Callback to be invoked when the API call is successful. |
fail | function | - | No | Callback to be invoked when the API call fails. |
complete | function | - | No | Callback to be invoked when the API call is complete, regardless of success or failure. |
Return value of success
Name | Type | Description |
|---|---|---|
errMsg | string | Error message. |
dataList | Array<any> | Data array obtained. |
Example
has.batchGetStorage({
keyList: ['key1', 'key2'],
success: (res) => {
// res.dataList: ['value1', 'value2']
console.log('batchGetStorage success:', res.dataList);
},
fail: (err) => {
console.log('batchGetStorage fail:', err);
},
complete: (res) => {
console.log('batchGetStorage complete:', res);
}
});