e2

滴滴侠,fai抖

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

首先创建一个springboot项目。

添加pom配置:

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

pom配置文件内容为:

  1.  
    <?xml version="1.0" encoding="UTF-8"?>
  2.  
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3.  
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4.  
    <modelVersion>4.0.0</modelVersion>
  5.  
     
  6.  
    <groupId>com.pangjh</groupId>
  7.  
    <artifactId>springboot-redis</artifactId>
  8.  
    <version>0.0.1-SNAPSHOT</version>
  9.  
    <packaging>jar</packaging>
  10.  
     
  11.  
    <name>springboot-redis</name>
  12.  
    <description>redis缓存</description>
  13.  
     
  14.  
    <parent>
  15.  
    <groupId>org.springframework.boot</groupId>
  16.  
    <artifactId>spring-boot-starter-parent</artifactId>
  17.  
    <version>2.0.0.RELEASE</version>
  18.  
    <relativePath/> <!-- lookup parent from repository -->
  19.  
    </parent>
  20.  
     
  21.  
    <properties>
  22.  
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  23.  
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  24.  
    <java.version>1.8</java.version>
  25.  
    </properties>
  26.  
     
  27.  
    <dependencies>
  28.  
    <dependency>
  29.  
    <groupId>org.springframework.boot</groupId>
  30.  
    <artifactId>spring-boot-starter-web</artifactId>
  31.  
    </dependency>
  32.  
     
  33.  
    <dependency>
  34.  
    <groupId>org.springframework.boot</groupId>
  35.  
    <artifactId>spring-boot-starter-test</artifactId>
  36.  
    <scope>test</scope>
  37.  
    </dependency>
  38.  
    <dependency>
  39.  
    <groupId>org.springframework.boot</groupId>
  40.  
    <artifactId>spring-boot-starter-data-redis</artifactId>
  41.  
    </dependency>
  42.  
    </dependencies>
  43.  
     
  44.  
    <build>
  45.  
    <plugins>
  46.  
    <plugin>
  47.  
    <groupId>org.springframework.boot</groupId>
  48.  
    <artifactId>spring-boot-maven-plugin</artifactId>
  49.  
    </plugin>
  50.  
    </plugins>
  51.  
    </build>
  52.  
     
  53.  
     
  54.  
    </project>

创建redis配置文件RedisConfig.java:

内容为:

  1.  
    package com.pangjh.conf;
  2.  
     
  3.  
    import java.util.concurrent.CountDownLatch;
  4.  
     
  5.  
    import org.springframework.beans.factory.annotation.Autowired;
  6.  
    import org.springframework.cache.annotation.CachingConfigurerSupport;
  7.  
    import org.springframework.cache.annotation.EnableCaching;
  8.  
    import org.springframework.context.annotation.Bean;
  9.  
    import org.springframework.context.annotation.Configuration;
  10.  
    import org.springframework.data.redis.connection.RedisConnectionFactory;
  11.  
    import org.springframework.data.redis.core.StringRedisTemplate;
  12.  
    import org.springframework.data.redis.listener.PatternTopic;
  13.  
    import org.springframework.data.redis.listener.RedisMessageListenerContainer;
  14.  
    import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
  15.  
     
  16.  
     
  17.  
    /**
  18.  
    * redis配置
  19.  
    * @author pangjianhui
  20.  
    *
  21.  
    */
  22.  
    @Configuration
  23.  
    @EnableCaching
  24.  
    public class RedisConfig extends CachingConfigurerSupport {
  25.  
     
  26.  
    @Bean
  27.  
    RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
  28.  
    MessageListenerAdapter listenerAdapter) {
  29.  
     
  30.  
    RedisMessageListenerContainer container = new RedisMessageListenerContainer();
  31.  
    container.setConnectionFactory(connectionFactory);
  32.  
    container.addMessageListener(listenerAdapter, new PatternTopic("chat"));
  33.  
     
  34.  
    return container;
  35.  
    }
  36.  
     
  37.  
    @Bean
  38.  
    MessageListenerAdapter listenerAdapter(Receiver receiver) {
  39.  
    return new MessageListenerAdapter(receiver, "receiveMessage");
  40.  
    }
  41.  
     
  42.  
    @Bean
  43.  
    Receiver receiver(CountDownLatch latch) {
  44.  
    return new Receiver(latch);
  45.  
    }
  46.  
     
  47.  
    @Bean
  48.  
    CountDownLatch latch() {
  49.  
    return new CountDownLatch(1);
  50.  
    }
  51.  
     
  52.  
    @Bean
  53.  
    StringRedisTemplate template(RedisConnectionFactory connectionFactory) {
  54.  
    return new StringRedisTemplate(connectionFactory);
  55.  
    }
  56.  
     
  57.  
    public class Receiver {
  58.  
     
  59.  
     
  60.  
    private CountDownLatch latch;
  61.  
     
  62.  
    @Autowired
  63.  
    public Receiver(CountDownLatch latch) {
  64.  
    this.latch = latch;
  65.  
    }
  66.  
     
  67.  
    public void receiveMessage(String message) {
  68.  
    latch.countDown();
  69.  
    }
  70.  
    }
  71.  
     
  72.  
     
  73.  
    }

