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 an on-premises server instance from the
system command prompt, you must 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": "<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.
Workflow to Use Persistence
Steps | Command Line | Dashboard |
---|---|---|
1. Create file mps_cache_config | Manually create a JSON file and place it in the
config folder of the server instance. | 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.connect to create a data cache. | Use mps.cache.connect to create 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.
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
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
.
Package the file myCounter.m
using the
Production Server Compiler app or
mcc
.
Deploy the archive (myCounter.ctf
file) to the
server.
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 = 0 1 2 3 4
As expected, the results from the testing environment workflow and the deployment environment workflow are the same.
get
| mps.cache.connect
| mps.cache.control
| mps.cache.Controller
| mps.cache.DataCache
| mps.sync.mutex
| mps.sync.TimedMATFileMutex
| mps.sync.TimedRedisMutex
| put