一、环境
- Redis-x64-3.2.100.zip
- SpringBoot 1.5.10.RELEASE
Redis-x64-3.2.100.zip 下载地址:https://github.com/MicrosoftArchive/redis/releases
pom依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
redis: database: @redis.database@ # Database index used by the connection factory. host: @redis.host@ # Redis server host. password: @redis.password@ # Login password of the redis server. port: @redis.port@ pool: max-active: 50 # Max number of connections that can be allocated by the pool at a given time. Use a negative value for no limit. max-idle: 50 # Max number of "idle" connections in the pool. Use a negative value to indicate an unlimited number of idle connections. max-wait: 50 # Maximum amount of time (in milliseconds) a connection allocation should block before throwing an exception when the pool is exhausted. Use a negative value to block indefinitely. min-idle: 0 timeout: 1000
三.config
@Configuration public class RedisConfiguration { @Autowired private JedisConnectionFactory jedisConnectionFactory; @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) { RedisTemplate<String, Object> template = new RedisTemplate<String, Object>(); template.setConnectionFactory(jedisConnectionFactory); template.setKeySerializer(new StringRedisSerializer()); template.setValueSerializer(new JdkSerializationRedisSerializer()); template.afterPropertiesSet(); return template; } }
四.测试Test
@Autowired
private RedisTemplate<String, Object> redisTemplate;
// 存取对象
redisTemplate.opsForValue().set("user", new User(1, "张三"));
User user = (User) redisTemplate.opsForValue().get("user");
System.out.println(user);