Hear from Salesforce leaders on how to create and deploy Agentforce agents.
Contact Us 1-800-596-4880

Redis Connector 5.4 Examples - Mule 4

The following examples show how to use Redis Connector to set a key value on the Redis server and how to use Redis Connector as a custom object store in a Mule app.

Set a Key Value on the Redis Server

The following screenshot shows the flow for this example:

Studio flow with Listener, Set, and Set Payload

To use Anypoint Studio to set a key value on the Redis server:

  1. Create a new Mule project by clicking File > New > Mule Project.

  2. Enter a name for the new project and click Finish.

  3. In the Mule Palette view, select HTTP > Listener and drag it to the Studio canvas.

    HTTP Listener is the entry point for the flow and provides the key and its value.

  4. Click the plus sign (+) next to the Connector Configuration field.

  5. Leave the default configuration and click OK.

  6. Set the Path field to /.

  7. In the Mule Palette view, search for redis and drag the Set operation to the right of Listener.

    The Set operation sends data to the Redis server.

  8. Set the properties for the Set operation as follows:

    • Set the Display Name field to Set value for key into Redis.

    • Set the Key field to #[payload.key].

    • Set the Value field to #[payload.value]:

      Set operation properties window on General tab
  9. In the Mule Palette view, search for payload and drag the Set Payload component to the right of Set value for key into Redis on the Studio canvas.

    The Set Payload component creates the response for the incoming HTTP request.

  10. Set the Set Payload properties as follows:

    • Set the Display Name field to Set value response.

    • Set the Value field to Successfully set value: #[payload.value] to key: #[payload.key]:

      Set Payload properties window on General tab
  11. Save your changes and deploy the app by right-clicking the Studio canvas and selecting Run project <project name>.

  12. When the app is running, use an HTTP client app to send a POST request to localhost:8081/ with content-type application/x-www-form-urlencoded and a body in url-encoded format.

    The request body should contain a key and value:

    curl -X POST -d "key=test-key" -d "value=test-value" localhost:8081/

The XML for this example should look like this:

Use Redis Connector as a Custom Object Store

In Studio to configure Redis Connector as a custom object store in a Mule app:

  1. Add the Redis namespace to the <mule> element:

    xmlns:redis="http://www.mulesoft.org/schema/mule/redis"
  2. Add the location of the Redis schema referred to by the Redis namespace:

    http://www.mulesoft.org/schema/mule/redis
    http://www.mulesoft.org/schema/mule/redis/current/mule-redis.xsd
    text
  3. Add the HTTP namespace to the <mule> element:

    xmlns:http="http://www.mulesoft.org/schema/mule/http"
  4. Add the location of the HTTP schema referred to by the HTTP namespace:

    http://www.mulesoft.org/schema/mule/http
    http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
    text
  5. Add the object store namespace to the <mule> element:

    xmlns:os="http://www.mulesoft.org/schema/mule/os"
  6. Add the location of the object store schema referred to by the object store’s namespace:

    http://www.mulesoft.org/schema/mule/os
    http://www.mulesoft.org/schema/mule/os/current/mule-os.xsd
    text
  7. Using the Studio XML editor, add an <os:config> element to your project and configure it to contain a Redis connection type (clustered-connection or nonclustered-connection).

    <os:config name="ObjectStore_Config">
         <redis:nonclustered-connection host="${redis.host}"/>
    </os:config>
    xml
  8. Add an <os:object-store> element to your project and configure it to reference the configuration created previously:

    <os:object-store
    	name="Object_store"
    	config-ref="ObjectStore_Config"
    	maxEntries="1"
    	entryTtl="60"
    	expirationInterval="10"
    	expirationIntervalUnit="SECONDS"/>
    xml
  9. Add an <http:listener-config> element to your project and configure its attributes:

    <http:listener-config
    	name="HTTP_Listener_Configuration"
    	host="0.0.0.0"
     	port="8081" doc:name="Listener"/>
    xml
  10. Add an empty <flow> element to your project:

    <<flow> name="set-flow">
    </flow>
    xml
  11. Within the <flow> element, add an <http:listener> element:

    <http:listener config-ref="HTTP_Listener_Configuration" path="/" />
    xml
  12. Within the <flow> element, add an <os:store> element after the <http:listener> element and configure <os:store> to use the object store created previously:

    <os:store
    	key="#[attributes.queryParams.key]"
    	objectStore="Object_store"
    	failIfPresent="true"
    	failOnNullValue="false">
    	<os:value ><![CDATA[#[attributes.queryParams.value]]]></os:value>
    </os:store>
    xml

    When you’re done, the XML file should look like this:

View on GitHub