redis配置:

  1.  
    # REDIS
  2.  
    # Redis数据库索引(默认为0)
  3.  
    spring.redis.database=0
  4.  
    # Redis服务器地址 (默认为127.0.0.1)
  5.  
    spring.redis.host=127.0.0.1
  6.  
    # Redis服务器连接端口 (默认为6379)
  7.  
    spring.redis.port=6379
  8.  
    # Redis服务器连接密码(默认为空)
  9.  
    spring.redis.password=
  10.  
    # 连接超时时间(毫秒)
  11.  
    spring.redis.timeout=2000

以上操作基本完成了springboot2.0与redis的整合,下面我们测试使用:

 

 

  1. 通过代码方式使用redis

    编写controller:

  1.  
    package com.pangjh.controller;
  2.  
     
  3.  
    import org.springframework.beans.factory.annotation.Autowired;
  4.  
    import org.springframework.data.redis.core.StringRedisTemplate;
  5.  
    import org.springframework.web.bind.annotation.RequestMapping;
  6.  
    import org.springframework.web.bind.annotation.RestController;
  7.  
     
  8.  
     
  9.  
    @RestController
  10.  
    public class PangjhController {
  11.  
     
  12.  
    @Autowired
  13.  
    private StringRedisTemplate template;
  14.  
     
  15.  
    @RequestMapping("/setValue")
  16.  
    public String setValue(){
  17.  
    if(!template.hasKey("shabao")){
  18.  
    template.opsForValue().append("shabao", "我是傻宝");
  19.  
    return "使用redis缓存保存数据成功";
  20.  
    }else{
  21.  
    template.delete("shabao");
  22.  
    return "key已存在";
  23.  
    }
  24.  
    }
  25.  
     
  26.  
    @RequestMapping("/getValue")
  27.  
    public String getValue(){
  28.  
     
  29.  
    if(!template.hasKey("shabao")){
  30.  
    return "key不存在,请先保存数据";
  31.  
    }else{
  32.  
    String shabao = template.opsForValue().get("shabao");//根据key获取缓存中的val
  33.  
    return "获取到缓存中的数据:shabao="+shabao;
  34.  
    }
  35.  
    }
  36.  
     
  37.  
    }

 

启动项目(注意这里启动项目需要确保redis服务的开启状态)

请求:http://localhost:8080/setValue

存储数据到redis成功!

