1, 引入 jar,pom 配置
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-data-Redis</artifactId>
- </dependency>
- <dependency>
- <groupId>org.apache.commons</groupId>
- <artifactId>commons-pool2</artifactId>
- </dependency>
2,Redis 配置, application.YAML
- #Redis
- Redis:
- host: 127.0.0.1
- port: 6379
- password:
- database: 1
- timeout: 10000 # 连接超时时间(毫秒)
- lettuce:
- pool:
- max-active: 8 # 连接池最大连接数 (使用负值表示没有限制) 默认 8
- max-wait: -1 # 连接池最大阻塞等待时间 (使用负值表示没有限制) 默认 -1
- max-idle: 8 # 连接池中的最大空闲连接默认 8
- min-idle: 0 # 连接池中的最小空闲连接默认 0
3.RedisConfig 配置
- package com.deploy.config;
- import org.springframework.boot.autoconfigure.AutoConfigureAfter;
- import org.springframework.boot.autoconfigure.data.Redis.RedisAutoConfiguration;
- 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.lettuce.LettuceConnectionFactory;
- import org.springframework.data.Redis.core.RedisTemplate;
- import org.springframework.data.Redis.serializer.GenericJackson2JsonRedisSerializer;
- import org.springframework.data.Redis.serializer.StringRedisSerializer;
- import javax.annotation.Resource;
- import java.io.Serializable;
- import java.lang.reflect.Method;
- import java.util.HashSet;
- import java.util.Set;
- /**
- * Redis 缓存配置类
- * @author xuc
- *
- */
- @Configuration
- @EnableCaching
- @AutoConfigureAfter(RedisAutoConfiguration.class)
- public class RedisConfig extends CachingConfigurerSupport {
- public RedisConfig() {
- System.out.println("RedisConfig 容器启动初始化...");
- }
- @Resource
- private LettuceConnectionFactory lettuceConnectionFactory;
- @Bean
- public KeyGenerator keyGenerator() {
- return new KeyGenerator() {
- @Override
- public Object generate(Object target, Method method, Object... params) {
- StringBuffer sb = new StringBuffer();
- sb.append(target.getClass().getName());
- sb.append(method.getName());
- for (Object obj : params) {
- sb.append(obj.toString());
- }
- return sb.toString();
- }
- };
- }
- // 缓存管理器
- @Bean
- public CacheManager cacheManager() {
- RedisCacheManager.RedisCacheManagerBuilder builder = RedisCacheManager.RedisCacheManagerBuilder
- .fromConnectionFactory(lettuceConnectionFactory);
- @SuppressWarnings("serial")
- Set<String> cacheNames = new HashSet<String>() {
- {
- add("codeNameCache");
- }
- };
- builder.initialCacheNames(cacheNames);
- return builder.build();
- }
- @Bean
- public RedisTemplate<String,Serializable> redisCacheTemplate(LettuceConnectionFactory redisConnectionFactory){
- RedisTemplate<String,Serializable> template = new RedisTemplate<String,Serializable>();
- template.setKeySerializer(new StringRedisSerializer());
- template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
- template.setHashKeySerializer(new StringRedisSerializer());// Hash key 序列化
- template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());// Hash value 序列化
- template.setConnectionFactory(redisConnectionFactory);
- return template;
- }
- }
4,RedisTemplate 使用
- @Autowired
- private RedisTemplate<String, Serializable> redisCacheTemplate;
- @RequestMapping("test")
- public Map test(){
- Map<String,Object> result = new HashMap<>();
- result.put("code", "000000");
- result.put("msg", "success");
- User u = new User();
- u.setId("123");
- u.setState(23);
- u.setUser_photo("huangjinjin");
- u.setPosition("cto");
- result.put("body", u);
- redisCacheTemplate.opsForValue().set(String.valueOf(u.getId()), u);
- redisCacheTemplate.opsForValue().set("code",123412);
- Serializable code = redisCacheTemplate.opsForValue().get("code");
- return successList(result);
- }
5,RedisTemplate 主要 API
RedisTemplate 中定义了对 5 种数据结构操作
- redisTemplate.opsForValue();// 操作字符串
- redisTemplate.opsForHash();// 操作 hash
- redisTemplate.opsForList();// 操作 list
- redisTemplate.opsForSet();// 操作 set
- redisTemplate.opsForZSet();// 操作有序 set
API 参考连接
https://www.cnblogs.com/shamo89/p/8622152.html
来源: http://www.jianshu.com/p/6f29e15b167a