redis分布式缓存

1.Springcache+redis 缓存

controller层

@RestController
 2 @RequestMapping("/test")
 3 public class CacheController {
 4 
 5     @Resource
 6     private CacheService cacheService;
 7 
 8    @RequestMapping("/")
 9    public Provinces detail(@RequestParam String provinceId){
10        return cacheService.detail(provinceId);
11    }
12 }
View Code

  service层

@Service
public class CacheService {

    @Autowired
    private CacheDao cacheDao;

   // @Cacheable(value = "province", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#provinceid")
    @Cacheable( value = "province",key="#provinceId")
    public Provinces detail(String provinceId) {
        Provinces provinces =null;
        provinces =  cacheDao.detail(provinceId);
        System.out.println("///"+provinces);
        return provinces;
    }
}
View Code

dao层

@Repository
public interface CacheDao extends Serializable{
    Provinces detail(String provinceid);
}
View Code
<?xml version="1.0" encoding="UTF-8" ?>   
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.example.cache.dao.CacheDao">
    <select id="detail" resultType="com.example.cache.entity.Provinces">
        select T3.province province,T3.provinceid provinceid, T3.id id
        from g_provinces T3
        where provinceid = #{provinceid}
    </select>
</mapper>
View Code

entity层

@ApiModel(value = "省份信息表")
@Data
public class Provinces implements Serializable {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "")
    private Integer id;

    @ApiModelProperty(value = "")
    private String province;
    @ApiModelProperty(value = "")
    private String provinceid;
}
View Code

Config层

@Configuration
@EnableCaching
public class CacheConfig  extends JCacheConfigurerSupport{

    @Bean
    public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory){
        return RedisCacheManager
                .builder(redisConnectionFactory)
                .cacheDefaults(RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofMinutes(1)))
                .transactionAware()
                .build();
    }
}
View Code

application.yml

server:
    port: 8087
spring:
    redis:
        host: 127.0.0.1
        port: 6379
    datasource:
        url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true&zeroDateTimeBehavior=convertToNull
        username: root
        password: 123456
        driverClassName : com.mysql.jdbc.Driver
View Code

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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>spring_cache</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring_cache</name>
    <description>Demo project for Spring Boot</description>


    <dependencies>
         
        <!--web依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.1.6.RELEASE</version>
        </dependency>

        <!--测试依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <!--数据库配置-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <!-- redis连接 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

        <!--数据库配置-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.28</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.0</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.30</version>
        </dependency>
        <!-- Spring Fox -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>

        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>bootstrap</artifactId>
            <version>3.3.5</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.20</version>
        </dependency>
    </dependencies>

    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
View Code

sql文件

CREATE TABLE `g_provinces` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `provinceid` varchar(20) NOT NULL,
  `province` varchar(50) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=36 DEFAULT CHARSET=utf8 COMMENT='省份信息表';

/*Data for the table `g_provinces` */

insert  into `g_provinces`(`id`,`provinceid`,`province`) values (1,'110000','北京市'),(2,'120000','天津市'),(3,'130000','河北省'),(4,'140000','山西省'),(5,'150000','内蒙古自治区'),(6,'210000','辽宁省'),(7,'220000','吉林省'),(8,'230000','黑龙江省'),(9,'310000','上海市'),(10,'320000','江苏省2'),(11,'330000','浙江省'),(12,'340000','安徽省'),(13,'350000','福建省'),(14,'360000','江西省'),(15,'370000','山东省'),(16,'410000','河南省'),(17,'420000','湖北省'),(18,'430000','湖南省'),(19,'440000','广东省'),(20,'450000','广西壮族自治区'),(21,'460000','海南省'),(22,'500000','重庆市'),(23,'510000','四川省'),(24,'520000','贵州省'),(25,'530000','云南省'),(26,'540000','西藏自治区'),(27,'610000','陕西省'),(28,'620000','甘肃省'),(29,'630000','青海省'),(30,'640000','宁夏回族自治区'),(31,'650000','新疆维吾尔自治区'),(32,'710000','台湾省'),(33,'810000','香港特别行政区'),(34,'820000','澳门特别行政区'),(35,'110001','托尔斯泰');
View Code

 

posted @ 2019-08-15 21:57  馥郁  阅读(89)  评论(0)    收藏  举报