pom 依赖
1<dependency> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-data-redis</artifactId> 4 </dependency> 5 <dependency> 6 <groupId>redis.clients</groupId> 7 <artifactId>jedis</artifactId> 8 </dependency>
yml文件配置
1#springboot版本为2:0:2RELEASE中: 2 redis: 3 # Redis数据库索引(默认为0) 4 database: 10 5 # Redis服务器地址 6 host: localhost 7 # Redis服务器连接端口 8 port: 6379 9 # Redis服务器连接密码(默认为空) 10 password: wiki123 11 timeout: 3 12 jedis: 13 pool: 14 max-active: 8 # 连接池最大连接数(使用负值表示没有限制) 15 max-wait: -1 # 连接池最大阻塞等待时间(使用负值表示没有限制) 16 max-idle: 8 # 连接池中的最大空闲连接 17 min-idle: 0 # 连接池中的最小空闲连接 18 19#redis配置 springboot版本为1.3.2RELEASE中的RedisProperties配置文件类 20# redis: 21# # Redis数据库索引(默认为0) 22# database: 10 23# # Redis服务器地址 24# host: localhost 25# # Redis服务器连接端口 26# port: 6379 27# # Redis服务器连接密码(默认为空) 28# password: wiki123 29# timeout: 3 30# pool: 31# # 连接池最大连接数(使用负值表示没有限制) 32# max-active: 8 33# # 连接池最大阻塞等待时间(使用负值表示没有限制) 34# max-wait: -1 35# # 连接池中的最大空闲连接 36# max-idle: 8 37# # 连接池中的最小空闲连接 38# min-idle: 0 39# # 连接超时时间(毫秒)
配置redis
1package top.xz.hand.config; 2 3import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 4import org.springframework.boot.context.properties.ConfigurationProperties; 5import org.springframework.context.annotation.Bean; 6import org.springframework.context.annotation.Configuration; 7import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; 8import org.springframework.data.redis.core.RedisTemplate; 9import org.springframework.data.redis.core.StringRedisTemplate; 10import redis.clients.jedis.JedisPoolConfig; 11 12@Configuration 13@EnableAutoConfiguration 14public class RedisConfig { 15 16 /** 17 * 获取JedisPoolConfig配置 18 * 19 * @return 20 */ 21 @Bean 22 @ConfigurationProperties(prefix = "spring.redis.jedis.pool") 23 public JedisPoolConfig getRedisConfig(){ 24 JedisPoolConfig config = new JedisPoolConfig(); 25 return config; 26 } 27 28 /** 29 * 获取JedisConnectionFactory工厂 30 * 31 * @return 32 */ 33 @Bean 34 @ConfigurationProperties(prefix = "spring.redis") 35 public JedisConnectionFactory getConnectionFactory() { 36 JedisConnectionFactory factory = new JedisConnectionFactory(); 37 factory.setUsePool(true); 38 JedisPoolConfig config = getRedisConfig(); 39 factory.setPoolConfig(config); 40 return factory; 41 } 42 43 /** 44 * 获取RedisTemplate模板 45 * 46 * @return 47 */ 48 @Bean 49 public RedisTemplate<?, ?> getRedisTemplate() { 50 JedisConnectionFactory factory = getConnectionFactory(); 51 RedisTemplate<?, ?> template = new StringRedisTemplate(factory); 52 return template; 53 } 54 55}
redis接口
1package top.xz.hand.service; 2 3public interface RedisService { 4 /** 5 * set存数据 6 * @param key 7 * @param value 8 * @return 9 */ 10 boolean set(String key, String value); 11 12 /** 13 * get获取数据 14 * @param key 15 * @return 16 */ 17 String get(String key); 18 19 /** 20 * 设置有效天数 21 * @param key 22 * @param expire 23 * @return 24 */ 25 boolean expire(String key, long expire); 26 27 /** 28 * 移除数据 29 * @param key 30 * @return 31 */ 32 boolean remove(String key); 33 34}
实现
1package top.xz.hand.service.impl; 2 3import org.springframework.dao.DataAccessException; 4import org.springframework.data.redis.connection.RedisConnection; 5import org.springframework.data.redis.core.RedisCallback; 6import org.springframework.data.redis.core.RedisTemplate; 7import org.springframework.data.redis.serializer.RedisSerializer; 8import org.springframework.stereotype.Service; 9import top.xz.hand.service.RedisService; 10 11import javax.annotation.Resource; 12import java.util.concurrent.TimeUnit; 13@Service 14public class RedisServiceImpl implements RedisService { 15 @Resource 16 private RedisTemplate<String, ?> redisTemplate; 17 18 @Override 19 public boolean set(final String key, final String value) { 20 boolean result = redisTemplate.execute(new RedisCallback<Boolean>() { 21 @Override 22 public Boolean doInRedis(RedisConnection connection) throws DataAccessException { 23 RedisSerializer<String> serializer = redisTemplate.getStringSerializer(); 24 connection.set(serializer.serialize(key), serializer.serialize(value)); 25 return true; 26 } 27 }); 28 return result; 29 } 30 31 @Override 32 public String get(final String key) { 33 String result = redisTemplate.execute(new RedisCallback<String>() { 34 @Override 35 public String doInRedis(RedisConnection connection) throws DataAccessException { 36 RedisSerializer<String> serializer = redisTemplate.getStringSerializer(); 37 byte[] value = connection.get(serializer.serialize(key)); 38 return serializer.deserialize(value); 39 } 40 }); 41 return result; 42 } 43 44 @Override 45 public boolean expire(final String key, long expire) { 46 return redisTemplate.expire(key, expire, TimeUnit.SECONDS); 47 } 48 49 @Override 50 public boolean remove(final String key) { 51 boolean result = redisTemplate.execute(new RedisCallback<Boolean>() { 52 @Override 53 public Boolean doInRedis(RedisConnection connection) throws DataAccessException { 54 connection.del(key.getBytes()); 55 return true; 56 } 57 }); 58 return result; 59 } 60}