Contact Free trial Login

Redis Connector

Select

Redis is an open source, advanced key-value store. It is often referred to as a data structure server since keys can contain strings, hashes, lists, sets and sorted sets.

The Redis Connector offers complete support for its CRUD API.

Prerequisites

This document assumes that you are familiar with Mule, Anypoint Connectors, Anypoint Studio essentials, elements in a Mule flow, and global elements.

Namespace and Schema

When designing your application in Studio, the act of dragging the connector from the palette onto the Anypoint Studio canvas should automatically populate the XML code with the connector namespace and schema location.

If you manually code the Mule application in Studio’s XML editor or other text editor, paste these into the header of your Configuration XML, inside the <mule> tag.

<mule xmlns:redis="http://www.mulesoft.org/schema/mule/redis"
  ...
  xsi:schemaLocation="http://www.mulesoft.org/schema/mule/redis http://www.mulesoft.org/schema/mule/connector/current/mule-redis.xsd">
  ...
  <flow name="yourFlow">
  ...
  </flow>
</mule>

Using the Connector in a Mavenized Mule App

If you are coding a Mavenized Mule application, this XML snippet must be included in your pom.xml file.

<dependency>
  <groupId>org.mule.modules</groupId>
  <artifactId>mule-redis-connector</artifactId>
  <version>RELEASE</version>
</dependency>

Inside the <version> tags, put the desired version number, the word RELEASE for the latest release, or SNAPSHOT for the latest available version.

Compatibility

Software Version

Mule Runtime

4.0.0 or higher

Redis server

2.8.x, 3.0.x, and 3.2.x

Installing

  1. In Anypoint Studio, click the Exchange icon in the Studio taskbar.

  2. Click Login in Anypoint Exchange.

  3. Search for the connector and click Install.

  4. Follow the prompts to install the connector.

When Studio has an update, a message displays in the lower right corner, which you can click to install the update.

To use Redis connector in your Mule application, configure a global Redis element that can be used by all the connectors in the application.

The sections that follow provide the properties to configure the global element.

Non-Clustered Configuration

Configuration

In the image above, the placeholder values refer to a configuration file placed in the src folder of your project. You can either hardcode your credentials into the global configuration properties, or reference a configuration file that contains these values. For simpler maintenance and better re-usability of your project, Mule recommends that you use a configuration file. Keeping these values in a separate file is useful if you need to deploy to different environments, such as production, development, and QA, where your access credentials differ.

Field Description

Name

Enter a name for this configuration to reference it later.

Host

Redis host. Use this field when you want to access you Redis server that is not part of a cluster through the connector.

Port

Redis port. The port on which your non cluster server is running.

Connection Timeout

Connection timeout in milliseconds.

Entry Ttl

The default time to live to be set on keys stored through the connector.

Password

Redis password. Set this in case that the server requires authentication.

Clustered Configuration

Configuration
Field Description

Name

Enter a name for this configuration to reference it later.

Cluster Hosts

Redis cluster hosts, separated by comma and with the format host:port. Use this field when you want to access you Redis cluster through the connector.

Connection Timeout

Connection timeout in milliseconds.

Entry Ttl

The default time to live to be set on keys stored through the connector.

Upgrading from an Older Version

If you’re currently using an older version of the connector, a small popup appears in the bottom right corner of Anypoint Studio with an "Updates Available" message.

  1. Click the popup and check for available updates.

  2. Click the Connector version checkbox and click Next and follow the instructions provided by the user interface.

  3. Restart Studio when prompted.

  4. After restarting, when creating a flow and using the Object Store Connector, if you have several versions of the connector installed, you may be asked which version you would like to use. Pick one of these.

We recommend that you keep Studio up to date with the connector’s latest version.

Using This Connector

You can use this connector as an inbound endpoint for consuming messages from a channel. You can subscribe to one of these channels through the connector or as an outbound connector for sending commands to the Redis server.

Save a Value for a Key into a Redis Server

