Skip to content

Commit

Permalink
feat: update mmkv
Browse files Browse the repository at this point in the history
  • Loading branch information
richerfu committed Apr 9, 2024
1 parent dea3d2a commit 75d0932
Showing 1 changed file with 81 additions and 6 deletions.
87 changes: 81 additions & 6 deletions src/ecosystem/third/mmkv.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,93 @@ MMKV is an efficient, small, easy-to-use mobile key-value storage framework used

In fact, mmkv can fully run on OpenHarmony devices. We provide a simple native binding for MMKV.

::: danger Tip
This is an early preview version, providing only a small number of APIs.

You can also submit [PR](https://github.com/ohos-rs/mmkv/pulls) or [issue](https://github.com/ohos-rs/mmkv/issues) to submit more features.
:::

## Install

```shell
ohpm install @ohos-rs/mmkv
```

## API

```ts
export interface InitOption {
/** mmkv instance's log level */
logLevel?: MMKVLogLevel
/** mmkv instance mode */
mode?: MMKVMode
/** mmkv instance id if is empty, will use default. */
mmapId?: string
}

/** MMKV log level */
export const enum MMKVLogLevel {
Debug = 0,
Info = 1,
Warning = 2,
Error = 3,
None = 4
}

export const enum MMKVMode {
SingleProcess = 1,
MultiProcess = 2
}

export class MMKV {
constructor(rootDir: string, options?: InitOption | undefined | null)
enableAutoKeyExpire(expire: number): void
/** set bool to mmkv */
encodeBool(key: string, value: boolean, expire?: number | undefined | null): void
/** get bool from mmkv */
decodeBool(key: string): boolean
/** get string */
decodeString(key: string): string
/** set string */
encodeString(key: string, value: string, expire?: number | undefined | null): void
/** set number include int float etc.s */
encodeNumber(key: string, value: number, expire?: number | undefined | null): void
/** get number */
decodeNumber(key: string): number
/** set bigint which will store as `Vec<string>`, and the first element is a flag, 1 for negative numbers. */
encodeBigint(key: string, value: bigint, expire?: number | undefined | null): void
/** get bigint */
decodeBigint(key: string): bigint
/** remove key or keys */
removeValueForKey(key: string | Array<string>): void
/** check key if existed */
containsKey(key: string): boolean
/** get current mmkv instance's all keys */
allKeys(): Array<string>
/** get current mmkv instance's mmap id */
getMmapId(): string
/**
* get current mmkv instance's storage size
* @default TOTAL
*/
getStorageSize(sizeType?: 'ACTUAL' | 'TOTAL'): number
/**
* get kv's count
* @default false
*/
count(filterExpire?: boolean | undefined | null): number
/**
* get key's size
* @default false
*/
getValueSize(key: string, actual?: boolean | undefined | null): number
/** clear all kv with current mmkv */
clearAll(keepSpace?: boolean | undefined | null): void
/** clear memory cache with current mmkv */
clearMemoryCache(keepSpace?: boolean | undefined | null): void
/** basic method to back up data */
static backUpToDirectory(dir: string, mmapId?: string | undefined | null): void
/** basic method to restore data */
static restoreFromDirectory(dir: string, mmapId?: string | undefined | null): void
/** remove instance */
static removeStorage(mmapId: string): void
}
```

## Usage

```ts
Expand Down

0 comments on commit 75d0932

Please sign in to comment.