Configure Redis in CUBA platform

Hi team,
Im using CUBA latest version. I need to batch update in the server. So by using Redis we are going to batch update the values in DB. How we can configure Redis with our CUBA platform…?

1 Like

Hi,

Just a reminder, that CUBA application is basically a Spring application, so you can use almost all spring libraries in CUBA. So, for Redis, you can use spring-data-redis.

For example, we can create a service that will use Redis. To do this, you need to add a dependency in core module:

        compile "org.springframework.data:spring-data-redis:2.3.0.RELEASE"
        compile "redis.clients:jedis:3.3.0"

And configure corresponding beans. I have Redis installed locally, so my configuration in core module looks like this (file spriing.xml):

    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
          p:use-pool="true"/>
    <!-- redis template definition -->
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
          p:connection-factory-ref="jedisConnectionFactory"/>

After that, you can create a service and use RedisTemplate in it like it is described in the Spring documentation. Simple example:

@Service(RedisService.NAME)
public class RedisServiceBean implements RedisService {

    @Autowired
    private RedisTemplate<String, String> template;

    @Resource(name="redisTemplate")
    private ListOperations<String, String> listOps;

    @Override
    public String storeBook(Book book) {
        listOps.leftPush(book.getTitle(), book.getAuthor());
        return listOps.rightPop(book.getTitle());
    }
}

Then you can invoke this service from screens, other services, etc.There is a small PoC project attached.

redis-sample.zip (84.5 KB)

2 Likes

Thanks lot Andrey…

Hi Kannan,

Did it help?

1 Like

Hi Andrey,
Yeah it was helpful…but we need some clarification …we are going to use redis cache to store data from that we will move the data to db…so we need how to configure server connection with redis in local…for example hostname and port we have to add in spring…where we have to add and how to configure the server connection…?

I have attached what i have add in spring.xml file…but it showing error…

redis

Hi Kannan,

Which error do you get? I use the following configuration and it works for me:

    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
          p:host-name="127.0.0.1" p:port="6379" p:usePool="true" p:database="0"/>

1 Like

Hi Andrey,
Tnx man…it is working…