This use case describes how to use the connector to assign a value to a key in the Redis server.

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

  2. In the new project dialog box, provide a name for your new project. Click Finish.

    New project dialog
  3. Navigate through the project’s structure and double click src/main/mule/project-name.xml to open it. The steps below are all performed on this file.

  4. Go to the palette and search for HTTP, then drag and drop a new HTTP Connector Listener to the canvas. This element is the entry point for the flow and provides the key and value to be set for that key.

  5. Go to the palette and search for Redis, then drag and drop a new Redis Set operation after the HTTP connector. This element sends data to the Redis server.

  6. Double click the Redis Set operation and set its properties as follows:

    1. Set Display Name to Set Value For Key Into Redis.

    2. Choose from the Extension Configuration drop down Redis__Configuration, which is the default name of a configuration, or any other configuration that you configured.

    3. Choose Set from the Operation drop down.

    4. Set Key to #[payload.key].

    5. Set Value to #[payload.value].

      redis set config
  7. Go to the palette and search for Set Payload, then drag and drop a new Set Payload element after the Redis connector. This element creates the response for the incoming HTTP request.

    redis set raw flow
  8. Double click the flow’s top margin to open its properties, and change the name of the flow to set-flow.

    redis set flow config
  9. Double click the HTTP Connector [Listener] to open its properties.

    1. Click the green plus sign beside the Connector Configuration drop down menu.

    2. A pop-up appears, leave the default configurations and click ok.

    3. Set Path to /.

    4. Set Display Name to Listener.

      Set HTTP configuration
  10. Double click *Set Payload and set its properties as below.

    1. Set Display Name to Set Value Response.

    2. Set Value to Value Successfully Set.

      Set HTTP response configuration
  11. If you configured a Redis global element with placeholder values, you must now provide values for these placeholders. Open /src/main/resources/mule-app.properties and provide values for following properties: config.host, config.port, and config.connectionTimeout.

  12. Deploy the app.

  13. Once the app is running, send an HTTP request to it to trigger its flow. To do this, use the curl command line utility or an HTTP client app (such as Postman) to send a POST request with content-type application/x-www-form-urlencoded and a body in url-encoded format to localhost:8081/. The request’s body should contain a key and a value. For this you can use the following curl command:

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

    Congratulations! You have just set a value for a key into the Redis server.

Save a Value for a Key into Redis Server Code

  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/sfdc-composite/current/mule-redis.xsd
  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
  5. Add a redis:config element to your project, then configure its attributes:

    <redis:config name="Redis__Configuration" host="${config.host}"
     connectionTimeout="${config.connectionTimeout}" port="${config.port}"
     doc:name="Redis: Configuration"/>
  6. Add a 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="HTTP Listener Configuration"/>
  7. Add an empty flow element to your project:

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

    <http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="Set value HTTP endpoint"/>
  9. Within the flow element, add a redis:set after the http:listener:

    <redis:set config-ref="Redis__Configuration" key="#[payload.key]" value="#[payload.value]" doc:name="Set value for key into Redis"/>
  10. Within the flow element, add a set-payload element after redis:set:

    <set-payload value="Value successfully set" doc:name="Set value response"/>
  11. When you’re done, the XML file should look like this:

    <?xml version="1.0" encoding="UTF-8"?>
    
    <mule xmlns:redis="http://www.mulesoft.org/schema/mule/redis"
    xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking"
    xmlns:http="http://www.mulesoft.org/schema/mule/http"
    xmlns="http://www.mulesoft.org/schema/mule/core"
    xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
    	xmlns:spring="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans
    	http://www.springframework.org/schema/beans/spring-beans-current.xsd
    http://www.mulesoft.org/schema/mule/core
    http://www.mulesoft.org/schema/mule/core/current/mule.xsd
    http://www.mulesoft.org/schema/mule/http
    http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
    http://www.mulesoft.org/schema/mule/ee/tracking
    http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd
    http://www.mulesoft.org/schema/mule/redis
    http://www.mulesoft.org/schema/mule/redis/current/mule-redis.xsd">
        <redis:config name="Redis__Configuration"
        host="${config.host}"
        connectionTimeout="${config.connectionTimeout}" port="${config.port}"
        doc:name="Redis: Configuration"/>
        <http:listener-config name="HTTP_Listener_Configuration"
        host="0.0.0.0"
        port="8081" doc:name="HTTP Listener Configuration"/>
        <flow name="set-flow">
            <http:listener config-ref="HTTP_Listener_Configuration" path="/"
             doc:name="Set value HTTP endpoint"/>
            <redis:set config-ref="Redis__Configuration" key="#[payload.key]"
             value="#[payload.value]" doc:name="Set value for key into Redis"/>
            <set-payload
             value="Successfully set value: #[payload.value] to key: #[payload.key]"
             doc:name="Set value response"/>
        </flow>
    </mule>

