1、pom文件
< dependency> < groupId> org. springframework. boot< / groupId> < artifactId> spring- boot- starter- data- redis< / artifactId> < / dependency>
2、redis工具类
@Service ( value = "redisService" )
@Slf4j
public class RedisService { @Autowired private RedisTemplate redisTemplate; public void removeBatchKey ( final String . . . keys) { for ( String key : keys) { removeByKey ( key) ; } } @SuppressWarnings ( "unchecked" ) public void removePattern ( final String pattern) { Set < Serializable > keys = redisTemplate. keys ( pattern) ; if ( ! CollectionUtils . isEmpty ( keys) ) { redisTemplate. delete ( keys) ; } } @SuppressWarnings ( "unchecked" ) public void removeByKey ( final String key) { if ( exists ( key) ) { redisTemplate. delete ( key) ; } } @SuppressWarnings ( "unchecked" ) public boolean existsBatchKey ( final String . . . keys) { for ( String key : keys) { if ( ! redisTemplate. hasKey ( key) ) { return false ; } } return true ; } @SuppressWarnings ( "unchecked" ) public boolean exists ( final String key) { return redisTemplate. hasKey ( key) ; } @SuppressWarnings ( "unchecked" ) public Object get ( final String key) { Object result; ValueOperations < Serializable , Object > operations = redisTemplate. opsForValue ( ) ; result = operations. get ( key) ; return result; } @SuppressWarnings ( "unchecked" ) public boolean setNoExpire ( final String key, Object value) { boolean result = false ; try { ValueOperations < Serializable , Object > operations = redisTemplate. opsForValue ( ) ; operations. set ( key, value) ; result = true ; } catch ( DataAccessException e) { log. error ( "Write RedisCache failure:" + e) ; } return result; } @SuppressWarnings ( "unchecked" ) public boolean setTakeOverTime ( final String key, Object value, Long expireTime) { boolean result = false ; try { ValueOperations < Serializable , Object > operations = redisTemplate. opsForValue ( ) ; operations. set ( key, value) ; redisTemplate. expire ( key, expireTime, TimeUnit . SECONDS ) ; result = true ; } catch ( DataAccessException e) { log. error ( "Write RedisCache failure:" + e) ; } return result; } @SuppressWarnings ( "unchecked" ) public void setKeyValue ( String key, Object field, Object value) { if ( StringUtils . isEmpty ( key) || null == field || null == value) { log. debug ( "Read RedisCache failure: Parameter cannot Empty" ) ; } redisTemplate. opsForHash ( ) . put ( key, field, value) ; } @SuppressWarnings ( "unchecked" ) public long getHashKeylen ( String key) { if ( StringUtils . isEmpty ( key) ) { log. debug ( "Read RedisCache failure: Parameter cannot Empty" ) ; } return redisTemplate. opsForHash ( ) . size ( key) ; } @SuppressWarnings ( "unchecked" ) public Object get ( String key, Object field) { if ( StringUtils . isEmpty ( key) || null == field) { log. debug ( "Read RedisCache failure: Parameter cannot Empty" ) ; } return redisTemplate. opsForHash ( ) . get ( key, field) ; } @SuppressWarnings ( "unchecked" ) public Object deleteHash ( String key, String field) { if ( StringUtils . isEmpty ( key) || StringUtils . isEmpty ( field) ) { log. debug ( "Read RedisCache failure: Parameter cannot Empty" ) ; } return redisTemplate. opsForHash ( ) . delete ( key, field) ; } @SuppressWarnings ( "unchecked" ) public boolean existKeyValue ( String key, String field) { if ( StringUtils . isEmpty ( key) || StringUtils . isEmpty ( field) ) { return false ; } else { HashOperations < String , String , Object > operations = redisTemplate. opsForHash ( ) ; return operations. hasKey ( "spring:session:sessions:" + key, field) ; } } @SuppressWarnings ( "unchecked" ) public void setExpireTime ( Object key, Long timeout, TimeUnit timeUnit) { redisTemplate. expire ( key, timeout, TimeUnit . SECONDS ) ; } @SuppressWarnings ( "unchecked" ) public long increment ( String key, long value) { if ( StringUtils . isEmpty ( key) || StringUtils . isEmpty ( value) ) { log. debug ( "Read RedisCache failure: Parameter cannot Empty" ) ; } return redisTemplate. opsForValue ( ) . increment ( key, value) ; } }