文档管理中心
我的

自定义缓存拦截器

从6.0.0(20)开始,支持自定义缓存拦截器。

Remote Communication Kit模块提供了拦截器能力,支持开发者根据业务需求,实现自定义缓存拦截器。通过拦截器机制,开发者能够介入缓存处理流程,包括缓存数据的预处理、加载逻辑定制等,从而精准匹配复杂业务场景对缓存逻辑的差异化需求,提升系统的灵活性与可扩展性。

约束与限制

自定义缓存拦截器能力支持Phone、2in1、Tablet、Wearable、TV设备。

使用自定义缓存拦截器

  1. 导入模块。

    收起
    自动换行
    深色代码主题
    复制
    1. import { rcp } from '@kit.RemoteCommunicationKit';
  2. 实现自定义缓存拦截器。

    收起
    自动换行
    深色代码主题
    复制
    1. class BlindCacheInterceptor implements rcp.Interceptor {
    2. private readonly cache: rcp.ResponseCache;
    3. constructor(cache: rcp.ResponseCache) {
    4. this.cache = cache;
    5. }
    6. async intercept(context: rcp.RequestContext, next: rcp.RequestHandler): Promise<rcp.Response> {
    7. const key: rcp.ResponseCacheKey = {
    8. url: context.request.url,
    9. method: context.request.method,
    10. };
    11. const responseInCache = await this.cache.get(key);
    12. if (responseInCache) {
    13. return rcp.createResponse(context.request, responseInCache.response, new Date());
    14. }
    15. const networkResponse = await next.handle(context);
    16. await this.cache.set(key, rcp.createCachedResponse(networkResponse));
    17. return networkResponse;
    18. }
    19. }
  3. 创建ResponseCache实例。其中,pathToFolder即HTTP缓存响应记录文件路径,'/path/dir'请根据实际情况替换为想要存储HTTP缓存的沙箱路径。

    收起
    自动换行
    深色代码主题
    复制
    1. const responseCache = new rcp.ResponseCache({
    2. persistent: {
    3. kind: 'file-system',
    4. pathToFolder: '/data/storage/el2/base/entry/temp/CustomCache' // 请根据自身业务选择合适的路径
    5. }
    6. });
  4. 创建会话。在创建Session时,添加Interceptors参数。

    收起
    自动换行
    深色代码主题
    复制
    1. const session: rcp.Session = rcp.createSession({
    2. interceptors : [new BlindCacheInterceptor(responseCache)]
    3. });
  5. 发起第一次请求。'https://www.example.com'请根据实际情况替换为支持HTTP缓存协议的URL。本次请求将会从网络服务器获取数据,此时可查看缓存状态信息,此时缓存条数应当为1。

    收起
    自动换行
    深色代码主题
    复制
    1. // 请求的网址是示例网址,请根据实际需求更改
    2. const responseA = await session.put('https://www.example.com');
    3. console.info(`Request succeeded, message is ${JSON.stringify(responseA)}`);
    4. let cacheState = await responseCache.getState();
    5. console.info(`The current number of cache entries is: ${cacheState.count}`);
  6. 发起第二次请求。'https://www.example.com'请根据实际情况替换为支持HTTP缓存协议的URL。本次请求将会按照自定义缓存拦截器逻辑从缓存中获取响应,此时可查看缓存状态信息,此时缓存命中数应当为1。

    收起
    自动换行
    深色代码主题
    复制
    1. // 请求的网址是示例网址,请根据实际需求更改
    2. const responseB = await session.put('https://www.example.com');
    3. console.info(`Request succeeded, message is ${JSON.stringify(responseB)}`);
    4. cacheState = await responseCache.getState();
    5. console.info(`The current cache hit count is: ${cacheState.hitCount}`);