Spring Data Redis 是Spring 框架提供的用于操作Redis的方式,最近整理了下它的用法,解决了使用过程中遇到的一些难点与坑点,希望对大家有所帮助。本文涵盖了Redis的安装、Spring Cache结合Redis的使用、Redis连接池的使用和RedisTemplate的使用等内容。
Caused by: java.lang.NoClassDefFoundError: org/apache/commons/pool2/impl/GenericObjectPoolConfig at org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration$LettucePoolingClientConfigurationBuilder.(LettucePoolingClientConfiguration.java:84) ~[spring-data-redis-2.1.5.RELEASE.jar:2.1.5.RELEASE] at org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration.builder(LettucePoolingClientConfiguration.java:48) ~[spring-data-redis-2.1.5.RELEASE.jar:2.1.5.RELEASE] at org.springframework.boot.autoconfigure.data.redis.LettuceConnectionConfiguration$PoolBuilderFactory.createBuilder(LettuceConnectionConfiguration.java:149) ~[spring-boot-autoconfigure-2.1.3.RELEASE.jar:2.1.3.RELEASE] at org.springframework.boot.autoconfigure.data.redis.LettuceConnectionConfiguration.createBuilder(LettuceConnectionConfiguration.java:107) ~[spring-boot-autoconfigure-2.1.3.RELEASE.jar:2.1.3.RELEASE] at org.springframework.boot.autoconfigure.data.redis.LettuceConnectionConfiguration.getLettuceClientConfiguration(LettuceConnectionConfiguration.java:93) ~[spring-boot-autoconfigure-2.1.3.RELEASE.jar:2.1.3.RELEASE] at org.springframework.boot.autoconfigure.data.redis.LettuceConnectionConfiguration.redisConnectionFactory(LettuceConnectionConfiguration.java:74) ~[spring-boot-autoconfigure-2.1.3.RELEASE.jar:2.1.3.RELEASE] at org.springframework.boot.autoconfigure.data.redis.LettuceConnectionConfiguration$$EnhancerBySpringCGLIB$$5caa7e47.CGLIB$redisConnectionFactory$0() ~[spring-boot-autoconfigure-2.1.3.RELEASE.jar:2.1.3.RELEASE] at org.springframework.boot.autoconfigure.data.redis.LettuceConnectionConfiguration$$EnhancerBySpringCGLIB$$5caa7e47$$FastClassBySpringCGLIB$$b8ae2813.invoke() ~[spring-boot-autoconfigure-2.1.3.RELEASE.jar:2.1.3.RELEASE] at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244) ~[spring-core-5.1.5.RELEASE.jar:5.1.5.RELEASE] at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:363) ~[spring-context-5.1.5.RELEASE.jar:5.1.5.RELEASE] at org.springframework.boot.autoconfigure.data.redis.LettuceConnectionConfiguration$$EnhancerBySpringCGLIB$$5caa7e47.redisConnectionFactory() ~[spring-boot-autoconfigure-2.1.3.RELEASE.jar:2.1.3.RELEASE] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_91] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_91] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_91] at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_91] at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE] ... 111 common frames omitted
自由操作Redis
Spring Cache 给我们提供了操作Redis缓存的便捷方法,但是也有很多局限性。比如说我们想单独设置一个缓存值的有效期怎么办?我们并不想缓存方法的返回值,我们想缓存方法中产生的中间值怎么办?此时我们就需要用到RedisTemplate这个类了,接下来我们来讲下如何通过RedisTemplate来自由操作Redis中的缓存。
@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 SetsMembers(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 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 ListlRange(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); } }
RedisController
测试RedisService中缓存操作的Controller,大家可以调用测试下。
/** * Redis测试Controller * Created by macro on 2020/3/3. */ @Api(tags = "RedisController", description = "Redis测试") @Controller @RequestMapping("/redis") publicclassRedisController{ @Autowired private RedisService redisService; @Autowired private PmsBrandService brandService;