Redis installation, deployment, and integration with spring cache

Source: Internet
Author: User
Tags redis server

Installation

Go to http://redis.io/Download the latest stable version of the source code. Unzip, go to unzip directory, execute

makemake install

After src/ that, there are several more files in the directory:

redis-serverredis-benchmarkredis-cliredis-conf

To the copy /usr/redis directory.

Deployment

redis-confto modify a file, add:

requirepass 111111

Set the connection password to 111111 . And then execute

./redis-server redis-conf

To start the Redis server.

Integration with spring cache

First pom.xml Add the Jedis and spring data redis dependencies in:

<!--Spring Data Redis ---        <dependency>            <groupId>Org.springframework.data</groupId>            <artifactid>Spring-data-redis</artifactid>            <version>${spring-redis}</version>        </Dependency>        <dependency>            <groupId>Redis.clients</groupId>            <artifactid>Jedis</artifactid>            <version>${jedis}</version>        </Dependency>

Because we're going to use Jackson to provide the class serialization feature, we need to add:

<dependency>            <groupId>org.codehaus.jackson</groupId>            <artifactId>jackson-mapper-asl</artifactId>            <version>1.9.4</version>        </dependency>

Then, add the following in the spring configuration file:

<!--redis cache configuration --     <beans:bean id= "redisconnectionfactory"class=" Org.springframework.data.redis.connection.jedis.JedisConnectionFactory " >                        <beans:property name="HostName" value="XX.XX.XX.XX" />         <beans:property name="password" value="111111" / >    </Beans:bean>    <!--serializer --    <beans:bean id= "keyserializer" class=" Org.springframework.data.redis.serializer.StringRedisSerializer " />    <beans:bean id= "ValueSerializer" class=" Org.springframework.data.redis.serializer.JacksonJsonRedisSerializer " />    <!--redis template ---    <beans:bean id= "redistemplate"class=" Org.springframework.data.redis.core.StringRedisTemplate ">                        <beans:property name= "connectionfactory" ref=" Redisconnectionfactory " />        <beans:property name="Keyserializer" ref="Keyserializer " />        <beans:property name="ValueSerializer" ref="ValueSerializer" />    </Beans:bean>    <!--cache Manager --    <beans:bean id= "CacheManager" class=" Org.springframework.data.redis.cache.RedisCacheManager ">        <beans:constructor-arg name="template" ref="Redistemplate" / >    </Beans:bean>

Once you have done this, you can use the method in the service method @Cacheable :

@Override    @Transactionaltrue)    @Cacheable"cache""#username")    publicfindMemberboolean isWired) {        MemberModel mem = memMapper.selectByUsername(username);        CheckUtils.nullCheck(mem);        return mem;    }

However, using the spring cache has the disadvantage of not setting the cache expiration time. If there is a need for this, you must use the client directly instead jedis spring cache .

Redis installation, deployment, and integration with spring cache

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.