解決spring data redis的那些坑
spring data redis的那些坑
spring 的IOC很少有bug,AOPbug開始多起來,到了它的一些“玩具”一樣的組件,bug無處不在。而且跟一般的開源框架不同,在github上你報(bào)告issue,會被“這不是一個(gè)bug”強(qiáng)行關(guān)閉。開一博文記錄,給遇到同樣問題而苦惱的人歇歇腳。
1. 使用lua腳本,返回類型解析錯誤
背景:一般來講,就算腳本里沒有return語句,redis也是會返回執(zhí)行結(jié)果,看起來就像:{“Ok” = “ok”},或者{“ok”:”ok”}。然而對于一些操作redis沒有返回,或者return語句后面返回一個(gè)值,spring包了的那一層殼就會出問題。影響的包:spring封裝了jedis的所有版本,包括:spring-data-redis 2.0以下的所有版本,以及使用了jedis的2.0以上版本:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> <version>2.0.0.RELEASE</version> <exclusions> <exclusion> <groupId>io.lettuce</groupId> <artifactId>lettuce-core</artifactId> </exclusion> </exclusions> </dependency>
這種情況下就會遇到
XXX cannot be cast to XXX
原因:DefaultScriptExecutor.java類中:
public <T> T execute(final RedisScript<T> script, final RedisSerializer<?> argsSerializer, final RedisSerializer<T> resultSerializer, final List<K> keys, final Object... args) { return template.execute((RedisCallback<T>) connection -> { final ReturnType returnType = ReturnType.fromJavaType(script.getResultType()); // return type is wrong. final byte[][] keysAndArgs = keysAndArgs(argsSerializer, keys, args); final int keySize = keys != null ? keys.size() : 0; if (connection.isPipelined() || connection.isQueueing()) { // We could script load first and then do evalsha to ensure sha is present, // but this adds a sha1 to exec/closePipeline results. Instead, just eval connection.eval(scriptBytes(script), returnType, keySize, keysAndArgs); return null; } return eval(connection, script, returnType, keySize, keysAndArgs, resultSerializer); }); }
而作為消費(fèi)者,一般會將返回值設(shè)置為Object,因?yàn)橥粋€(gè)腳本里有若干的邏輯,不同情況下返回值可能是布爾型,字符串型,Number型等。
ScriptSource scriptSource = new ResourceScriptSource(new ClassPathResource("META-INF/scripts/redis.lua")); DefaultRedisScript<Object> redisScript = new DefaultRedisScript<Object>(); redisScript.setScriptSource(scriptSource); redisScript.setResultType(Object.class);
而DefaultScriptExecutor的execute方法,會把Object類型解析為List類型,進(jìn)而設(shè)置returnType為Multi。
public Object convert(Object result) { if (result instanceof String) { // evalsha converts byte[] to String. Convert back for consistency return SafeEncoder.encode((String) result); } if (returnType == ReturnType.STATUS) { return JedisConverters.toString((byte[]) result); } if (returnType == ReturnType.BOOLEAN) { // Lua false comes back as a null bulk reply if (result == null) { return Boolean.FALSE; } return ((Long) result == 1); } if (returnType == ReturnType.MULTI) { List<Object> resultList = (List<Object>) result; List<Object> convertedResults = new ArrayList<>(); for (Object res : resultList) { if (res instanceof String) { // evalsha converts byte[] to String. Convert back for // consistency convertedResults.add(SafeEncoder.encode((String) res)); } else { convertedResults.add(res); } } return convertedResults; } return result; }
會因?yàn)閞esult(原本只是一個(gè)Object),被解析為List,轉(zhuǎn)換出了問題。此外,這里居然沒有設(shè)置null的轉(zhuǎn)換,難道null就不是List了。。。好在spring redis基于lettuce的實(shí)現(xiàn)不存在這個(gè)問題。
2. spring redis基于lettuce配置Client必須顯示調(diào)用
從官方的reference看,spring的lettuce的配置只需要簡單使用一個(gè)包含host、port、database、password等鏈接必須信息構(gòu)造的RedisStandaloneConfiguration對象作為參數(shù)傳遞給LettuceConnectionFactory 的構(gòu)造函數(shù),同理連接池,然而實(shí)際使用中發(fā)現(xiàn),ConnectionFactory用于建立連接的是從它的client屬性獲取的服務(wù)器地址等,因此必須調(diào)用afterPropertiesSet方法。
現(xiàn)在client信息有了,可以連接,但是連接池又未開啟,盡管已經(jīng)在構(gòu)造器參數(shù)中指定過。受限于時(shí)間,還沒有調(diào)這個(gè)點(diǎn)。
LettucePoolingClientConfiguration poolingClientConfiguration = LettucePoolingClientConfiguration.builder() .poolConfig(new GenericObjectPoolConfig()) .build(); RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration( redisProperty.getHost(),redisProperty.getPort() ); redisStandaloneConfiguration.setDatabase(redisProperty.getDatabase()); LettuceConnectionFactory cf = new LettuceConnectionFactory(redisStandaloneConfiguration, poolingClientConfiguration); cf.afterPropertiesSet(); // must StringRedisTemplate stringRedisTemplate = new StringRedisTemplate(); stringRedisTemplate.setConnectionFactory(cf); setSerializer(stringRedisTemplate);
spring data redis 的優(yōu)缺點(diǎn)
spring-data-redis是由spring的 cache api 整合 redis 而來,它的命名規(guī)則由spring cache 的規(guī)則來定義key和對key的管理,進(jìn)一步弱化redis的API。
事實(shí)上redis提供的功能已經(jīng)足夠強(qiáng)大,并且可以直接使用,同時(shí)支持靈活的分庫。
spring 的 cache 功能主要由 @Cacheable @CacheEvict @CachePut 實(shí)現(xiàn)
@Cacheable
主要針對方法配置,能夠根據(jù)方法的請求參數(shù)對其結(jié)果進(jìn)行緩存@CachePut
主要針對方法配置,能夠根據(jù)方法的請求參數(shù)對其結(jié)果進(jìn)行緩存,和 @Cacheable 不同的是,它每次都會觸發(fā)真實(shí)方法的調(diào)用@CachEvict
主要針對方法配置,能夠根據(jù)一定的條件對緩存進(jìn)行清空
默認(rèn)情況下Spring使用CacheManagerBean 來實(shí)現(xiàn),其實(shí)現(xiàn)有3種:EHCache,Redis,ConcurrentHashMap,默認(rèn)的ConcurrentHashMap 是沒有過期的。
Redis 的使用也是要自己手動調(diào) expire ,所以暫時(shí)使用原生的 jedis ,直接調(diào)用 redis 的api
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java實(shí)現(xiàn)AES加密和解密方式完整示例
這篇文章主要給大家介紹了關(guān)于Java實(shí)現(xiàn)AES加密和解密方式的相關(guān)資料,AES加密為最常見的對稱加密算法,是一種區(qū)塊加密標(biāo)準(zhǔn),這個(gè)標(biāo)準(zhǔn)用來替代原先的DES,已經(jīng)被多方分析且廣為全世界所使用,需要的朋友可以參考下2023-10-10Springboot項(xiàng)目啟動到一半卡住了,不報(bào)錯問題及解決
這篇文章主要介紹了Springboot項(xiàng)目啟動到一半卡住了,不報(bào)錯問題及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06spring-boot通過@Scheduled配置定時(shí)任務(wù)及定時(shí)任務(wù)@Scheduled注解的方法
這篇文章主要介紹了spring-boot通過@Scheduled配置定時(shí)任務(wù),文中還給大家介紹了springboot 定時(shí)任務(wù)@Scheduled注解的方法,需要的朋友可以參考下2017-11-11Hadoop MultipleOutputs輸出到多個(gè)文件中的實(shí)現(xiàn)方法
這篇文章主要介紹了 Hadoop MultipleOutputs輸出到多個(gè)文件中的實(shí)現(xiàn)方法的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下2017-10-10java 實(shí)現(xiàn)切割文件和合并文件的功能
這篇文章主要介紹了java 實(shí)現(xiàn)切割文件和合并文件的功能的相關(guān)資料,這里實(shí)現(xiàn)文件的切割的實(shí)現(xiàn)代碼和文件合并的實(shí)現(xiàn)代碼,需要的朋友可以參考下2017-07-07