1# @system.storage (数据存储) 2<!--Kit: ArkData--> 3<!--Subsystem: DistributedDataManager--> 4<!--Owner: @yanhuii--> 5<!--Designer: @houpengtao1--> 6<!--Tester: @yippo; @logic42--> 7<!--Adviser: @ge-yafang--> 8 9> **说明:** 10> 11> - 从API Version 6开始,该模块不再维护,可以使用模块[`@ohos.data.storage`](js-apis-data-storage.md)。在API Version 9后,推荐使用新模块[`@ohos.data.preferences`](js-apis-data-preferences.md)。 12> 13> - 本模块首批接口从API version 3开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 14> 15> - 本模块接口仅可在FA模型下使用。 16 17## 导入模块 18 19```js 20import storage from '@system.storage'; 21``` 22 23## storage.get 24 25get(options: GetStorageOptions): void 26 27通过索引读取缓存中存储的值。 28 29**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core.Lite 30 31**参数:** 32 33| 参数名 | 类型 | 必填 | 说明 | 34| ------- | -------------------- | ---- | ---------- | 35| options | [GetStorageOptions](#getstorageoptions) | 是 | 接口配置信息。 | 36 37**示例:** 38 39```js 40export default { 41 storageGet() { 42 storage.get({ 43 key: 'storage_key', 44 success: function(data) { 45 console.log('call storage.get success: ' + data); 46 }, 47 fail: function(data, code) { 48 console.log('call storage.get fail, code: ' + code + ', data: ' + data); 49 }, 50 complete: function() { 51 console.log('call complete'); 52 }, 53 }); 54 } 55} 56``` 57 58## storage.set 59 60set(options: SetStorageOptions): void 61 62修改缓存中索引对应的值。 63 64**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core.Lite 65 66**参数:** 67 68| 参数名 | 类型 | 必填 | 说明 | 69| ------- | ------------------- | ---- | ---------- | 70| options | [SetStorageOptions](#setstorageoptions) | 是 | 接口配置信息。 | 71 72**示例:** 73 74```js 75export default { 76 storageSet() { 77 storage.set({ 78 key: 'storage_key', 79 value: 'storage value', 80 success: function() { 81 console.log('call storage.set success.'); 82 }, 83 fail: function(data, code) { 84 console.log('call storage.set fail, code: ' + code + ', data: ' + data); 85 }, 86 }); 87 } 88} 89``` 90 91## storage.clear 92 93clear(options?: ClearStorageOptions): void 94 95清空缓存中存储的键值对。 96 97**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core.Lite 98 99**参数:** 100 101| 参数名 | 类型 | 必填 | 说明 | 102| ------- | ------------------------------------------- | ---- | -------------- | 103| options | [ClearStorageOptions](#clearstorageoptions) | 否 | 接口配置信息。 | 104 105**示例:** 106 107```js 108export default { 109 storageClear() { 110 storage.clear({ 111 success: function() { 112 console.log('call storage.clear success.'); 113 }, 114 fail: function(data, code) { 115 console.log('call storage.clear fail, code: ' + code + ', data: ' + data); 116 }, 117 }); 118 } 119} 120``` 121 122## storage.delete 123 124delete(options: DeleteStorageOptions): void 125 126删除缓存中索引对应的键值对。 127 128**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core.Lite 129 130**参数:** 131 132| 参数名 | 类型 | 必填 | 说明 | 133| ------- | --------------------------------------------- | ---- | -------------- | 134| options | [DeleteStorageOptions](#deletestorageoptions) | 是 | 接口配置信息。 | 135 136**示例:** 137 138```js 139export default { 140 storageDelete() { 141 storage.delete({ 142 key: 'Storage1', 143 success: function() { 144 console.log('call storage.delete success.'); 145 }, 146 fail: function(data, code) { 147 console.log('call storage.delete fail, code: ' + code + ', data: ' + data); 148 }, 149 }); 150 } 151} 152``` 153 154## GetStorageOptions 155 156**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core.Lite 157 158| 名称 | 类型 | 必填 | 说明 | 159| -------- | ---------------- | ---- | ------------------- | 160| key | string | 是 | 内容索引。 | 161| default | string | 否 | key不存在则返回的默认值。 | 162| success | (data: any) => void | 否 | 接口调用成功的回调函数,data为返回key对应的value。 | 163| fail | (data: string, code: number) => void | 否 | 接口调用失败的回调函数,data为错误信息,code为错误码。 | 164| complete | () => void | 否 | 接口调用结束的回调函数。 | 165 166 167## SetStorageOptions 168 169**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core.Lite 170 171| 名称 | 类型 | 必填 | 说明 | 172| -------- | ------------------- | ---- | -------------------- | 173| key | string | 是 | 要修改的存储值的索引。 | 174| value | string | 是 | 新值。长度需小于128字节。 | 175| success | () => void | 否 | 接口调用成功的回调函数。 | 176| fail | (data: string, code: number) => void | 否 | 接口调用失败的回调函数,data为错误信息,code为错误码。 | 177| complete | () => void | 否 | 接口调用结束的回调函数。 | 178 179 180## ClearStorageOptions 181 182**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core.Lite 183 184| 名称 | 类型 | 必填 | 说明 | 185| -------- | --------------------- | ---- | -------------------- | 186| success | () => void | 否 | 接口调用成功的回调函数。 | 187| fail | (data: string, code: number) => void | 否 | 接口调用失败的回调函数,data为错误信息,code为错误码。 | 188| complete | () => void | 否 | 接口调用结束的回调函数。 | 189 190 191## DeleteStorageOptions 192 193**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core.Lite 194 195| 名称 | 类型 | 必填 | 说明 | 196| -------- | -------------------- | ---- | ------------------ | 197| key | string | 是 | 内容索引。 | 198| success | () => void | 否 | 接口调用成功的回调函数。 | 199| fail | (data: string, code: number) => void | 否 | 接口调用失败的回调函数,data为错误信息,code为错误码。 | 200| complete | () => void | 否 | 接口调用结束的回调函数。 |