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
相关文章
相关标签/搜索