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-conf
to 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