跳到主要内容

自定义缓存拦截器

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

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

约束与限制

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

使用自定义缓存拦截器

  1. 导入模块。

    import { rcp } from '@kit.RemoteCommunicationKit';
  2. 实现自定义缓存拦截器。

    class BlindCacheInterceptor implements rcp.Interceptor {
    private readonly cache: rcp.ResponseCache;
    constructor(cache: rcp.ResponseCache) {
    this.cache = cache;
    }
    async intercept(context: rcp.RequestContext, next: rcp.RequestHandler): Promise<rcp.Response> {
    const key: rcp.ResponseCacheKey = {
    url: context.request.url,
    method: context.request.method,
    };
    const responseInCache = await this.cache.get(key);
    if (responseInCache) {
    return rcp.createResponse(context.request, responseInCache.response, new Date());
    }
    const networkResponse = await next.handle(context);
    await this.cache.set(key, rcp.createCachedResponse(networkResponse));
    return networkResponse;
    }
    }
  3. 创建ResponseCache实例。其中,pathToFolder即HTTP缓存响应记录文件路径,”/path/dir”请根据实际情况替换为想要存储HTTP缓存的沙箱路径。

    const responseCache = new rcp.ResponseCache({
    persistent: {
    kind: 'file-system',
    pathToFolder: "/path/dir" // 请根据自身业务选择合适的路径
    }
    });
  4. 创建会话。在创建Session时,添加Interceptors参数。

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

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

    const responseB = await session.put('https://www.example.com');
    console.info(`Request succeeded, message is ${JSON.stringify(responseB)}`);
    cacheState = await responseCache.getState();
    console.info(`The current cache hit count is: ${cacheState.hitCount}`);