Chinaunix首页 | 论坛 | 博客
  • 博客访问: 563941
  • 博文数量: 298
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 3077
  • 用 户 组: 普通用户
  • 注册时间: 2019-06-17 10:57
文章分类

全部博文(298)

文章存档

2022年(96)

2021年(201)

2019年(1)

我的朋友

分类: Java

2021-06-02 11:01:06


1. pom添加依赖

	

点击(此处)折叠或打开

  1. <!--redis-->
  2.  <dependency>
  3.     <groupId>org.springframework.boot</groupId>
  4.     <artifactId>spring-boot-starter-data-redis</artifactId>
  5.  </dependency>

2. application.properties 配置文件

	

点击(此处)折叠或打开

  1. #===========Redis配置===========
  2. # Redis数据库索引(默认为0)
  3. spring.redis.database=0
  4. # Redis服务器地址
  5. spring.redis.host=127.0.0.1
  6. # Redis服务器连接端口
  7. spring.redis.port=6379
  8. # Redis服务器连接密码
  9. spring.redis.password=root
  10. # 连接池最大连接数(使用负值表示没有限制)
  11. spring.redis.pool.max-active=200
  12. # 连接池最大阻塞等待时间(使用负值表示没有限制)
  13. spring.redis.pool.max-wait=-1
  14. # 连接池中的最大空闲连接
  15. spring.redis.pool.max-idle=10
  16. # 连接池中的最小空闲连接
  17. spring.redis.pool.min-idle=0
  18. # 连接超时时间(毫秒)
  19. spring.redis.timeout=2000ms
  20. spring.redis.jedis.pool.max-wait=-1ms
  21. #===========Redis配置===========

3. RedisConfig.java 配置类

	

点击(此处)折叠或打开

  1. package org.fh.config;
  2.  
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.data.redis.connection.RedisConnectionFactory;
  6. import org.springframework.data.redis.core.RedisTemplate;
  7. import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
  8. import org.springframework.data.redis.serializer.StringRedisSerializer;
  9.  
  10. import com.fasterxml.jackson.databind.ObjectMapper;
  11. import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
  12. import com.fasterxml.jackson.annotation.PropertyAccessor;
  13. import com.fasterxml.jackson.annotation.JsonAutoDetect;
  14.  
  15. /**
  16.  * 说明:Redis
  17.  * from:www fhadmin org
  18.  */
  19. @Configuration
  20. public class RedisConfig {
  21.  
  22.     @Bean
  23.     @SuppressWarnings("all")
  24.     public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
  25.         RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
  26.         template.setConnectionFactory(factory);
  27.         Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
  28.         ObjectMapper om = new ObjectMapper();
  29.         om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
  30.         om.activateDefaultTyping(LaissezFaireSubTypeValidator.instance , ObjectMapper.DefaultTyping.NON_FINAL);
  31.         jackson2JsonRedisSerializer.setObjectMapper(om);
  32.         StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
  33.         // key采用String的序列化方式
  34.         template.setKeySerializer(stringRedisSerializer);
  35.         // hash的key也采用String的序列化方式
  36.         template.setHashKeySerializer(stringRedisSerializer);
  37.         // value序列化方式采用jackson
  38.         template.setValueSerializer(jackson2JsonRedisSerializer);
  39.         // hash的value序列化方式采用jackson
  40.         template.setHashValueSerializer(jackson2JsonRedisSerializer);
  41.         template.afterPropertiesSet();
  42.         return template;
  43.     }
  44.  
  45. }

4. 调用redis

	

点击(此处)折叠或打开

  1. @Autowired
  2.    private RedisTemplate<String, Object> redisTemplate;
  3.  
  4.     /**
  5.      * 普通缓存获取
  6.      * @param key 键
  7.      * @return 值
  8.      */
  9.     public Object get(String key) {
  10.         return key == null ? null : redisTemplate.opsForValue().get(key);
  11.     }
  12.  
  13.     /**
  14.      * 普通缓存放入
  15.      * @param key 键
  16.      * @param value 值
  17.      * @return true成功 false失败
  18.      */
  19.     public boolean set(String key, Object value) {
  20.         try {
  21.             redisTemplate.opsForValue().set(key, value);
  22.             return true;
  23.         } catch (Exception e) {
  24.             //e.printStackTrace();
  25.             return false;
  26.         }
  27.     }

 



阅读(12770) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~