Redis Configuration and beginner operations in spring

Source: Internet
Author: User

When spring and Redis are combined, the configuration bean is often the first to fix the Jedispoolconfig object, which reads:

<bean id= "Poolconfig" class= "Redis.clients.jedis.JedisPoolConfig" >
<property name= "maxactive" value= "/>"
<property name= "Maxidle" value= "/>"
<property name= "maxwait" value= "20000"/>
</bean>
Then configure the Jedisconnectionfactory object:
 <bean id= "ConnectionFactory" class= " Org.springframework.data.redis.connection.jedis.JedisConnectionFactory, 
<property name= "HostName" Value= "192.168.36.131"/>
<property name= "Port" value= "6379"/>
<property name= "password" value= " 123456 "/>
<property name=" Poolconfig "ref=" Poolconfig "/>
</bean>
Because Java objects cannot be stored directly in Redis, you need to serialize Java objects, have a key sequencer and a value sequencer, and configure key and value in the Redistemplate object:
 <bean id= "Jdkserializationredisserializer" class= " Org.springframework.data.redis.serializer.JdkSerializationRedisSerializer "/> 
<bean id=" Stringredisserializer "class=" Org.springframework.data.redis.serializer.StringRedisSerializer "/>
<bean Id= "Redistemplate" class= "org.springframework.data.redis.core.RedisTemplate";
<property name= " ConnectionFactory "ref=" ConnectionFactory "/>
<property name=" Keyserializer "ref=" StringRedisSerializer "/
<property name= "ValueSerializer" ref= "Jdkserializationredisserializer"/>
</bean>
With the above configuration, you can operate the Redis database, the following is a code for a simple test connection
ApplicationContext applicationcontext=new classpathxmlapplicationcontext ("Applicationcontext.xml");
Redistemplate redistemplate= (redistemplate) Applicationcontext.getbean ("Redistemplate");

Redistemplate.opsforvalue (). Set ("Role1", "Zhangsan");
Sessioncallback sessioncallback=new Sessioncallback () {//Sessioncallback mainly because set and get operations when they may not access a Redis connection, and call them through the Sessioncallback interface. You can use only one Redis connection
@Override
Public Object Execute (redisoperations redisoperations) throws DataAccessException {
Redisoperations.opsforvalue (). Set ("Role2", "Lisi");
Return Redisoperations.opsforvalue (). Get ("Role2");
}
};
String Aa=redistemplate.execute (sessioncallback). toString ();
SYSTEM.OUT.PRINTLN (AA);

Redis Configuration and beginner operations in spring

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.