第六,在需要使用缓存服务的模块中,编写业务代码,完成缓存操作。1. 加入缓存jar包依赖配置
- <?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">
- <parent>
- <artifactId>web</artifactId>
- <groupId>com.aitongyi.web</groupId>
- <version>1.0-SNAPSHOT</version>
- </parent>
- <modelVersion>4.0.0</modelVersion>
-
- <artifactId>cache</artifactId>
-
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- </properties>
-
- <dependencies>
- <!-- CacheService缓存服务中需要用到的对象依赖 -->
- <dependency>
- <groupId>com.aitongyi.web</groupId>
- <artifactId>bean</artifactId>
- <version>${project.version}</version>
- </dependency>
- <!-- Redis依赖 -->
- <dependency>
- <groupId>redis.clients</groupId>
- <artifactId>jedis</artifactId>
- <version>2.7.2</version>
- </dependency>
- </dependencies>
- </project>
2. 创建配置对象

- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.ComponentScan;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.context.annotation.EnableAspectJAutoProxy;
-
-
- import redis.clients.jedis.JedisPool;
- import redis.clients.jedis.JedisPoolConfig;
-
- @Configuration
- @EnableAspectJAutoProxy
- @ComponentScan(basePackages = {"com.aitongyi.web.cache"})
- public class CacheConfig {
- /** redis缓存服务器地址 */
- @Value("${redis.host}")
- private String host;
- /** redis缓存服务器端口 */
- @Value("${redis.port}")
- private Integer port;
- /** redis缓存服务器连接超时时间 */
- @Value("${redis.timeout}")
- private Integer timeout;
-
- @Bean(name = "jedisPool")
- public JedisPool jedispool() {
- JedisPoolConfig config = new JedisPoolConfig();
- config.setMaxWaitMillis(30000); // 最大等待时间
- config.setMaxTotal(32); // 最大连接数
- config.setMinIdle(6); // 允许最小的空闲连接数
- config.setTestOnBorrow(false); // 申请到连接时是否效验连接是否有效,对性能有影响,建议关闭
- config.setTestOnReturn(false); // 使用完连接放回连接池时是否效验连接是否有效,对性能有影响,建议关闭
- config.setTestWhileIdle(true); // 申请到连接时,如果空闲时间大于TimeBetweenEvictionRunsMillis时间,效验连接是否有效,建议开启,对性能有效不大
- config.setTimeBetweenEvictionRunsMillis(30000); //TestWhileIdle的判断依据
- return new JedisPool(config, host, port, timeout);
- }
- }
3. 创建缓存服务
- /**
- * 缓存服务
- * Created by admin on 16/8/18.
- */
- @Component
- public class CacheService {
- @Autowired
- private JedisPool jedisPool;
-
- /**
- * 设置缓存对象
- * @param key
- * @param value
- */
- public void set(String key,String value){
- Jedis jedis = null;
- try{
- jedis = jedisPool.getResource();
- jedis.set(key, value);
- }finally{
- if(jedis != null){
- jedis.close();
- }
- }
- }
-
- /**
- * 获取缓存对象
- * @param key
- * @return
- */
- public String get(String key){
- Jedis jedis = null;
- try{
- jedis = jedisPool.getResource();
- return jedis.get(key);
- }finally{
- if(jedis != null){
- jedis.close();
- }
- }
- }
-
- /**
- * 删除缓存对象
- * @param key
- */
- public void del(String key){
- Jedis jedis = null;
- try{
- jedis = jedisPool.getResource();
- jedis.del(key);
- }finally{
- if(jedis != null){
- jedis.close();
- }
- }
- }
-
- }
4. 对缓存服务的依赖管理
在【back】服务中的pom.xml中加入【cache】模块的依赖
4. 对缓存服务的依赖管理
在【back】服务中的pom.xml中加入【cache】模块的依赖
- <dependencies>
- <dependency>
- <groupId>com.aitongyi.web</groupId>
- <artifactId>dao</artifactId>
- <version>${project.version}</version>
- </dependency>
- <dependency>
- <groupId>com.aitongyi.web</groupId>
- <artifactId>bean</artifactId>
- <version>${project.version}</version>
- </dependency>
- <dependency>
- <groupId>com.aitongyi.web</groupId>
-
- <dependencies>
- <dependency>
- <groupId>com.aitongyi.web</groupId>
- <artifactId>dao</artifactId>
- <version>${project.version}</version>
- </dependency>
- <dependency>
- <groupId>com.aitongyi.web</groupId>
- <artifactId>bean</artifactId>
- <version>${project.version}</version>
- </dependency>
- <dependency>
- <groupId>com.aitongyi.web</groupId>
- <artifactId>service</artifactId>
- copy