pom.xml;java
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"mysql
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">web
<modelVersion>4.0.0</modelVersion>redis
<groupId>com.testboot</groupId>spring
<artifactId>Bootest</artifactId>sql
<version>0.0.1-SNAPSHOT</version>數據庫
<parent>apache
<groupId>org.springframework.boot</groupId>緩存
<artifactId>spring-boot-starter-parent</artifactId>app
<version>1.5.7.RELEASE</version>
</parent>
<dependencies>
<!-- 開啓web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- thymeleaf模板引擎 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- 用JdbcTemplate與數據庫進行鏈接 -->
<!-- MYSQL -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- Spring Boot JDBC -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- 用JdbcTemplate與數據庫進行鏈接 -->
<!-- druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.29</version>
</dependency>
<!-- redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.yml資源文件:
logging:
level:
org.springframework: INFO
com.example: DEBUG
spring:
datasource:
url : jdbc:mysql://localhost:3306/mysqllocalhost
username : root
password : 123456
driverClassName : com.mysql.jdbc.Driver
redis:
host: 127.0.0.1
password: 123456
port: 6379
pool:
max-idle: 100
min-idle: 1
max-active: 1000
max-wait: -1
package com.boot.cache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* redis 配置類
* @author Administrator
*/
@Configuration
@EnableCaching// 啓用緩存
public class RedisCacheConfig extends CachingConfigurerSupport{
@Autowired
private RedisProperties redisProperties;
@Bean(name="redisConnectionFactory")
public JedisConnectionFactory redisConnectionFactory(){
JedisConnectionFactory redisConnectionFactory = new JedisConnectionFactory();
redisConnectionFactory.setHostName(redisProperties.getHost());
redisConnectionFactory.setPort(redisProperties.getPort());
redisConnectionFactory.setPassword(redisProperties.getPassword());
return redisConnectionFactory;
}
/***
* 緩存管理器
* @param redisTemplate
*/
@Bean
public CacheManager cacheManager(RedisTemplate<?,?> redisTemplate){
CacheManager cacheManager = new RedisCacheManager(redisTemplate);
return cacheManager;
}
/**
* redis模板操做類,相似於jdbcTemplate的一個類;
* 防止中文亂碼
*/
@Bean
public RedisTemplate<String ,String> redisTemplate(RedisConnectionFactory factory){
RedisTemplate<String,String> redisTemplate = new RedisTemplate<String,String>();
redisTemplate.setConnectionFactory(factory);
RedisSerializer<String> redisSerializer = new StringRedisSerializer();//Long類型不能夠會出現異常信息;
redisTemplate.setKeySerializer(redisSerializer);
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.setHashKeySerializer(redisSerializer);
return redisTemplate;
}
}
package com.boot.cache;
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Repository;
@Repository
public class RedisDao {
@Autowired
private StringRedisTemplate template;
//添加緩存數據
public void setKey(String key, String value) {
ValueOperations<String, String> ops = template.opsForValue();
ops.set(key, value, 10, TimeUnit.MINUTES);// 10分鐘後過時
}
//查詢緩存數據
public String getValue(String key) {
ValueOperations<String, String> ops = this.template.opsForValue();
return ops.get(key);
}
//刪除緩存數據
public void removeValue(String key) {
template.delete(key);
}
}