第六,在需要使用緩存服務的模塊中,編寫業務代碼,完成緩存操作。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. 創建配置對象
![](http://static.javashuo.com/static/loading.gif)
- 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