1、在Maven pom.xml文件中加入Redis包
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> 2、SpringBoot配置文件中配置Redis连接 #redis spring.redis.host=127.0.0.1 spring.redis.port=6379 spring.redis.database=2 spring.redis.password= spring.redis.jedis.pool.max-idle=100 spring.redis.jedis.pool.max-active=300 spring.redis.jedis.pool.max-wait=10000 spring.redis.timeout=10000
3、Redis配置类 @Configuration public class RedisConfig { @Bean() @SuppressWarnings("all") public RedisTemplate<Serializable, Object> redisTemplate(RedisConnectionFactory factory) { RedisTemplate<Serializable, Object> template = new RedisTemplate<Serializable, Object>(); template.setConnectionFactory(factory); JdkSerializationRedisSerializer jdkSerializationRedisSerializer = new JdkSerializationRedisSerializer(); ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); //jackson2JsonRedisSerializer.setObjectMapper(om); StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); // key采用String的序列化方式 template.setKeySerializer(stringRedisSerializer); // hash的key也采用String的序列化方式 template.setHashKeySerializer(stringRedisSerializer); // value序列化方式采用jackson template.setValueSerializer(jdkSerializationRedisSerializer); // hash的value序列化方式采用jackson template.setHashValueSerializer(jdkSerializationRedisSerializer); template.afterPropertiesSet(); return template; } }
4、工具类JedisUtil
@Component public class JedisUtil { private Logger logger = LoggerFactory.getLogger(RedisUtil.class); @Autowired private RedisTemplate<Serializable, Object> redisTemplate; public JedisUtil() { } public void remove(String... keys) { String[] var2 = keys; int var3 = keys.length; for(int var4 = 0; var4 < var3; ++var4) { String key = var2[var4]; this.remove(key); } } public void removePattern(String pattern) { Set<Serializable> keys = this.redisTemplate.keys(pattern); if (keys.size() > 0) { this.redisTemplate.delete(keys); } } public void remove(String key) { if (this.exists(key)) { this.redisTemplate.delete(key); } } public boolean exists(String key) { return this.redisTemplate.hasKey(key).booleanValue(); } public Object get(String key) { Object result = null; ValueOperations<Serializable, Object> operations = this.redisTemplate.opsForValue(); result = operations.get(key); return result; } public boolean set(String key, Object value) { boolean result = false; try { ValueOperations<Serializable, Object> operations = this.redisTemplate.opsForValue(); operations.set(key, value); result = true; } catch (Exception var5) { var5.printStackTrace(); } return result; } public boolean set(String key, Object value, Long expireTime) { boolean result = false; try { ValueOperations<Serializable, Object> operations = this.redisTemplate.opsForValue(); operations.set(key, value, expireTime.longValue(), TimeUnit.SECONDS); result = true; } catch (Exception var6) { var6.printStackTrace(); } return result; } public void setRedisTemplate(RedisTemplate<Serializable, Object> redisTemplate) { this.redisTemplate = redisTemplate; } public boolean hset(String key, String hashKey, Object value, Long expireTime) { boolean result = false; try { HashOperations<Serializable, String, Object> operations = this.redisTemplate.opsForHash(); operations.put(key, hashKey, value); this.redisTemplate.expire(key, expireTime.longValue(), TimeUnit.SECONDS); result = true; } catch (Exception var7) { var7.printStackTrace(); } return result; } public boolean hset(String key, String hashKey, Object value) { boolean result = false; try { HashOperations<Serializable, String, Object> operations = this.redisTemplate.opsForHash(); operations.put(key, hashKey, value); result = true; } catch (Exception var6) { var6.printStackTrace(); } return result; } public Object hget(String key, String hashKey) { Object result = null; HashOperations<Serializable, String, Object> operations = this.redisTemplate.opsForHash(); result = operations.get(key, hashKey); return result; } public boolean hdel(String key, String hashKey) { boolean result = false; try { HashOperations<Serializable, String, Object> operations = this.redisTemplate.opsForHash(); operations.delete(key, new Object[]{hashKey}); result = true; } catch (Exception var5) { var5.printStackTrace(); } return result; } public Set<String> hgetAllKeys(String key) { Set<String> result = null; HashOperations<Serializable, String, Object> operations = this.redisTemplate.opsForHash(); result = operations.keys(key); return result; } public List<Object> hgetAllValues(String key) { List<Object> result = null; HashOperations<Serializable, String, Object> operations = this.redisTemplate.opsForHash(); result = operations.values(key); return result; } public Map<String, Object> hgetAll(String key) { Map<String, Object> result = null; HashOperations<Serializable, String, Object> operations = this.redisTemplate.opsForHash(); result = operations.entries(key); return result; } public void lpush(String key, Object value) { ListOperations<Serializable, Object> operations = this.redisTemplate.opsForList(); operations.leftPush(key, value); } public Object rpop(String key) { ListOperations<Serializable, Object> operations = this.redisTemplate.opsForList(); return operations.rightPop(key); } public Object index(String key, int index) { ListOperations<Serializable, Object> operations = this.redisTemplate.opsForList(); return operations.index(key, (long)index); } public Long increment(String key) { return this.redisTemplate.opsForValue().increment(key, 1L); } public Long increment(String key, long delta) { return this.redisTemplate.opsForValue().increment(key, delta); } public RedisTemplate getRedisTemplate() { return this.redisTemplate; } }