直接上代码,围绕着代码来讨论
redisTemplate.execute((RedisCallback<Object>)(connection)->{Cursor<byte[]> scan = connection
.scan(ScanOptions.scanOptions().count(2).match("*").build());
scan.forEachRemaining((bytes)->{System.out.println(newString(bytes));});//while (scan.hasNext()) {// System.out.println(new String(scan.next()));//}returnnull;});
通过上述代码我们可以看到,可以通过迭代器来获取到redis中单库中所有的key,但这样底层是怎么做的呢?我们现在假定有两种方案去实现这种方案。
① redis 客户端(jedis、lettuce)scan执行之后返回了所有的数据,迭代器next()每次都从数据集中取出一条消费。但这样有个疑点 =>
ScanOptions.scanOptions().count(2).match("*").build()
我们知道count(2)代表着每次只向redis中请求2条返回数据呀。如果全部返回也不符合scan命令的设计!
② 每次迭代next()都要判断是否数据集中还有数据,没有的话去redis中通过游标取下次一的数据集(2条)。然后将获取到数据集迭代器替换到游标中,上一个数据集回收(防止内存过大),使迭代器可以正常流转。
那上面两条都是我自己的猜测,可能两条都不对,也有可能两条对一条,具体我们还是要看源码怎么做的。
分析源码
Cursor<byte[]> scan = connection.scan(ScanOptions.scanOptions().count(2).match("*").build());
这段代码返回了一个Cursor 游标,我们看看他的具体实现是这个类:
org.springframework.data.redis.core.ScanCursor
。
这个类实现了Cursor,实现了迭代器的所有方法。
当我们执行
forEachRemaining
或者
hasNext()
的时候都会去判断
@OverridepublicbooleanhasNext(){assertCursorIsOpen();// delegate可以理解为数据集的迭代器,是一个list类型,从redis获取到的数据集会放到这个list中while(!delegate.hasNext()&&!CursorState.FINISHED.equals(state)){// 可以看到如果说数据集读取完了,会继续去往redis中取下一组scan(cursorId);}if(delegate.hasNext()){returntrue;}return cursorId >0;}
scan 方法
privatevoidscan(long cursorId){// 去请求redis获取下一组keyScanIteration<T> result =doScan(cursorId,this.scanOptions);// 将数据集processScanResult(result);}
processScanResult 方法
privatevoidprocessScanResult(ScanIteration<T> result){// redis 中的游标
cursorId = result.getCursorId();if(isFinished(cursorId)){
state =CursorState.FINISHED;}if(!CollectionUtils.isEmpty(result.getItems())){// 替换迭代器 *** 重要代码
delegate = result.iterator();}else{resetDelegate();}}
可以看到forEachRemaining其实也要去先判断数据集中有值。
自此我们验证了上述的两条推断中第二条是争取的,每次迭代器流转的时候都会去判断是否还有数据,没有的话就会从redis取到新的数据集,然后把数据集的迭代器替换到游标中,从而实现了redis单库取出所有的key。spring-data-redis中实现的很优雅,很巧妙,我们也可以学习这种设计模式来批量获取远程分页数据等。最后我们来看下ScanCursor的整体代码:
/*
* Copyright 2014-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/packageorg.springframework.data.redis.core;importjava.util.Collections;importjava.util.Iterator;importjava.util.NoSuchElementException;importorg.springframework.dao.InvalidDataAccessApiUsageException;importorg.springframework.lang.Nullable;importorg.springframework.util.Assert;importorg.springframework.util.CollectionUtils;/**
* Redis client agnostic {@link Cursor} implementation continuously loading additional results from Redis server until
* reaching its starting point {@code zero}. <br />
* <strong>Note:</strong> Please note that the {@link ScanCursor} has to be initialized ({@link #open()} prior to usage.
*
* @author Christoph Strobl
* @author Thomas Darimont
* @author Duobiao Ou
* @author Marl Paluch
* @param <T>
* @since 1.4
*/publicabstractclassScanCursor<T>implementsCursor<T>{privateCursorState state;// 游标状态privatelong cursorId;// 游标当前位置privateIterator<T> delegate;// 从redis获取到的结果集的迭代器,只要实现过Iterator的类都可以被代理privatefinalScanOptions scanOptions;// 配置信息privatelong position;// 位置/**
* Crates new {@link ScanCursor} with {@code id=0} and {@link ScanOptions#NONE}
*/publicScanCursor(){this(ScanOptions.NONE);}/**
* Crates new {@link ScanCursor} with {@code id=0}.
*
* @param options the scan options to apply.
*/publicScanCursor(ScanOptions options){this(0, options);}/**
* Crates new {@link ScanCursor} with {@link ScanOptions#NONE}
*
* @param cursorId the cursor Id.
*/publicScanCursor(long cursorId){this(cursorId,ScanOptions.NONE);}/**
* Crates new {@link ScanCursor}
*
* @param cursorId the cursor Id.
* @param options Defaulted to {@link ScanOptions#NONE} if {@code null}.
*/publicScanCursor(long cursorId,@NullableScanOptions options){this.scanOptions = options !=null? options :ScanOptions.NONE;this.cursorId = cursorId;this.state =CursorState.READY;this.delegate =Collections.emptyIterator();}privatevoidscan(long cursorId){ScanIteration<T> result =doScan(cursorId,this.scanOptions);processScanResult(result);}/**
* Performs the actual scan command using the native client implementation. The given {@literal options} are never
* {@code null}.
*
* @param cursorId
* @param options
* @return
*/protectedabstractScanIteration<T>doScan(long cursorId,ScanOptions options);/**
* Initialize the {@link Cursor} prior to usage.
*/publicfinalScanCursor<T>open(){if(!isReady()){thrownewInvalidDataAccessApiUsageException("Cursor already "+ state +". Cannot (re)open it.");}
state =CursorState.OPEN;doOpen(cursorId);returnthis;}/**
* Customization hook when calling {@link #open()}.
*
* @param cursorId
*/protectedvoiddoOpen(long cursorId){scan(cursorId);}privatevoidprocessScanResult(ScanIteration<T> result){
cursorId = result.getCursorId();if(isFinished(cursorId)){
state =CursorState.FINISHED;}if(!CollectionUtils.isEmpty(result.getItems())){
delegate = result.iterator();}else{resetDelegate();}}/**
* Check whether {@code cursorId} is finished.
*
* @param cursorId the cursor Id
* @return {@literal true} if the cursor is considered finished, {@literal false} otherwise.s
* @since 2.1
*/protectedbooleanisFinished(long cursorId){return cursorId ==0;}privatevoidresetDelegate(){
delegate =Collections.emptyIterator();}/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.Cursor#getCursorId()
*/@OverridepubliclonggetCursorId(){return cursorId;}/*
* (non-Javadoc)
* @see java.util.Iterator#hasNext()
*/@OverridepublicbooleanhasNext(){assertCursorIsOpen();while(!delegate.hasNext()&&!CursorState.FINISHED.equals(state)){scan(cursorId);}if(delegate.hasNext()){returntrue;}return cursorId >0;}privatevoidassertCursorIsOpen(){if(isReady()||isClosed()){thrownewInvalidDataAccessApiUsageException("Cannot access closed cursor. Did you forget to call open()?");}}/*
* (non-Javadoc)
* @see java.util.Iterator#next()
*/@OverridepublicTnext(){assertCursorIsOpen();if(!hasNext()){thrownewNoSuchElementException("No more elements available for cursor "+ cursorId +".");}T next =moveNext(delegate);
position++;return next;}/**
* Fetch the next item from the underlying {@link Iterable}.
*
* @param source
* @return
*/protectedTmoveNext(Iterator<T> source){return source.next();}/*
* (non-Javadoc)
* @see java.util.Iterator#remove()
*/@Overridepublicvoidremove(){thrownewUnsupportedOperationException("Remove is not supported");}/*
* (non-Javadoc)
* @see java.io.Closeable#close()
*/@Overridepublicfinalvoidclose(){try{doClose();}finally{
state =CursorState.CLOSED;}}/**
* Customization hook for cleaning up resources on when calling {@link #close()}.
*/protectedvoiddoClose(){}/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.Cursor#isClosed()
*/@OverridepublicbooleanisClosed(){return state ==CursorState.CLOSED;}protectedfinalbooleanisReady(){return state ==CursorState.READY;}protectedfinalbooleanisOpen(){return state ==CursorState.OPEN;}/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.Cursor#getPosition()
*/@OverridepubliclonggetPosition(){return position;}/**
* @author Thomas Darimont
*/enumCursorState{READY,OPEN,FINISHED,CLOSED;}}
版权归原作者 澄风 所有, 如有侵权,请联系我们删除。