请求:http://localhost:8080/getValue,查询数据

    

 

 

  1. 通过注解方式使用redis

    编写model:

  1.  
    package com.pangjh.model;
  2.  
     
  3.  
    import java.io.Serializable;
  4.  
     
  5.  
    public class User implements Serializable {
  6.  
     
  7.  
    private static final long serialVersionUID = 1L;
  8.  
     
  9.  
    private String id;
  10.  
     
  11.  
    private String name;
  12.  
     
  13.  
    private int age;
  14.  
     
  15.  
    public User() {
  16.  
    super();
  17.  
    }
  18.  
     
  19.  
    public User(String id, String name, int age) {
  20.  
    super();
  21.  
    this.id = id;
  22.  
    this.name = name;
  23.  
    this.age = age;
  24.  
    }
  25.  
     
  26.  
    public String getId() {
  27.  
    return id;
  28.  
    }
  29.  
     
  30.  
    public void setId(String id) {
  31.  
    this.id = id;
  32.  
    }
  33.  
     
  34.  
    public String getName() {
  35.  
    return name;
  36.  
    }
  37.  
     
  38.  
    public void setName(String name) {
  39.  
    this.name = name;
  40.  
    }
  41.  
     
  42.  
    public int getAge() {
  43.  
    return age;
  44.  
    }
  45.  
     
  46.  
    public void setAge(int age) {
  47.  
    this.age = age;
  48.  
    }
  49.  
     
  50.  
     
  51.  
     
  52.  
    }

编写接口:

  1.  
    package com.pangjh.service;
  2.  
     
  3.  
    import org.springframework.cache.annotation.CacheEvict;
  4.  
    import org.springframework.cache.annotation.Cacheable;
  5.  
     
  6.  
    import com.pangjh.model.User;
  7.  
     
  8.  
    public interface UserService {
  9.  
     
  10.  
    @Cacheable(value="users", key="'user_'+#id")
  11.  
    User getUser(String id);
  12.  
     
  13.  
    @CacheEvict(value="users", key="'user_'+#id",condition="#id!=1")
  14.  
    void deleteUser(String id);
  15.  
     
  16.  
    }

编写接口实现类:

  1.  
    package com.pangjh.serviceImpl;
  2.  
     
  3.  
    import org.springframework.stereotype.Service;
  4.  
     
  5.  
    import com.pangjh.model.User;
  6.  
    import com.pangjh.service.UserService;
  7.  
     
  8.  
    @Service
  9.  
    public class UserServiceImpl implements UserService {
  10.  
     
  11.  
    @Override
  12.  
    public User getUser(String id) {
  13.  
    System.out.println(id+"进入实现类获取数据!");
  14.  
    User user = new User();
  15.  
    user.setId(id);
  16.  
    user.setName("香菇");
  17.  
    user.setAge(18);
  18.  
    return user;
  19.  
    }
  20.  
     
  21.  
    @Override
  22.  
    public void deleteUser(String id) {
  23.  
    System.out.println(id+"进入实现类删除数据!");
  24.  
    }
  25.  
     
  26.  
    }

编写controller:

  1.  
    package com.pangjh.controller;
  2.  
     
  3.  
    import org.springframework.beans.factory.annotation.Autowired;
  4.  
    import org.springframework.web.bind.annotation.RequestMapping;
  5.  
    import org.springframework.web.bind.annotation.RestController;
  6.  
     
  7.  
    import com.pangjh.model.User;
  8.  
    import com.pangjh.service.UserService;
  9.  
     
  10.  
    @RestController
  11.  
    public class UserController {
  12.  
     
  13.  
    @Autowired
  14.  
    private UserService userService;
  15.  
     
  16.  
    @RequestMapping("/getUser")
  17.  
    public User getUser(){
  18.  
    User user = userService.getUser("xianggu");
  19.  
    return user;
  20.  
    }
  21.  
     
  22.  
    @RequestMapping("/deleteUser")
  23.  
    public String deleteUser(){
  24.  
    userService.deleteUser("xianggu");
  25.  
    return "执行了删除";
  26.  
    }
  27.  
     
  28.  
     
  29.  
     
  30.  
    }

项目结构如下:

 

启动项目

请求:http://localhost:8080/getUser

 

在缓存的有效时间内,重复请求,后台只会打印一次:

 

代码:https://github.com/pangjianhui1991/xianggu.git

 

 

posted on 2018-08-18 22:40  纯黑Se丶  阅读(883)  评论(0编辑  收藏  举报