美文网首页
007.Spring Boot Redis Cache Samp

007.Spring Boot Redis Cache Samp

作者: airkisser | 来源:发表于2017-03-14 23:51 被阅读0次

一、目录结构

目录结构

二、pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.airkisser</groupId>
    <artifactId>spring-boot-sample-redis-cache</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>spring-boot-sample-redis</name>
    <description>Spring boot redis cache sample</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <finalName>${artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

三、application.properties

logging.level.com.airkisser=debug

#----------------------------------
#  Redis
#----------------------------------
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.password=
spring.redis.database=0
spring.redis.pool.max-active=8
spring.redis.pool.max-idle=8
spring.redis.pool.max-wait=-1
spring.redis.pool.min-idle=0
spring.redis.timeout=0

四、java

Application.java

package com.airkisser;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(Application.class);
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

RedisConfig .java

package com.airkisser.config;

import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Configuration;

@Configuration
// 开启缓存支持,
// Spring boot自动注入redisConnectionFactory,redisTemplate,cacheManager
@EnableCaching
public class RedisConfig {

}

UserController.java

package com.airkisser.web;

import com.airkisser.entity.User;
import com.airkisser.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
    private static final Logger LOGGER = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/user", method = RequestMethod.GET)
    public User getUser(Long id, String username, String password) {
        LOGGER.debug("Get User Request...");
        return userService.getUser(id, username, password);
    }

}

UserService .java

package com.airkisser.service;

import com.airkisser.entity.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
public class UserService {

    private static final Logger LOGGER = LoggerFactory.getLogger(UserService.class);

    @Cacheable(value = "userCache")
    public User getUser(Long id, String username, String password) {
        LOGGER.debug("No cache,find from db...id=" + id);
        return new User(id, username, password);
    }

}

User.java

package com.airkisser.entity;

import java.io.Serializable;

public class User implements Serializable{
    private static final long serialVersionUID = 4714012106558852332L;

    private Long id;
    private String username;
    private String password;

    public User() {
    }

    public User(Long id, String username, String password) {
        this.id = id;
        this.username = username;
        this.password = password;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

五、启动Redis

Redis启动界面

六、结果

请求1:http://localhost:8090/user?id=1&username=zhangsan&password=123456
控制台LOG:

2017-03-14 23:46:01.725 DEBUG 5144 --- [apr-8090-exec-9] com.airkisser.web.UserController         : Get User Request...
2017-03-14 23:46:01.730 DEBUG 5144 --- [apr-8090-exec-9] com.airkisser.service.UserService        : No cache,find from db...id=1

Redis数据:


Redis数据

重复请求1:http://localhost:8090/user?id=1&username=zhangsan&password=123456
没有执行UserService.getUser()方法,控制台LOG:
2017-03-14 23:46:57.817 DEBUG 5144 --- [apr-8090-exec-1] com.airkisser.web.UserController : Get User Request...

相关文章

网友评论

      本文标题:007.Spring Boot Redis Cache Samp

      本文链接:https://www.haomeiwen.com/subject/vwpknttx.html