Redis緩存配置

對於緩存管理,其實就是四個步驟

  • 第一,在【cache】組件中的pom.xml中加入redis的第三方java客戶端jedis的jar包
  • 第二,通過編寫一個緩存配置類,來管理連接池
  • 第三,編寫緩存服務,提供緩存操作接口
  • 第四,在需要使用緩存服務的【back】服務中,加入項目依賴,其他任何服務需要使用緩存服務,都可以配置類似的依賴
  • 第五,在【back】服務啓動配置中加入緩存配置類,以保障緩存服務能再服務啓動的時候初始化

  • 第六,在需要使用緩存服務的模塊中,編寫業務代碼,完成緩存操作。

    1. 加入緩存jar包依賴配置

    [sql]  view plain  copy
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <project xmlns="http://maven.apache.org/POM/4.0.0"  
    3.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    4.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
    5.     <parent>  
    6.         <artifactId>web</artifactId>  
    7.         <groupId>com.aitongyi.web</groupId>  
    8.         <version>1.0-SNAPSHOT</version>  
    9.     </parent>  
    10.     <modelVersion>4.0.0</modelVersion>  
    11.   
    12.     <artifactId>cache</artifactId>  
    13.   
    14.     <properties>  
    15.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
    16.     </properties>  
    17.   
    18.     <dependencies>  
    19.         <!-- CacheService緩存服務中需要用到的對象依賴    -->  
    20.         <dependency>  
    21.             <groupId>com.aitongyi.web</groupId>  
    22.             <artifactId>bean</artifactId>  
    23.             <version>${project.version}</version>  
    24.         </dependency>  
    25.         <!-- Redis依賴    -->  
    26.         <dependency>  
    27.             <groupId>redis.clients</groupId>  
    28.             <artifactId>jedis</artifactId>  
    29.             <version>2.7.2</version>  
    30.         </dependency>  
    31.     </dependencies>  
    32. </project>  

      

    2. 創建配置對象

    [sql]  view plain  copy
    1. import org.springframework.beans.factory.annotation.Value;  
    2. import org.springframework.context.annotation.Bean;  
    3. import org.springframework.context.annotation.ComponentScan;  
    4. import org.springframework.context.annotation.Configuration;  
    5. import org.springframework.context.annotation.EnableAspectJAutoProxy;  
    6.   
    7.   
    8. import redis.clients.jedis.JedisPool;  
    9. import redis.clients.jedis.JedisPoolConfig;  
    10.   
    11. @Configuration  
    12. @EnableAspectJAutoProxy  
    13. @ComponentScan(basePackages = {"com.aitongyi.web.cache"})  
    14. public class CacheConfig {  
    15.     /** redis緩存服務器地址    */  
    16.     @Value("${redis.host}")  
    17.     private String host;  
    18.     /** redis緩存服務器端口    */  
    19.     @Value("${redis.port}")  
    20.     private Integer port;  
    21.     /** redis緩存服務器連接超時時間    */  
    22.     @Value("${redis.timeout}")  
    23.     private Integer timeout;  
    24.   
    25.     @Bean(name = "jedisPool")  
    26.     public JedisPool jedispool() {  
    27.         JedisPoolConfig config = new JedisPoolConfig();  
    28.         config.setMaxWaitMillis(30000); //  最大等待時間  
    29.         config.setMaxTotal(32);         //  最大連接數  
    30.         config.setMinIdle(6);           //  允許最小的空閒連接數  
    31.         config.setTestOnBorrow(false);  //  申請到連接時是否效驗連接是否有效,對性能有影響,建議關閉  
    32.         config.setTestOnReturn(false);  //  使用完連接放回連接池時是否效驗連接是否有效,對性能有影響,建議關閉  
    33.         config.setTestWhileIdle(true);  //  申請到連接時,如果空閒時間大於TimeBetweenEvictionRunsMillis時間,效驗連接是否有效,建議開啓,對性能有效不大  
    34.         config.setTimeBetweenEvictionRunsMillis(30000); //TestWhileIdle的判斷依據  
    35.         return new JedisPool(config, host, port, timeout);  
    36.     }  
    37. }  


    3. 創建緩存服務

    [sql]  view plain  copy
    1. /**  
    2.  * 緩存服務  
    3.  * Created by admin on 16/8/18.  
    4.  */  
    5. @Component  
    6. public class CacheService {  
    7.     @Autowired  
    8.     private JedisPool jedisPool;  
    9.   
    10.     /**  
    11.      * 設置緩存對象  
    12.      * @param key  
    13.      * @param value  
    14.      */  
    15.     public void set(String key,String value){  
    16.         Jedis jedis = null;  
    17.         try{  
    18.             jedis = jedisPool.getResource();  
    19.             jedis.set(key, value);  
    20.         }finally{  
    21.             if(jedis != null){  
    22.                 jedis.close();  
    23.             }  
    24.         }  
    25.     }  
    26.   
    27.     /**  
    28.      * 獲取緩存對象  
    29.      * @param key  
    30.      * @return  
    31.      */  
    32.     public String get(String key){  
    33.         Jedis jedis = null;  
    34.         try{  
    35.             jedis = jedisPool.getResource();  
    36.             return jedis.get(key);  
    37.         }finally{  
    38.             if(jedis != null){  
    39.                 jedis.close();  
    40.             }  
    41.         }  
    42.     }  
    43.   
    44.     /**  
    45.      * 刪除緩存對象  
    46.      * @param key  
    47.      */  
    48.     public void del(String key){  
    49.         Jedis jedis = null;  
    50.         try{  
    51.             jedis = jedisPool.getResource();  
    52.             jedis.del(key);  
    53.         }finally{  
    54.             if(jedis != null){  
    55.                 jedis.close();  
    56.             }  
    57.         }  
    58.     }  
    59.   
    60. }  

    4. 對緩存服務的依賴管理

    在【back】服務中的pom.xml中加入【cache】模塊的依賴

    [sql]  view plain  copy
    1.   
    2. }  

    4. 對緩存服務的依賴管理

    在【back】服務中的pom.xml中加入【cache】模塊的依賴

    [sql]  view plain  copy
    1. <dependencies>  
    2.         <dependency>  
    3.             <groupId>com.aitongyi.web</groupId>  
    4.             <artifactId>dao</artifactId>  
    5.             <version>${project.version}</version>  
    6.         </dependency>  
    7.         <dependency>  
    8.             <groupId>com.aitongyi.web</groupId>  
    9.             <artifactId>bean</artifactId>  
    10.             <version>${project.version}</version>  
    11.         </dependency>  
    12.         <dependency>  
    13.             <groupId>com.aitongyi.web</groupId>  
    14. [sql]  view plain  copy
      1. <dependencies>  
      2.         <dependency>  
      3.             <groupId>com.aitongyi.web</groupId>  
      4.             <artifactId>dao</artifactId>  
      5.             <version>${project.version}</version>  
      6.         </dependency>  
      7.         <dependency>  
      8.             <groupId>com.aitongyi.web</groupId>  
      9.             <artifactId>bean</artifactId>  
      10.             <version>${project.version}</version>  
      11.         </dependency>  
      12.         <dependency>  
      13.             <groupId>com.aitongyi.web</groupId>  
      14.             <artifactId>service</artifactId>  
      15.  copy
相關文章
相關標籤/搜索