1、maven依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2、配置檔案application.properties在添加配置:
#redis 叢集
spring.redis.cluster.nodes=192.168.94.130:7000, 192.168.94.130:7001, 192.168.94.130:7002, 192.168.94.130:7003, 192.168.94.130:7004, 192.168.94.130:7005
#redis單個節點
#spring.redis.database=0
#spring.redis.host=192.168.94.130
#spring.redis.port=6379
#spring.redis.password=
#spring.redis.pool.max-idle=8
#spring.redis.pool.min-idle=0
#spring.redis.pool.max-active=8
#spring.redis.pool.max-wait=-1
#spring.redis.timeout=5000
3、redisTemplate測試:
@Autowired
RedisTemplate redisTemplate;
@Test
public void redisTest() {
String key = "aaa";
String value = "bbb";
ValueOperations opsForValue = redisTemplate.opsForValue();
//資料插入
opsForValue.set(key, value);
String valueFromRedis = opsForValue.get(key);
System.out.println(valueFromRedis)
//資料删除
redisTemplate.delete(key);
valueFromRedis = opsForValue.get(key);
System.out.println(valueFromRedis)
}
4.
Spring boot工程測試:
application.properties檔案
server.port=8080
#mysql
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://192.168.94.130:3306/mytest
spring.datasource.username=xuhaixing
spring.datasource.password=xuhaixing
#spring.jpa.hibernate.naming-strategy=com.xhx.ms.entity.MySQLUpperCaseStrategy
#redis
#spring.redis.database=0
#spring.redis.host=192.168.94.130
#spring.redis.port=6379
#spring.redis.password=
#spring.redis.pool.max-idle=8
#spring.redis.pool.min-idle=0
#spring.redis.pool.max-active=8
#spring.redis.pool.max-wait=-1
#spring.redis.timeout=5000
#redis 叢集
spring.redis.cluster.nodes=192.168.94.130:7000, 192.168.94.130:7001, 192.168.94.130:7002, 192.168.94.130:7003, 192.168.94.130:7004, 192.168.94.130:7005
#jpa
spring.jpa.database=mysql
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
javaConfig
package com.xhx.ms.config;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sun.org.apache.xml.internal.utils.StringBufferPool;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.lang.reflect.Method;
/**
* redis緩存配置
* Created by xuhaixing on 17-11-25.
*/
@Configuration
@EnableCaching//啟用緩存
public class RedisCacheConfig extends CachingConfigurerSupport {
/**
* 自定義key,此方法會根據類名+方法名+所有參數的值生成一個
* 唯一的key,即@Cacheable中的key
*/
@Override
public KeyGenerator keyGenerator(){
return new KeyGenerator() {
@Override
public Object generate(Object o, Method method, Object... objects) {
StringBuilder sb = new StringBuilder();
sb.append(o.getClass().getName());
sb.append(method.getName());
for(Object obj:objects){
sb.append(obj.toString());
}
System.out.println(sb);
return sb.toString();
}
};
}
/**
* redisTemplate緩存操作類
* @param redisConnectionFactory
* @return
*/
@Bean
public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
// StringRedisSerializer redisSerializer = new StringRedisSerializer();
// redisTemplate.setKeySerializer(redisSerializer);
// redisTemplate.setHashKeySerializer(redisSerializer);
setSerializer(redisTemplate);
return redisTemplate;
}
private void setSerializer(RedisTemplate template) {
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(
Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
template.setValueSerializer(jackson2JsonRedisSerializer);
template.setHashValueSerializer(jackson2JsonRedisSerializer);
}
/**
* 緩存管理器
*
*/
@Bean
public CacheManager cacheManager(RedisTemplate<?,?> redisTemplate){
RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate);
redisCacheManager.setDefaultExpiration(3000);//s
return redisCacheManager;
}
}
Service注入緩存
package com.xhx.ms.service;
import com.xhx.ms.entity.Person;
import com.xhx.ms.repository.PersonRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
* Created by xuhaixing on 17-11-25.
*/
@Service
public class PersonService {
@Resource
private PersonRepository personRepository;
@Autowired
private RedisTemplate<String,Object> redisTemplate;
public void test(){
ValueOperations<String, Object> valueOperations = redisTemplate.opsForValue();
valueOperations.set("mykey1","random1="+Math.random());
System.out.println(valueOperations.get("mykey1"));
}
@Cacheable(value="person")
public Person findById(String id){
System.out.println("------findById-----id = "+id);
return personRepository.findOne(id);
}
// @CacheEvict(value="person",allEntries=true) //删除全部
@CacheEvict(value="person",key="'com.xhx.ms.service.PersonServicefindById'+#id") //删除指定
public void deleteFromCache(String id){
System.out.println("------deleteFromCache-----從緩存删除");
}
}
@Cacheable會記住請求參數和傳回值,隻有第一次請求時執行了方法,後面請求都沒有執行方法,清楚緩存後又執行方法
主方法
package com.xhx.ms;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringbootRedisApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootRedisApplication.class, args);
}
}