Main Content

Data Caching Basics

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.

Typical Workflow for Data Caching

StepsCommand LineDashboard
1. Create file mps_cache_configManually create a JSON file and place it in the config folder of the server instance. Do not include the .json extension in the filename.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 (MATLAB Compiler SDK).

  • 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 (MATLAB Compiler SDK) to create a data cache.Use mps.cache.connect (MATLAB Compiler SDK) to create a data cache.

Configure Server to Use Redis

Create Redis Configuration File

Before starting a persistence service for an on-premises server instance from the system command prompt, you must create a JSON file called mps_cache_config (no .json extension) and place it in the config folder of the server instance. If you use the dashboard to manage an on-premises server instance and for server deployments on the cloud, the mps_cache_config file is automatically created on server creation.

mps_cache_config

{
  "Connections": {
    "<connection_name>": {
      "Provider": "Redis",
      "Host": "<hostname>",
      "Port": <port_number>,
      "Key": <access_key>  
    }
  }
}

Specify the <connection_name>, <hostname>, and <port_number> in the JSON file. The host name can either be localhost or a remote host name obtained from an Azure® Redis cache resource. If you use Azure Cache for Redis, you must specify an access key. To use an Azure Redis cache, you need a Microsoft® Azure account.

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 config folder of the server instance.

Install WSL for Server Instances Running on Windows

If your MATLAB Production Server™ instance runs on a Windows® machine, you require additional configuration. The following configuration is not required for server instances that run on Linux® and macOS.

  • You need to install Windows Subsystem for Linux (WSL). For details on installing WSL, see Microsoft documentation.

  • If the MATLAB Production Server software is installed on a network drive, you must mount the network drive in WSL.

Example: Increment Counter Using 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 in MATLAB Compiler SDK

  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 =
    
         0    1     2     3     4

Deployment Workflow Using MATLAB Production Server

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 for MATLAB Function Execution 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 =
    
         0    1     2     3     4

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

See Also

(MATLAB Compiler SDK) | (MATLAB Compiler SDK) | (MATLAB Compiler SDK) | (MATLAB Compiler SDK) | (MATLAB Compiler SDK) | (MATLAB Compiler SDK) | (MATLAB Compiler SDK)

Related Topics