目录

Performance Optimization Practices: Cache Plugins

Overview

For large-scale app development and multi-team collaboration scenarios, build efficiency is a key factor affecting the development experience.

To address these issues, Hvigor provides the cache-enhancement plugin @ohos/hvigor-cache-booster. It adopts a multi-level cache lookup mechanism, searching for build caches in the following order: build directory of the current module -> local cache -> remote cache. This significantly improves the efficiency of the first build.

Constraints

Installing the Plugin

  1. The plugin @ohos/hvigor-cache-booster is available on the npm central registry. Configure the registry address in the .npmrc file.
    @ohos:registry=https://repo.harmonyos.com/npm/
  2. Configure the plugin dependency in the hvigor-config.json5 file of the project.
    "dependencies": {
        "@ohos/hvigor-cache-booster": "7.0.0"  
      },
  3. Run Sync or Build. DevEco Studio will automatically install the dependency.

Example

Add the following code in the module-level hvigorfile.ts file, and adjust configurations as needed to enable the cache enhancement plugin. For complete usage instructions, see README.md of @ohos/hvigor-cache-booster.

If you want to enable remote cache, the server must implement corresponding APIs following specifications and provide services. For server-side API specifications, see the README.md file of the plugin.

// entry/hvigorfile.ts
import { hapTasks } from '@ohos/hvigor-ohos-plugin';
import { CacheBooster } from '@ohos/hvigor-cache-booster';
import { arkCacheChainFactory, ArkCacheChainConfig, ChainType } from '@ohos/hvigor-cache-booster';

// Cache plugin configuration for local and remote cache.
const arkChainCfg: ArkCacheChainConfig = {
  local: { enable: true, pushEnable: true, minPushIntervalHours: 0 },
  remote: { enable: true, pushEnable: true }
};

// Cache chain type, which can be DEFAULT (indicating the source code directory) or OHOS_TEST (indicating the test directory).
const chain0 = arkCacheChainFactory.createArkCacheChain(ChainType.DEFAULT, arkChainCfg);
const chain1 = arkCacheChainFactory.createArkCacheChain(ChainType.OHOS_TEST, arkChainCfg);

export default {
  system: hapTasks,
  plugins: [new CacheBooster({
    enable: true,
    remoteUrl: 'https://...',   // Remote cache URL, which must be configured based on your actual environment when remote cache is enabled.
    strictSSL: false,
    cacheManagerChains: [chain0, chain1]
  })]
}