zoukankan      html  css  js  c++  java
  • spring boot 集成 Redis

    前提:你已经安装了Redis

    1、创建一个spring boot 工程

    2、pom 引入依赖:spring-boot-starter-data-redis

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

    3、在application.propertise 配置Redis相关参数

    ########################################################
    ###redis (redis)
    ########################################################
    #开发
    spring.redis.host=localhost
    spring.redis.port= 6379
    spring.redis.password=123456
    
    spring.session.store-type=redis
    spring.redis.database=1
    # 连接池中的最大空闲连接
    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= 6000

    4.创建Redis工厂

    @Configuration
    @EnableCaching
    public class RedisConfig extends CachingConfigurerSupport {
        @Value("${spring.redis.host}")
        private String host;
    
        @Value("${spring.redis.port}")
        private int port;
    
        @Value("${spring.redis.timeout}")
        private int timeout;
    
        @Value("${spring.redis.pool.max-idle}")
        private int maxIdle;
    
        @Value("${spring.redis.pool.max-wait}")
        private long maxWaitMillis;
    
        @Value("${spring.redis.password}")
        private String password;
    
        @Bean
        public JedisPool redisPoolFactory() {
          
            JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
            jedisPoolConfig.setMaxIdle(maxIdle);
            jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
            jedisPoolConfig.setEvictionPolicyClassName("org.apache.commons.pool2.impl.DefaultEvictionPolicy");
            JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, password);
    
            return jedisPool;
        }

    5、需要redis时,直接注入即可

    @Autowired
        private RedisTemplate redisTemplate;

    6. redisTemplate 使用,Redis可以操作五种数据类型(字符串(String)、哈希/散列/字典(Hash)、列表(List)、集合(Set)、有序集合(sorted set))

    6.1 字符串(String)

    保存

    redisTemplate.opsForValue().set("key1","value1");  
    redisTemplate.opsForValue().set("key2","value2"); 

    取值

    String result1=redisTemplate.opsForValue().get("key1").toString();  
    String result2=redisTemplate.opsForValue().get("key2").toString(); 

    6.2 哈希/散列/字典(Hash)

    保存

    Map<String,String> map=new HashMap<String,String>();  
    map.put("key1","1");  
    map.put("key2","2");  
    map.put("key3","3");  
    map.put("key4","4");  
    map.put("key5","5");  
    redisTemplate.opsForHash().putAll("hashMap",map);

    取值

    String value=(String)redisTemplate.opsForHash().get("hashMap","key1");          //结果是 1
    Map<String,String> resultMap= redisTemplate.opsForHash().entries("hashMap");   //结果是 {key1=1, key2=2, key5=5, key3=3, key4=4}
    List
    <String>reslutMapList=redisTemplate.opsForHash().values("hashMap"); //结果是 取得所有value值[1,2,3,4,5]
    Set
    <String>resultMapSet=redisTemplate.opsForHash().keys("hashMap"); //结果是 取得key值 [key1, key2, key5, key3, key4] 

    6.3 列表(List)

    保存

    List<Pays> requestJsonList = new Array<>();
     redisTemplate.opsForList().leftPush("key1", requestJsonList);  //将list集合放入Redis

    取值

    List<Pays> requestJsonList1 = (List<Pays>) redisTemplate.opsForList().leftPop("key1");

    7.总结操作

    redisTemplate.opsForValue();//操作字符串
    redisTemplate.opsForHash();//操作hash
    redisTemplate.opsForList();//操作list
    redisTemplate.opsForSet();//操作set
    redisTemplate.opsForZSet();//操作有序set
  • 相关阅读:
    Android ArrayAdapter使用
    Android 更改字体
    C# CRC16校验码 1.0
    获取当前主题颜色 Flutter
    C# 每个字节接受 处理串口数据 的方法
    C# CRC
    char* 与 string 互转
    typeof 子类获取父类
    flutter Row 垂直或水平放置多个widget
    flutter 容器 几种写法
  • 原文地址:https://www.cnblogs.com/XiDaPuBen/p/10309225.html
Copyright © 2011-2022 走看看