DiskLruCache is a disk-based Lease Recently Used (LRU) cache library designed for OpenHarmony. It allows access to disk data using the LRU algorithm.
This project is developed based on the open source DiskLruCache library. It provides the following features:
ohpm install @ohos/disklrucache
Import DiskLruCache on the index.ets page of DevEco Studio.
import { DiskLruCache } from '@ohos/disklrucache'
Declare an object on the Build page.
testDiskLruCache: DiskLruCache = undefined
Initialize the object before using it. First, import Ability or Application and use GlobalContext.getContext.setObject("context", this.context) to register the context.
import Ability from '@ohos.application.Ability'
export default class MainAbility extends Ability {
onCreate(want, launchParam) {
GlobalContext.getContext.setObject("context", this.context)
}
}
Then, create an object on the page.
// Create a folder named diskLruCache with the application cache path and set the disk cache size to 3 MB. (This disk cache size is optional, with a default value of 300 MB and maximum value of 300 MB.)
this.testDiskLruCache = DiskLruCache.create(GlobalContext.getContext.getObject("context"), 3 * 1024 * 1024)
On the Build page, click the Add button to save an image file to the disk cache.
Cache a string synchronously.
let data: string = "Hello World Simple Example.";
this.testDiskLruCache.set('test', data);
Obtain a string from the disk cache synchronously.
let data:ArrayBuffer = this.testDiskLruCache.get('test');
console.log(String.fromCharCode.aplly(null, new Uint8Array(data)));
Cache a file synchronously.
import fs from '@ohos.file.fs';
let path = '/data/storage/el2/base/com.example.disklrucache/entry/files/testFile.txt';
let fd = fs.openSync(path, 0o2);
let length = fs.statSync(path).size;
let data = new ArrayBuffer(length);
fs.readSync(fd, data);
this.testDiskLruCache.set('testFile', data);
Obtain a file from the cache synchronously.
let data:ArrayBuffer = this.testDiskLruCache.get('testFile');
Cache a string asynchronously, and obtain a string from the cache asynchronously.
let value: string = "Hello World Simple Example.";
this.testDiskLruCache.setAsync('test', value).then(() => {
this.testDiskLruCache.getAsync('test').then((data) => {
console.log(String.fromCharCode.aplly(null, new Uint8Array(data)));
})
}).catch((err) => {
console.log('err =' + err);
})
Cache a file asynchronously, and obtain a file from the cache asynchronously.
import fs from '@ohos.file.fs';
let path = '/data/storage/el2/base/com.example.disklrucache/entry/files/testFile.txt';
let file = fs.openSync(path, 0o2);
let length = fs.statSync(path).size;
let value = new ArrayBuffer(length);
fs.readSync(file.fd, data);
this.testDiskLruCache.setAsync('test', value).then(() => {
this.testDiskLruCache.getAsync('test').then((data) => {
console.log(String.fromCharCode.aplly(null, new Uint8Array(data)));
})
}).catch((err) => {
console.log('err =' + err);
})
Perform other operations. For details, see the index.ets file.
API | Input Parameter | API Description |
---|---|---|
create(context, maxSize?: number): DiskLruCache | context, maxSize?: number | Creates an object and sets the path and size of the disk cache. |
setMaxSize(max: number) :void | max: number | Sets the maximum size of the disk cache. |
set(key: string, content: ArrayBuffer | string):void | key: string, content: ArrayBuffer | string | Stores data to the disk cache. |
setAsync(key: string, content: ArrayBuffer | string):Promise | key: string, content: ArrayBuffer | string | Stores data to the disk cache. This API uses a promise to return the result asynchronously. |
get(key: string): ArrayBuffer | key: string | Obtains data of the ArrayBuffer type from the disk cache. |
getAsync(key: string): Promise | key: string | Obtains data of the ArrayBuffer type from the disk cache. This API uses a promise to return the result asynchronously. |
getFileToPath(key: string): string | key: string | Obtains the path of a file or folder in the disk cache directory. |
getFileToPathAsync(key: string): Promise | key: string | Obtains the path of a file or folder in the disk cache directory. This API uses a promise to return the result asynchronously. |
getPath(): string | Obtains the path of the disk cache directory. | |
deleteCacheDataByKey(key: string): DiskCacheEntry | key: string | Deletes the data corresponding to the specified key from the disk-based LRU cache. |
cleanCacheData() | Clears all data from the disk-based LRU cache. |
DiskLruCache has been verified in the following versions:
/disklrucache/src/
- main/ets/components
- cache # Code related to the cache
- CustomMap.ets # Custom Map encapsulation
- DiskCacheEntry.ets # Disk cache entry
- DiskLruCache.ets # Disk-based LRU cache policy
- FileReader.ets # Code for reading and writing files
- FileUtils.ets # File utility class
/entry/src/
- main/ets
- pages # List of test pages
- index.ets # Testing disk cache page
If you find any problem when using DiskLruCache, submit an Issue or a PR to us.
This project is licensed under Apache License 2.0.
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。