zoukankan      html  css  js  c++  java
  • 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已完成。

  • 相关阅读:
    《Linux命令行与shell脚本编程大全 第3版》Linux命令行---57
    《Linux命令行与shell脚本编程大全 第3版》Linux命令行---56
    《Linux命令行与shell脚本编程大全 第3版》Linux命令行---55
    《Linux命令行与shell脚本编程大全 第3版》Linux命令行---54
    《Linux命令行与shell脚本编程大全 第3版》Linux命令行---53
    《Linux命令行与shell脚本编程大全 第3版》Linux命令行---51
    activity-启动动画的设定(下面弹出出现,弹入下面消失)
    TextView-显示自己添加的字体样式
    visibility-控件的显示跟隐藏设置
    RelativeLayout-属性大全
  • 原文地址:https://www.cnblogs.com/coder-fang/p/7791502.html
Copyright © 2011-2022 走看看