RedisService接口
import java. util. List ;
import java. util. Map ;
import java. util. Set ;
public interface RedisService { void set ( String key, Object value, long time) ; void set ( String key, Object value) ; Object get ( String key) ; Boolean del ( String key) ; Long del ( List < String > keys) ; Boolean expire ( String key, long time) ; Long getExpire ( String key) ; Boolean hasKey ( String key) ; Long incr ( String key, long delta) ; Long decr ( String key, long delta) ; Object hGet ( String key, String hashKey) ; Boolean hSet ( String key, String hashKey, Object value, long time) ; void hSet ( String key, String hashKey, Object value) ; Map < Object , Object > hGetAll ( String key) ; Boolean hSetAll ( String key, Map < String , Object > map, long time) ; void hSetAll ( String key, Map < String , ? > map) ; void hDel ( String key, Object . . . hashKey) ; Boolean hHasKey ( String key, String hashKey) ; Long hIncr ( String key, String hashKey, Long delta) ; Long hDecr ( String key, String hashKey, Long delta) ; Set < Object > sMembers ( String key) ; Long sAdd ( String key, Object . . . values) ; Long sAdd ( String key, long time, Object . . . values) ; Boolean sIsMember ( String key, Object value) ; Long sSize ( String key) ; Long sRemove ( String key, Object . . . values) ; List < Object > lRange ( String key, long start, long end) ; Long lSize ( String key) ; Object lIndex ( String key, long index) ; Long lPush ( String key, Object value) ; Long lPush ( String key, Object value, long time) ; Long lPushAll ( String key, Object . . . values) ; Long lPushAll ( String key, Long time, Object . . . values) ; Long lRemove ( String key, long count, Object value) ;
}
RedisServiceImpl 实现类
package com. macro. mall. common. service. impl ; import com. macro. mall. common. service. RedisService ;
import org. springframework. beans. factory. annotation. Autowired ;
import org. springframework. data. redis. core. RedisTemplate ; import java. util. List ;
import java. util. Map ;
import java. util. Set ;
import java. util. concurrent. TimeUnit ;
public class RedisServiceImpl implements RedisService { @Autowired private RedisTemplate < String , Object > redisTemplate; @Override public void set ( String key, Object value, long time) { redisTemplate. opsForValue ( ) . set ( key, value, time, TimeUnit . SECONDS ) ; } @Override public void set ( String key, Object value) { redisTemplate. opsForValue ( ) . set ( key, value) ; } @Override public Object get ( String key) { return redisTemplate. opsForValue ( ) . get ( key) ; } @Override public Boolean del ( String key) { return redisTemplate. delete ( key) ; } @Override public Long del ( List < String > keys) { return redisTemplate. delete ( keys) ; } @Override public Boolean expire ( String key, long time) { return redisTemplate. expire ( key, time, TimeUnit . SECONDS ) ; } @Override public Long getExpire ( String key) { return redisTemplate. getExpire ( key, TimeUnit . SECONDS ) ; } @Override public Boolean hasKey ( String key) { return redisTemplate. hasKey ( key) ; } @Override public Long incr ( String key, long delta) { return redisTemplate. opsForValue ( ) . increment ( key, delta) ; } @Override public Long decr ( String key, long delta) { return redisTemplate. opsForValue ( ) . increment ( key, - delta) ; } @Override public Object hGet ( String key, String hashKey) { return redisTemplate. opsForHash ( ) . get ( key, hashKey) ; } @Override public Boolean hSet ( String key, String hashKey, Object value, long time) { redisTemplate. opsForHash ( ) . put ( key, hashKey, value) ; return expire ( key, time) ; } @Override public void hSet ( String key, String hashKey, Object value) { redisTemplate. opsForHash ( ) . put ( key, hashKey, value) ; } @Override public Map < Object , Object > hGetAll ( String key) { return redisTemplate. opsForHash ( ) . entries ( key) ; } @Override public Boolean hSetAll ( String key, Map < String , Object > map, long time) { redisTemplate. opsForHash ( ) . putAll ( key, map) ; return expire ( key, time) ; } @Override public void hSetAll ( String key, Map < String , ? > map) { redisTemplate. opsForHash ( ) . putAll ( key, map) ; } @Override public void hDel ( String key, Object . . . hashKey) { redisTemplate. opsForHash ( ) . delete ( key, hashKey) ; } @Override public Boolean hHasKey ( String key, String hashKey) { return redisTemplate. opsForHash ( ) . hasKey ( key, hashKey) ; } @Override public Long hIncr ( String key, String hashKey, Long delta) { return redisTemplate. opsForHash ( ) . increment ( key, hashKey, delta) ; } @Override public Long hDecr ( String key, String hashKey, Long delta) { return redisTemplate. opsForHash ( ) . increment ( key, hashKey, - delta) ; } @Override public Set < Object > sMembers ( String key) { return redisTemplate. opsForSet ( ) . members ( key) ; } @Override public Long sAdd ( String key, Object . . . values) { return redisTemplate. opsForSet ( ) . add ( key, values) ; } @Override public Long sAdd ( String key, long time, Object . . . values) { Long count = redisTemplate. opsForSet ( ) . add ( key, values) ; expire ( key, time) ; return count; } @Override public Boolean sIsMember ( String key, Object value) { return redisTemplate. opsForSet ( ) . isMember ( key, value) ; } @Override public Long sSize ( String key) { return redisTemplate. opsForSet ( ) . size ( key) ; } @Override public Long sRemove ( String key, Object . . . values) { return redisTemplate. opsForSet ( ) . remove ( key, values) ; } @Override public List < Object > lRange ( String key, long start, long end) { return redisTemplate. opsForList ( ) . range ( key, start, end) ; } @Override public Long lSize ( String key) { return redisTemplate. opsForList ( ) . size ( key) ; } @Override public Object lIndex ( String key, long index) { return redisTemplate. opsForList ( ) . index ( key, index) ; } @Override public Long lPush ( String key, Object value) { return redisTemplate. opsForList ( ) . rightPush ( key, value) ; } @Override public Long lPush ( String key, Object value, long time) { Long index = redisTemplate. opsForList ( ) . rightPush ( key, value) ; expire ( key, time) ; return index; } @Override public Long lPushAll ( String key, Object . . . values) { return redisTemplate. opsForList ( ) . rightPushAll ( key, values) ; } @Override public Long lPushAll ( String key, Long time, Object . . . values) { Long count = redisTemplate. opsForList ( ) . rightPushAll ( key, values) ; expire ( key, time) ; return count; } @Override public Long lRemove ( String key, long count, Object value) { return redisTemplate. opsForList ( ) . remove ( key, count, value) ; }
}
Redis 配置类
import com. fasterxml. jackson. annotation. JsonAutoDetect ;
import com. fasterxml. jackson. annotation. PropertyAccessor ;
import com. fasterxml. jackson. databind. ObjectMapper ;
import com. fasterxml. jackson. databind. jsontype. impl. LaissezFaireSubTypeValidator ;
import com. macro. mall. common. service. RedisService ;
import com. macro. mall. common. service. impl. RedisServiceImpl ;
import org. springframework. context. annotation. Bean ;
import org. springframework. data. redis. cache. RedisCacheConfiguration ;
import org. springframework. data. redis. cache. RedisCacheManager ;
import org. springframework. data. redis. cache. RedisCacheWriter ;
import org. springframework. data. redis. connection. RedisConnectionFactory ;
import org. springframework. data. redis. core. RedisTemplate ;
import org. springframework. data. redis. serializer. Jackson2JsonRedisSerializer ;
import org. springframework. data. redis. serializer. RedisSerializationContext ;
import org. springframework. data. redis. serializer. RedisSerializer ;
import org. springframework. data. redis. serializer. StringRedisSerializer ; import java. time. Duration ;
public class RedisConfig { @Bean public RedisTemplate < String , Object > redisTemplate ( RedisConnectionFactory redisConnectionFactory) { RedisSerializer < Object > serializer = redisSerializer ( ) ; RedisTemplate < String , Object > redisTemplate = new RedisTemplate < > ( ) ; redisTemplate. setConnectionFactory ( redisConnectionFactory) ; redisTemplate. setKeySerializer ( new StringRedisSerializer ( ) ) ; redisTemplate. setValueSerializer ( serializer) ; redisTemplate. setHashKeySerializer ( new StringRedisSerializer ( ) ) ; redisTemplate. setHashValueSerializer ( serializer) ; redisTemplate. afterPropertiesSet ( ) ; return redisTemplate; } @Bean public RedisSerializer < Object > redisSerializer ( ) { Jackson2JsonRedisSerializer < Object > serializer = new Jackson2JsonRedisSerializer < > ( Object . class ) ; ObjectMapper objectMapper = new ObjectMapper ( ) ; objectMapper. setVisibility ( PropertyAccessor . ALL , JsonAutoDetect. Visibility . ANY ) ; objectMapper. activateDefaultTyping ( LaissezFaireSubTypeValidator . instance, ObjectMapper. DefaultTyping . NON_FINAL ) ; serializer. setObjectMapper ( objectMapper) ; return serializer; } @Bean public RedisCacheManager redisCacheManager ( RedisConnectionFactory redisConnectionFactory) { RedisCacheWriter redisCacheWriter = RedisCacheWriter . nonLockingRedisCacheWriter ( redisConnectionFactory) ; RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration . defaultCacheConfig ( ) . serializeValuesWith ( RedisSerializationContext. SerializationPair . fromSerializer ( redisSerializer ( ) ) ) . entryTtl ( Duration . ofDays ( 1 ) ) ; return new RedisCacheManager ( redisCacheWriter, redisCacheConfiguration) ; } @Bean public RedisService redisService ( ) { return new RedisServiceImpl ( ) ; } }