Spring Boot 實踐2 --項目中使用 Redis


背景:基於實踐1中,我們使用Redis做為緩存。

 (轉載請注明來源:cnblogs coder-fang)

 

  1. POM中加入依賴:
         <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-redis</artifactId>            
            </dependency>

     

  2. 在application.properties中加入配置:
    spring.redis.host=127.0.0.1
    spring.redis.port=6379
    spring.redis.pool.max-idle=8
    spring.redis.pool.max-active=8

     

  3. 創建RedisConfig類,並注入RedisTemplate bean:
    package com.test.demo.config;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.redis.connection.RedisConnectionFactory;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.data.redis.core.StringRedisTemplate;
    
    @Configuration
    public class RedisConfig {
    
        @Bean
        public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory)
        {
            StringRedisTemplate stringRedisTemplate = new StringRedisTemplate(factory);
            return stringRedisTemplate;
        }
    }

     

  4. 實現redisRepostory通用功能:
    package com.test.demo.db.repo.nosql;
    
    import java.util.concurrent.TimeUnit;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.stereotype.Repository;
    
    @Repository
    public class RedisRepository {
    
        @Autowired
        RedisTemplate<String, String> redisTemplate;
        
        public void add(String key,String value) {
            redisTemplate.opsForValue().set(key, value);
        }
        public void add(String key,String value,Long time) {
            redisTemplate.opsForValue().set(key, value, time, TimeUnit.MINUTES);
        }
        
        public String get(String key) {
            return redisTemplate.opsForValue().get(key);
        }
        
        public void delete(String key) {
            redisTemplate.opsForValue().getOperations().delete(key);
        }
        
    }

     

  5. 創建單元測試並運行:
    package com.test.demo;
    
    import static org.junit.Assert.assertEquals;
    import static org.junit.Assert.assertNull;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    
    import com.test.demo.db.repo.nosql.RedisRepository;
    
    
    
    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class RedisTest {
    
        @Autowired
        RedisRepository redisRepo;
        
        
        @Test
        public void testRedis(){
            redisRepo.add("test", "123", 1L);
            String val = redisRepo.get("test");
            assertEquals(val, "123");
            System.out.println(val);
            val = redisRepo.get("321");
            System.out.println(val);
            assertNull(val);
        }
    }
    View Code

     

YES,項目中使用redis已完成。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM