This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Use a Data Cache to Persist Data

Persistence provides a mechanism to cache data between calls to MATLAB® code running on a server instance. A persistence service runs separately from the server instance and can be started and stopped manually. A connection name links a server instance to a persistence service. A persistence service uses a persistence provider to store data. Currently, Redis™ is the only supported persistence provider. The connection name is used in MATLAB application code to create a data cache in the linked persistence service.

Before starting a persistence service for a server instance from the system command prompt, you need to create a JSON file called mps_cache_config and place it in the config folder of the server instance.

mps_cache_config

{
  "Connections": {
    "<connection_name>": {
      "Provider": "Redis",
      "Host": "localhost",
      "Port": <port_number>
    }
  }
}

Specify the <connection_name> and <port_number> in the JSON file. You can specify multiple connections in the file mps_cache_config. Each connection must have a unique name and a unique (host, port) pair. If you are using the persistence service through the dashboard, the file mps_cache_config is automatically created in the server instance config folder.

Workflow to Use Persistence

StepsCommand LineDashboard
1. Create file mps_cache_configManually create a JSON file and place it in the config folder of the server instance.Automatically created.
2. Start persistence service

Use mps-cache to start a persistence service.

For testing purposes, you can create a persistence service controller object using mps.cache.control.

  • Create a persistence service.

  • Add the persistence service to a server instance using a connection name.

  • Start the persistence service.

  • Attach the connection associated with a persistence service to a server instance.

3. Create a data cacheUse mps.cache.connect to create a data cache.Use mps.cache.connect to create a data cache.

Example: Increment a Counter Using a Data Cache

This example shows you how to use persistence to increment a counter using a data cache. The example presents two workflows: a testing workflow that uses the MATLAB and a deployment workflow that requires an active server instance.

Testing Workflow

  1. Create a persistence service that uses Redis as the persistence provider and start the service.

    ctrl = mps.cache.control('myRedisConnection','Redis','Port',4519)
    start(ctrl)
    
  2. Write MATLAB code that creates a cache and then updates a counter using the cache. Name the file myCounter.m

     myCounter.m

  3. Test the counter.

    for i = 1:5
        y(i) = myCounter('myCache','myRedisConnection');
    end
    y
    y =
    
         1     2     3     4     5

Deployment Workflow

Before you deploy code that uses persistence to a server instance, start the persistence service and attach it to the server instance. You can start the persistence service from the system command line using mps-cache or follow the steps in the dashboard. This example assumes your server instance uses the default host and port: localhost:9910.

  1. Package the file myCounter.m using the Production Server Compiler app or mcc.

  2. Deploy the archive (myCounter.ctf file) to the server.

  3. Test the counter. You can make calls to the server using the RESTful API from the MATLAB desktop.

    rhs = {['myCache'],['myRedisConnection']};
    body = mps.json.encoderequest(rhs,'Nargout',1);
    
    options = weboptions;
    options.ContentType = 'text';
    options.MediaType = 'application/json'; 
    options.Timeout = 30;
    
    for i = 1:5
    response = webwrite('http://localhost:9910/myCounter/myCounter', body, options);
    x(i) = mps.json.decoderesponse(response);
    end
    x = [x{:}]
    x =
    
         1     2     3     4     5

As expected, the results from the testing environment workflow and the deployment environment workflow are the same.

See Also

| | | | | |