uniapp data cache/acquisition

uni.setStorage(OBJECT) asynchronous cache

uni.setStorage is to store our data in the key value specified in the local cache, which will overwrite the content corresponding to the original key value. It is an asynchronous interface

Insert picture description here

Example:

Here is the stored code

uni.setStorage({
    
    
    key: 'id',   // 存储值的名称
    data: 'hello',   //   将要存储的数据
    success:res => {
    
    
    	// 成功后的回调
        console.log(res);
    }
});

Here is the code taken

uni.getStorage({
    
    
	key:'id',   // 储存在本地的变量名
	success:res => {
    
    
		// 成功后的回调
		console.log(res.data);   // hello  这里可做赋值的操作
	}
})

Note: The storage here is asynchronous

uni.setStorageSync(KEY,DATA) Synchronous storage

Insert picture description here

Here is to save

try {
    
    
    uni.setStorageSync('storage_key', 'hello');
} catch (e) {
    
    
    // error
}

Here is to take

try {
    
    
    const value = uni.getStorageSync('storage_key');
    if (value) {
    
    
        console.log(value);
    }
} catch (e) {
    
    
    // error
}

uni.getStorageInfo(OBJECT)

Asynchronously obtain the relevant information of the current storage.
Insert picture description here
Example

uni.getStorageInfo({
    
    
    success: function (res) {
    
    
        console.log(res.keys);
        console.log(res.currentSize);
        console.log(res.limitSize);
    }
});

Note: This is asynchronous acquisition

uni.getStorageInfoSync()

Example

try {
    
    
    const res = uni.getStorageInfoSync();
    console.log(res.keys);
    console.log(res.currentSize);
    console.log(res.limitSize);
} catch (e) {
    
    
    // error
}

Note: Here is synchronous acquisition

uni.removeStorage(OBJECT)

Asynchronously remove the specified key from the local cache.
Insert picture description here
Example

uni.removeStorage({
    
    
    key: 'storage_key',
    success: function (res) {
    
    
        console.log('success');
    }
});

Note: Here is to remove the specified key asynchronously

uni.removeStorageSync(KEY)

Synchronously remove the specified key from the local cache

try {
    
    
    uni.removeStorageSync('storage_key');
} catch (e) {
    
    
    // error
}

Note: Here is to remove the specified key synchronously

uni.clearStorageSync()

Clean up local data cache

Example

try {
    
    
    uni.clearStorageSync();
} catch (e) {
    
    
    // error
}

uni.clearStorageSync()

Synchronously clean up the local data cache.

Example

try {
    
    
    uni.clearStorageSync();
} catch (e) {
    
    
    // error
}

Guess you like

Origin blog.csdn.net/weixin_52400118/article/details/109625903