• springboot +redis配置


    springboot +redis配置

    pom依赖

    1 <dependency>
    2     <groupId>org.springframework.boot</groupId>
    3     <artifactId>spring-boot-starter-data-redis</artifactId>
    4 </dependency>
    spring boot data redis 的pom依赖

    Spring boot yml配置redis的参数

     1 spring:
     2 # REDIS (RedisProperties)
     3 # Redis数据库索引(默认为0)
     4   redis:
     5       database: 0 
     6       timeout: 0 
     7 # Redis服务器地址
     8       host: 127.0.0.1
     9 # Redis服务器连接端口
    10       port: 6379  
    11 # Redis服务器连接密码(默认为空)
    12       password:  
    13 # 连接池最大连接数(使用负值表示没有限制)
    14       pool: 
    15         max-active: 8  
    16 # 连接池最大阻塞等待时间(使用负值表示没有限制)
    17         max-wait: -1  
    18 # 连接池中的最大空闲连接
    19         max-idle: 8  
    20 # 连接池中的最小空闲连接
    21         min-idle: 0  
    22       cluster:
    23         max-redirects: 10
    24         nodes: 127.0.0.1:6080
    25 # 连接超时时间(毫秒)
    spring boot data redis的配置文件(yml)

    Spring boot config redis的模板

     1 @Configuration
     2 @EnableCaching
     3 public class RedisCacheConfig {
     4     @Bean
     5     public CacheManager cacheManager(RedisTemplate<?, ?> redisTemplate) {
     6         CacheManager cacheManager = new RedisCacheManager(redisTemplate);
     7         return cacheManager;
     8     }
     9 
    10     @Bean
    11     public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
    12         RedisTemplate<String, String> redisTemplate = new RedisTemplate<String, String>();
    13         redisTemplate.setConnectionFactory(factory);
    14         // key序列化方式;(不然会出现乱码;),但是如果方法上有Long等非String类型的话,会报类型转换错误;
    15         // 所以在没有自己定义key生成策略的时候,以下这个代码建议不要这么写,可以不配置或者自己实现ObjectRedisSerializer
    16         // 或者JdkSerializationRedisSerializer序列化方式;
    17         RedisSerializer<String> redisSerializer = new StringRedisSerializer();// Long类型不可以会出现异常信息;
    18         redisTemplate.setKeySerializer(redisSerializer);
    19         redisTemplate.setHashKeySerializer(redisSerializer);
    20         return redisTemplate;
    21     }
    22 
    23 }
    spring boot config redis的模板

    spring boot 的redis 工具类

      1 @SuppressWarnings("unchecked")
      2 @Component
      3 public class RedisUtil {
      4     @SuppressWarnings("rawtypes")
      5     @Autowired
      6     private RedisTemplate redisTemplate;
      7 
      8     /**
      9      * 批量删除对应的value
     10      * 
     11      * @param keys
     12      */
     13     public void remove(final String... keys) {
     14         for (String key : keys) {
     15             remove(key);
     16         }
     17     }
     18 
     19     /**
     20      * 批量删除key
     21      * 
     22      * @param pattern
     23      */
     24     public void removePattern(final String pattern) {
     25         Set<Serializable> keys = redisTemplate.keys(pattern);
     26         if (keys.size() > 0)
     27             redisTemplate.delete(keys);
     28     }
     29 
     30     /**
     31      * 删除对应的value
     32      * 
     33      * @param key
     34      */
     35     public void remove(final String key) {
     36         if (exists(key)) {
     37             redisTemplate.delete(key);
     38         }
     39     }
     40 
     41     /**
     42      * 判断缓存中是否有对应的value
     43      * 
     44      * @param key
     45      * @return
     46      */
     47     public boolean exists(final String key) {
     48         return redisTemplate.hasKey(key);
     49     }
     50 
     51     /**
     52      * 读取缓存
     53      * 
     54      * @param key
     55      * @return
     56      */
     57     public String get(final String key) {
     58         Object result = null;
     59         redisTemplate.setValueSerializer(new StringRedisSerializer());
     60         ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
     61         result = operations.get(key);
     62         if (result == null) {
     63             return null;
     64         }
     65         return result.toString();
     66     }
     67 
     68     /**
     69      * 写入缓存
     70      * 
     71      * @param key
     72      * @param value
     73      * @return
     74      */
     75     public boolean set(final String key, Object value) {
     76         boolean result = false;
     77         try {
     78             ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
     79             operations.set(key, value);
     80             result = true;
     81         } catch (Exception e) {
     82             e.printStackTrace();
     83         }
     84         return result;
     85     }
     86 
     87     /**
     88      * 写入缓存
     89      * 
     90      * @param key
     91      * @param value
     92      * @return
     93      */
     94     public boolean set(final String key, Object value, Long expireTime) {
     95         boolean result = false;
     96         try {
     97             ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
     98             operations.set(key, value);
     99             redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
    100             result = true;
    101         } catch (Exception e) {
    102             e.printStackTrace();
    103         }
    104         return result;
    105     }
    106 
    107     public boolean hmset(String key, Map<String, String> value) {
    108         boolean result = false;
    109         try {
    110             redisTemplate.opsForHash().putAll(key, value);
    111             result = true;
    112         } catch (Exception e) {
    113             e.printStackTrace();
    114         }
    115         return result;
    116     }
    117 
    118     public Map<String, String> hmget(String key) {
    119         Map<String, String> result = null;
    120         try {
    121             result = redisTemplate.opsForHash().entries(key);
    122         } catch (Exception e) {
    123             e.printStackTrace();
    124         }
    125         return result;
    126     }
    127 }
    spring boot 的redis工具类

    基本测试类如下:

     1 @RunWith(SpringRunner.class)
     2 @SpringBootTest
     3 public class RedisUtilTest {
     4     @Autowired
     5     private RedisUtil redisUtil;
     6 
     7     @Test
     8     public void testSet() {
     9         String key = "key";
    10         System.out.println(redisUtil.get(key));
    11         String value = "sdd";
    12         redisUtil.set(key, value);
    13         System.out.println(redisUtil.get(key));
    14     }
    15 }
    redis基本测试类

    现在这样就基本能够使用redis了

  • 相关阅读:
    LinqToXML~读XML文件
    C#~使用FileSystemWatcher来监视文件系统的变化
    HDU4144:Bacon's Cipher
    国产手机的路还很长
    同一个存储过程中,不能多次select into 到同一张表的问题
    IT行业为什么没有进度
    IOT(Index Organized Table)
    模版——容器,迭代器
    shell split分析日志文件
    VMware vSphere 服务器虚拟化之十七 桌面虚拟化之安装View链接服务器
  • 原文地址:https://www.cnblogs.com/xiaocao1434/p/8644307.html
Copyright © 2020-2023  润新知