data caching basics -凯发k8网页登录

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 to start a persistence service.

for testing purposes, you can create a persistence service controller object using (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 (matlab compiler sdk) to create a data cache.use (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": {
    "": {
      "provider": "redis",
      "host": "",
      "port": ,
      "key":   
    }
  }
}

specify the , , and 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 .

  • 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

     

  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 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 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

    网站地图