Use the Redis Connector as a Custom Object Store

This use case describes how the Redis connector can be used as a custom Object Store in a Mule application.

  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/sfdc-composite/current/mule-redis.xsd
  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
  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 namespace:

    http://www.mulesoft.org/schema/mule/os
    http://www.mulesoft.org/schema/mule/os/current/mule-os.xsd
  7. Add an os:config element to your project, then configure it to contain a Redis connection type (clustered or non-clustered). This is only possible in Studio and must be configured through XML (there is no support using the Studio graphic configuration).

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

    <os:object-store name="Object_store"
    	config-ref="ObjectStore_Config"
    	maxEntries="1"
    	entryTtl="60"
    	expirationInterval="10"
    	expirationIntervalUnit="SECONDS"/>
  9. Add a 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="HTTP Listener Configuration"/>
  10. Add an empty flow element to your project:

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

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

    <os:store
    	key="#[attributes.queryParams.key]"
    	objectStore="Object_store"
    	failIfPresent="true"
    	failOnNullValue="false">
    	<os:value ><![CDATA[#[attributes.queryParams.value]]]></os:value>
    </os:store>
  13. When you’re done, the XML file should look like this:

    <?xml version="1.0" encoding="UTF-8"?>
    
    <mule xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
    	xmlns:http="http://www.mulesoft.org/schema/mule/http"
    	xmlns:os="http://www.mulesoft.org/schema/mule/os"
    	xmlns:redis="http://www.mulesoft.org/schema/mule/redis"
    	xmlns="http://www.mulesoft.org/schema/mule/core"
    	xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="
    	http://www.mulesoft.org/schema/mule/ee/core
    	http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd
    	http://www.mulesoft.org/schema/mule/core
    	http://www.mulesoft.org/schema/mule/core/current/mule.xsd
    	http://www.mulesoft.org/schema/mule/redis
    	http://www.mulesoft.org/schema/mule/redis/current/mule-redis.xsd
    	http://www.mulesoft.org/schema/mule/os
    	http://www.mulesoft.org/schema/mule/os/current/mule-os.xsd
    	http://www.mulesoft.org/schema/mule/http
    	http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">
    
    	<http:listener-config name="HTTP_Listener_config">
    		<http:listener-connection
    		host="0.0.0.0"
    		port="8081" />
    	</http:listener-config>
    
    	<os:object-store name="Object_store"
    		config-ref="ObjectStore_Config"
    		maxEntries="1"
    		entryTtl="60"
    		expirationInterval="10"
    		expirationIntervalUnit="SECONDS"/>
    
    	<os:config name="ObjectStore_Config">
            	<redis:nonclustered-connection
    		host="${redis.host}"/>
        	</os:config>
    
    	<flow name="StoreFlow" >
            <http:listener doc:name="Listener" config-ref="HTTP_Listener_config"
    		path="/store"/>
            <os:store
    		doc:name="Store"
    		key="#[attributes.queryParams.key]"
    		objectStore="Object_store"
    		failIfPresent="true"
    		failOnNullValue="false">
    		<os:value ><![CDATA[#[attributes.queryParams.value]]]></os:value>
    	</os:store>
        	</flow>
    </mule>

Was this article helpful?

πŸ’™ Thanks for your feedback!

Edit on GitHub