本文共 7848 字,大约阅读时间需要 26 分钟。
开发工具:intellij idea
JDK版本:1.8
项目管理工具:Maven 4.0.0
pom.xml配置代码:
1 25 4.0.0 6 7com.goku 8spring-boot-redis 91.0-SNAPSHOT 1011 22 23 2412 2113 20org.apache.maven.plugins 14maven-compiler-plugin 1516 1925 29 30org.springframework.boot 26spring-boot-starter-parent 271.5.6.RELEASE 2831 32 48 49 5033 36 37org.springframework.boot 34spring-boot-starter-web 3538 42 43org.springframework.boot 39spring-boot-starter-test 40test 4144 47org.springframework.boot 45spring-boot-starter-data-redis 46
# REDIS (RedisProperties)# Redis数据库索引(默认为0)spring.redis.database=0# Redis服务器地址spring.redis.host=127.0.0.1# Redis服务器连接端口spring.redis.port=6379# Redis服务器连接密码(默认为空)spring.redis.password=# 连接池最大连接数(使用负值表示没有限制)spring.redis.pool.max-active=8# 连接池最大阻塞等待时间(使用负值表示没有限制)spring.redis.pool.max-wait=-1# 连接池中的最大空闲连接spring.redis.pool.max-idle=8# 连接池中的最小空闲连接spring.redis.pool.min-idle=0# 连接超时时间(毫秒)spring.redis.timeout=0
1 package com.goku.demo.config; 2 3 4 import org.springframework.core.convert.converter.Converter; 5 import org.springframework.core.serializer.support.DeserializingConverter; 6 import org.springframework.core.serializer.support.SerializingConverter; 7 import org.springframework.data.redis.serializer.RedisSerializer; 8 import org.springframework.data.redis.serializer.SerializationException; 9 /**10 * Created by nbfujx on 2017/11/8.11 */12 public class RedisObjectSerializer implements RedisSerializer
添加注解@EnableCaching,开启缓存功能
1 package com.goku.demo.config; 2 3 import org.springframework.cache.CacheManager; 4 import org.springframework.cache.annotation.CachingConfigurerSupport; 5 import org.springframework.cache.annotation.EnableCaching; 6 import org.springframework.context.annotation.Bean; 7 import org.springframework.context.annotation.Configuration; 8 import org.springframework.data.redis.cache.RedisCacheManager; 9 import org.springframework.data.redis.connection.RedisConnectionFactory;10 import org.springframework.data.redis.core.RedisTemplate;11 import org.springframework.data.redis.serializer.StringRedisSerializer;12 13 /**14 * Created by nbfujx on 2017-12-07.15 */16 @SuppressWarnings("SpringJavaAutowiringInspection")17 @Configuration18 @EnableCaching19 public class RedisConfig extends CachingConfigurerSupport {20 21 @Bean22 public CacheManager cacheManager(RedisTemplateredisTemplate) {23 RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);24 cacheManager.setDefaultExpiration(1800);25 return cacheManager;26 }27 28 @Bean29 public RedisTemplate redisTemplate(RedisConnectionFactory factory) {30 RedisTemplate template = new RedisTemplate<>();31 template.setConnectionFactory(factory);32 template.setKeySerializer(new StringRedisSerializer());33 template.setValueSerializer(new RedisObjectSerializer());34 return template;35 }36 }
1 package com.goku.demo; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.boot.web.servlet.ServletComponentScan; 6 7 /** 8 * Created by nbfujx on 2017/11/20. 9 */10 // Spring Boot 应用的标识11 @SpringBootApplication12 @ServletComponentScan13 public class DemoApplication {14 15 public static void main(String[] args) {16 // 程序启动入口17 // 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件18 SpringApplication.run(DemoApplication.class,args);19 }20 }
1 package test.com.goku.demo.model; 2 3 import java.io.Serializable; 4 5 /** 6 * Created by nbfujx on 2017-12-07. 7 */ 8 public class User implements Serializable { 9 10 private static final long serialVersionUID = -1L;11 12 private String username;13 private Integer age;14 15 public User(String username, Integer age) {16 this.username = username;17 this.age = age;18 }19 20 public String getUsername() {21 return username;22 }23 24 public void setUsername(String username) {25 this.username = username;26 }27 28 public Integer getAge() {29 return age;30 }31 32 public void setAge(Integer age) {33 this.age = age;34 }35 }
1 package test.com.goku.demo; 2 3 import com.goku.demo.DemoApplication; 4 import org.junit.Test; 5 import org.junit.runner.RunWith; 6 import org.slf4j.Logger; 7 import org.slf4j.LoggerFactory; 8 import org.springframework.beans.factory.annotation.Autowired; 9 import org.springframework.boot.test.context.SpringBootTest;10 import org.springframework.data.redis.core.RedisTemplate;11 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;12 import test.com.goku.demo.model.User;13 14 import java.io.Serializable;15 16 /**17 * Created by nbfujx on 2017-12-07.18 */19 @RunWith(SpringJUnit4ClassRunner.class)20 @SpringBootTest(classes = DemoApplication.class)21 public class TestRedis implements Serializable{22 23 private final Logger logger = LoggerFactory.getLogger(getClass());24 25 @Autowired26 private RedisTemplate redisTemplate;27 28 @Test29 public void test() throws Exception {30 // 保存字符串31 redisTemplate.opsForValue().set("数字", "111");32 this.logger.info((String) redisTemplate.opsForValue().get("数字"));33 }34 35 @Test36 public void testobject() throws Exception {37 User user = new User("用户1", 20);38 redisTemplate.opsForValue().set("用户1",user);39 // 保存对象40 User user2= (User) redisTemplate.opsForValue().get("用户1");41 this.logger.info(String.valueOf(user2.getAge()));42 }43 44 45 }
转载地址:http://etmml.baihongyu.com/