springboot09-redis

时间:2021-10-04 18:03:59

redis安装:

从redis官网下载redis包,解压后:

springboot09-redis

cmd执行命令启动本地redis:

D:

cd D:\Program Files\redis2.4.5\64bit

redis-server.exe redis.conf

起动成功后,使用Redis DeskTop Manager客户端连接访问

springboot09-redis

下面开始java代码:

1.导入依赖

<!--redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
</dependency>

2.配置RedisConfig

 package com.mlxs.springboot09.redis.config;

 import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.cache.CacheManager;
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.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import java.lang.reflect.Method; /**
* RedisConfig类描述:
*
* @author yangzhenlong
* @since 2017/3/16
*/
@Configuration
@EnableCaching
public class RedisConfig { @Bean
public CacheManager cacheManager(RedisTemplate redisTemplate){
return new RedisCacheManager(redisTemplate);
} /**
* redisTemplate对象
* @param factory
* @return
*/
@SuppressWarnings("SpringJavaAutowiringInspection")
@Bean
public RedisTemplate<String, String> redisTemplate( RedisConnectionFactory factory){
StringRedisTemplate redisTemplate = new StringRedisTemplate (factory);
redisTemplate.setValueSerializer(this.getRedisSerializer());
return redisTemplate;
} /**
* key生成策略
* @return
*/
@Bean
public KeyGenerator keyGenerator(){
return new KeyGenerator() {
@Override
public Object generate(Object target, Method method, Object... params) {
StringBuilder sb = new StringBuilder();
sb.append(target.getClass().getName()).append(".")
.append(method.getName()).append("-");//类名.方法名
if(params.length > 0){
for(Object param : params){
sb.append("&" + param.toString());//&123&abc
}
}
return sb.toString();
}
};
} /**
* json序列化对象
* @return
*/
private Jackson2JsonRedisSerializer getRedisSerializer(){
Jackson2JsonRedisSerializer redisSerializer = new Jackson2JsonRedisSerializer(Object.class);
redisSerializer.setObjectMapper(this.getObjectMapper());
return redisSerializer;
} private ObjectMapper getObjectMapper(){
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
return objectMapper;
}
}

3.单元测试

@RunWith(SpringRunner.class)
@SpringBootTest(classes = MainApp.class)
public class RedisTest { @Autowired
private RedisTemplate redisTemplate; @Test
public void test(){
List<User> userList = User.buildUser();
for(User user : userList) {
redisTemplate.opsForValue().set("user" + user.getId(), user);
} Object user1 = redisTemplate.opsForValue().get("user1");
System.out.println("user1:" + user1);
}
}

查看redis中的值:

springboot09-redis

控制台打印:

springboot09-redis

4.写service,使用key生成策略

@Service
public class UserService { @Cacheable(value = "userCache", keyGenerator = "keyGenerator")//设置redis 和
public List<User> users(){
return User.buildUser();
}
}

5.写controller,调用service。第一次调用,会在redis写入值,第二次调用不会再进service,直接从redis读取值

@RestController
public class UserController { @Autowired
private UserService userService; @RequestMapping("/users")
public List<User> list(){
return userService.users();
}
}

启动springboot启动类,访问http://localhost:8080/users

springboot09-redis

查看reids

springboot09-redis

相关文章