目录

Cache Sharing Between Sessions

The function of cache sharing between sessions is supported since version 6.0.0(20).

Data is isolated across sessions. When a service scenario requires cross-session sharing of cached data, you can leverage either of the following standard approaches to achieve this:

Constraints

The function of cache sharing between sessions supports phones, 2-in-1 devices, tablets, wearables, and TVs.

Referencing the Same HTTP Response Cache Instance

You can create a ResponseCache instance and configure it for different sessions to share cached data between sessions.

  1. Import the required modules.

    import { rcp } from '@kit.RemoteCommunicationKit';
  2. Create a ResponseCache instance. In the following code, pathToFolder indicates the file path for storing HTTP response cache records. Replace /path/dir with the sandbox path for storing HTTP response cache records.

    // Create a ResponseCache instance.
    const responseCache = new rcp.ResponseCache({
      persistent: {
        kind: 'file-system',
        pathToFolder: '/data/storage/el2/base/entry/temp/SomeInstance' // Select an appropriate path as needed.
      }
    });
  3. Create session A and session B. Reference the responseCache instance in both session A and session B.

    const sessionA: rcp.Session = rcp.createSession({
      requestConfiguration: {
        cache: responseCache,
      }
    });
    const sessionB: rcp.Session = rcp.createSession({
      requestConfiguration: {
        cache: responseCache,
      }
    });
  4. Initiate the first request using session A. Replace https://www.example.com with the actual URL that supports the HTTP caching protocol. This request retrieves data from the server. You can check the cache status to determine whether the retrieved data is successfully cached. In this example, the cache record count changes to 1.

    // The URL to request is only an example. Modify it as needed.
    const responseA = await sessionA.get('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}`);
  5. Initiate the second request using session B. Replace https://www.example.com with the actual URL that supports the HTTP caching protocol. This request directly retrieves data from the cache. At this point, you can check the cache status, and you will find that the cache hit count changes to 1.

    // The URL to request is only an example. Modify it as needed.
    const responseB = await sessionB.get('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}`);

Using the Same Cache Storage Path

You can create different ResponseCache instances with the same cache storage path and configure them for different sessions to share cached data between sessions.

  1. Import the required modules.

    import { rcp } from '@kit.RemoteCommunicationKit';
  2. Create the ResponseCacheA and ResponseCacheB instances that use the same cache storage path. In the following code, pathToFolder indicates the file path for storing HTTP response cache records. Replace /path/dir with the sandbox path for storing HTTP response cache records.

    // Create a ResponseCache instance.
    const responseCacheA = new rcp.ResponseCache({
      persistent: {
        kind: 'file-system',
        pathToFolder: '/data/storage/el2/base/entry/temp/SomeCache' // Select an appropriate path as needed.
      },
    });
    const responseCacheB = new rcp.ResponseCache({
      persistent: {
        kind: 'file-system',
        pathToFolder: '/data/storage/el2/base/entry/temp/SomeCache' // Select an appropriate path as needed.
      },
    });
  3. Create session A and session B. Configure the responseCacheA instance for session A and the responseCacheB instance for session B.

    const sessionA: rcp.Session = rcp.createSession({
      requestConfiguration: {
        cache: responseCacheA,
      }
    });
    const sessionB: rcp.Session = rcp.createSession({
      requestConfiguration: {
        cache: responseCacheB,
      }
    });
  4. Initiate the first request using session A. Replace https://www.example.com with the actual URL that supports the HTTP caching protocol. This request retrieves data from the server. You can check the cache status of the responseCacheA instance to determine whether the retrieved data is successfully cached. In this example, the cache record count changes to 1.

    // The URL to request is only an example. Modify it as needed.
    const responseA = await sessionA.get('https://www.example.com');
    console.info(`Request succeeded, message is ${JSON.stringify(responseA)}`);
    let cacheState = await responseCacheA.getState();
    console.info(`The current number of cache entries is: ${cacheState.count}`);
  5. Initiate the second request using session B. Replace https://www.example.com with the actual URL that supports the HTTP caching protocol. This request directly retrieves data from the cache. At this point, you can check the cache status of the responseCacheB instance, and you will find that both the cache record count and cache hit count change to 1.

    // The URL to request is only an example. Modify it as needed.
    const responseB = await sessionB.get('https://www.example.com');
    console.info(`Request succeeded, message is ${JSON.stringify(responseB)}`);
    cacheState = await responseCacheB.getState();
    console.info(`The current number of cache entries is: ${cacheState.count}`);
    console.info(`The current cache hit count is: ${cacheState.hitCount}`);