Data Caching Basics
Warning
Starting in a future release, Redis™ will no longer ship with MATLAB®
            Production Server™ and MATLAB
            Compiler SDK™. Redis supports the data persistence functionality in these products. Provide
                your own Redis persistence service instead. Affected functions include start, stop, and restart.
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
| Steps | Command Line | Dashboard | 
|---|---|---|
| 1. Create file mps_cache_config | Manually create a JSON file and place it in the configfolder of the server instance. Do not
                                include the.jsonextension in the
                                filename. | Automatically created. | 
| 2. Start persistence service | Use  For testing purposes, you can create a persistence
                                    service controller object using  | 
 | 
| 3. Create a data cache | Use mps.cache.connectto create a data cache. | Use mps.cache.connectto 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. 
- WSL should be installed for the Windows user account that will be running MATLAB Production Server. 
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
- 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) 
- Write MATLAB code that creates a cache and then updates a counter using the cache. Name the file - myCounter.m.
- 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 (MATLAB Production Server) or follow the steps in the dashboard. This example
                    assumes your server instance uses the default host and port:
                        localhost:9910.
- Package the file - myCounter.musing the Production Server Compiler app or- mcc.
- Deploy the archive ( - myCounter.ctffile) to the server.
- Test the counter. You can make calls to the server using the RESTful API for MATLAB Function Execution (MATLAB Production Server) 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
mps.cache.Controller | mps.cache.DataCache | mps.sync.TimedMATFileMutex | mps.sync.TimedRedisMutex | mps.cache.control | mps.cache.connect | mps.sync.mutex
