spring-boot集成6:集成redis實現字典緩存功能

Why redis?

redis是基於內存的key-value系統,能夠用做緩存中間件或者消息中間件,spring-boot提供了方便的方式和redis集成。java

 

1.maven依賴

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

  

2.配置

spring:
  application:
    name: monocase-framework # 應用名稱
  jackson:
    date-format: yyyy-MM-dd HH:mm:ss # 日期格式
  datasource:
    druid: # 鏈接池別名
      url: jdbc:mysql://${MYSQL_HOST:192.168.1.200}:${MYSQL_PORT:3306}/zhya-monocase-framework?useUnicode=true&characterEncoding=utf8
      username: root
      password: root@123
      type: com.alibaba.druid.pool.DruidDataSource # 鏈接池類型
      driver-class-name: com.mysql.jdbc.Driver
      poolPreparedStatements: true
      maxOpenPreparedStatements: 100
      maxActive: 100
      maxWaitMillis: 5000
  redis:
    database: 1
    host: ${REDIS_HOST:192.168.1.200}
    port: ${REDIS_PORT:6379}
    jedis:
      pool:
        max-active: 100
        max-wait: 2000ms

3.實例

 

4.測試

第一次會進行數據庫查詢mysql

第二次則不會,由於redis中已經緩存了redis

相關文章
相關標籤/搜索