Contact Free trial Login

Redis Examples - Mule 4

XML Save a Key Value

  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 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="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 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:

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

XML Custom Object Store

This use case describes how to use the 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/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’s 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 n 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:

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

See Also

We use cookies to make interactions with our websites and services easy and meaningful, to better understand how they are used and to tailor advertising. You can read more and make your cookie choices here. By continuing to use this site you are giving us your consent to do this.