Skip to content

Commit

Permalink
Added SCAN method.
Browse files Browse the repository at this point in the history
  • Loading branch information
danieleteti committed Mar 8, 2024
1 parent c50b4ef commit 2fee116
Show file tree
Hide file tree
Showing 11 changed files with 196 additions and 11 deletions.
31 changes: 30 additions & 1 deletion sources/Redis.Client.pas
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// Delphi REDIS Client
//
// Copyright (c) 2015-2023 Daniele Teti
// Copyright (c) 2015-2024 Daniele Teti
//
// https://github.com/danieleteti/delphiredisclient
//
Expand Down Expand Up @@ -218,6 +218,7 @@ TRedisClient = class(TRedisClientBase, IRedisClient)
function MOVE(const aKey: string; const aDB: Byte): boolean;
function PERSIST(const aKey: string): boolean;
function RANDOMKEY: TRedisString;
procedure SCAN(aPattern: string; aCallback: TProc<TArray<string>>);
// non system
function InTransaction: boolean;
// transations
Expand Down Expand Up @@ -265,6 +266,34 @@ function TRedisClient.SADD(const aKey, aValue: string): Integer;
Result := SADD(BytesOfUnicode(aKey), BytesOfUnicode(aValue));
end;

procedure TRedisClient.SCAN(aPattern: string; aCallback: TProc<TArray<string>>);
var
lCmd: IRedisCommand;
aCursor: integer;
AResp: TRedisRESPArray;
AKey: string;
Values: TArray<string>;
begin
aCursor := 0;
{$HINTS OFF} // H2443 - about inline function
repeat
lCmd := NewRedisCommand('SCAN');
lCmd.Add(aCursor);
lCmd.Add('MATCH');
lCmd.Add(Apattern);
AResp := ExecuteAndGetRESPArray(lCmd);
try
ACursor := AResp.I[0];
SetLength(Values, 0);
for AKey in AResp[1].ArrayValue do
Values := Values + [AKey];
finally
AResp.Free;
end;
aCallback(Values);
until aCursor = 0;
end;

function TRedisClient.SCARD(const aKey: string): Integer;
begin
FNextCMD := GetCmdList('SCARD').Add(aKey);
Expand Down
2 changes: 1 addition & 1 deletion sources/Redis.Command.pas
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// Delphi REDIS Client
//
// Copyright (c) 2015-2023 Daniele Teti
// Copyright (c) 2015-2024 Daniele Teti
//
// https://github.com/danieleteti/delphiredisclient
//
Expand Down
5 changes: 4 additions & 1 deletion sources/Redis.Commons.pas
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// Delphi REDIS Client
//
// Copyright (c) 2015-2023 Daniele Teti
// Copyright (c) 2015-2024 Daniele Teti
//
// https://github.com/danieleteti/delphiredisclient
//
Expand Down Expand Up @@ -38,6 +38,8 @@ interface
REDIS_NETLIB_INDY = 'indy';

type
TRedisKeyList = TArray<string>;

ERedisException = class(Exception)
end;

Expand Down Expand Up @@ -230,6 +232,7 @@ TRedisClientBase = class abstract(TInterfacedObject)
procedure AUTH(const aPassword: string); overload;
procedure AUTH(const aUsername, aPassword: string); overload;
function MOVE(const aKey: string; const aDB: Byte): boolean;
procedure SCAN(aPattern: string; aCallback: TProc<TArray<string>>);

// raw execute
function ExecuteAndGetRESPArray(const RedisCommand: IRedisCommand): TRedisRESPArray;
Expand Down
2 changes: 1 addition & 1 deletion sources/Redis.NetLib.Factory.pas
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// Delphi REDIS Client
//
// Copyright (c) 2015-2023 Daniele Teti
// Copyright (c) 2015-2024 Daniele Teti
//
// https://github.com/danieleteti/delphiredisclient
//
Expand Down
2 changes: 1 addition & 1 deletion sources/Redis.NetLib.INDY.pas
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// Delphi REDIS Client
//
// Copyright (c) 2015-2023 Daniele Teti
// Copyright (c) 2015-2024 Daniele Teti
//
// https://github.com/danieleteti/delphiredisclient
//
Expand Down
2 changes: 1 addition & 1 deletion sources/Redis.Values.pas
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// Delphi REDIS Client
//
// Copyright (c) 2015-2023 Daniele Teti
// Copyright (c) 2015-2024 Daniele Teti
//
// https://github.com/danieleteti/delphiredisclient
//
Expand Down
2 changes: 1 addition & 1 deletion sources/RedisMQ.pas
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// Delphi REDIS Client
//
// Copyright (c) 2015-2023 Daniele Teti
// Copyright (c) 2015-2024 Daniele Teti
//
// https://github.com/danieleteti/delphiredisclient
//
Expand Down
132 changes: 131 additions & 1 deletion tests/RedisClientTests.dproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ProjectGuid>{7843B265-EF0B-481C-9CF2-1FD021717D89}</ProjectGuid>
<ProjectVersion>19.5</ProjectVersion>
<ProjectVersion>20.1</ProjectVersion>
<FrameworkType>None</FrameworkType>
<Base>True</Base>
<Config Condition="'$(Config)'==''">Debug</Config>
Expand Down Expand Up @@ -303,6 +303,16 @@
<Operation>1</Operation>
</Platform>
</DeployClass>
<DeployClass Name="AndroidSplashImageDefV21">
<Platform Name="Android">
<RemoteDir>res\drawable-anydpi-v21</RemoteDir>
<Operation>1</Operation>
</Platform>
<Platform Name="Android64">
<RemoteDir>res\drawable-anydpi-v21</RemoteDir>
<Operation>1</Operation>
</Platform>
</DeployClass>
<DeployClass Name="AndroidSplashStyles">
<Platform Name="Android">
<RemoteDir>res\values</RemoteDir>
Expand All @@ -323,6 +333,66 @@
<Operation>1</Operation>
</Platform>
</DeployClass>
<DeployClass Name="AndroidSplashStylesV31">
<Platform Name="Android">
<RemoteDir>res\values-v31</RemoteDir>
<Operation>1</Operation>
</Platform>
<Platform Name="Android64">
<RemoteDir>res\values-v31</RemoteDir>
<Operation>1</Operation>
</Platform>
</DeployClass>
<DeployClass Name="Android_AdaptiveIcon">
<Platform Name="Android">
<RemoteDir>res\drawable-anydpi-v26</RemoteDir>
<Operation>1</Operation>
</Platform>
<Platform Name="Android64">
<RemoteDir>res\drawable-anydpi-v26</RemoteDir>
<Operation>1</Operation>
</Platform>
</DeployClass>
<DeployClass Name="Android_AdaptiveIconBackground">
<Platform Name="Android">
<RemoteDir>res\drawable</RemoteDir>
<Operation>1</Operation>
</Platform>
<Platform Name="Android64">
<RemoteDir>res\drawable</RemoteDir>
<Operation>1</Operation>
</Platform>
</DeployClass>
<DeployClass Name="Android_AdaptiveIconForeground">
<Platform Name="Android">
<RemoteDir>res\drawable</RemoteDir>
<Operation>1</Operation>
</Platform>
<Platform Name="Android64">
<RemoteDir>res\drawable</RemoteDir>
<Operation>1</Operation>
</Platform>
</DeployClass>
<DeployClass Name="Android_AdaptiveIconMonochrome">
<Platform Name="Android">
<RemoteDir>res\drawable</RemoteDir>
<Operation>1</Operation>
</Platform>
<Platform Name="Android64">
<RemoteDir>res\drawable</RemoteDir>
<Operation>1</Operation>
</Platform>
</DeployClass>
<DeployClass Name="Android_AdaptiveIconV33">
<Platform Name="Android">
<RemoteDir>res\drawable-anydpi-v33</RemoteDir>
<Operation>1</Operation>
</Platform>
<Platform Name="Android64">
<RemoteDir>res\drawable-anydpi-v33</RemoteDir>
<Operation>1</Operation>
</Platform>
</DeployClass>
<DeployClass Name="Android_Colors">
<Platform Name="Android">
<RemoteDir>res\values</RemoteDir>
Expand All @@ -333,6 +403,16 @@
<Operation>1</Operation>
</Platform>
</DeployClass>
<DeployClass Name="Android_ColorsDark">
<Platform Name="Android">
<RemoteDir>res\values-night-v21</RemoteDir>
<Operation>1</Operation>
</Platform>
<Platform Name="Android64">
<RemoteDir>res\values-night-v21</RemoteDir>
<Operation>1</Operation>
</Platform>
</DeployClass>
<DeployClass Name="Android_DefaultAppIcon">
<Platform Name="Android">
<RemoteDir>res\drawable</RemoteDir>
Expand Down Expand Up @@ -503,6 +583,56 @@
<Operation>1</Operation>
</Platform>
</DeployClass>
<DeployClass Name="Android_VectorizedNotificationIcon">
<Platform Name="Android">
<RemoteDir>res\drawable-anydpi-v24</RemoteDir>
<Operation>1</Operation>
</Platform>
<Platform Name="Android64">
<RemoteDir>res\drawable-anydpi-v24</RemoteDir>
<Operation>1</Operation>
</Platform>
</DeployClass>
<DeployClass Name="Android_VectorizedSplash">
<Platform Name="Android">
<RemoteDir>res\drawable</RemoteDir>
<Operation>1</Operation>
</Platform>
<Platform Name="Android64">
<RemoteDir>res\drawable</RemoteDir>
<Operation>1</Operation>
</Platform>
</DeployClass>
<DeployClass Name="Android_VectorizedSplashDark">
<Platform Name="Android">
<RemoteDir>res\drawable-night-anydpi-v21</RemoteDir>
<Operation>1</Operation>
</Platform>
<Platform Name="Android64">
<RemoteDir>res\drawable-night-anydpi-v21</RemoteDir>
<Operation>1</Operation>
</Platform>
</DeployClass>
<DeployClass Name="Android_VectorizedSplashV31">
<Platform Name="Android">
<RemoteDir>res\drawable-anydpi-v31</RemoteDir>
<Operation>1</Operation>
</Platform>
<Platform Name="Android64">
<RemoteDir>res\drawable-anydpi-v31</RemoteDir>
<Operation>1</Operation>
</Platform>
</DeployClass>
<DeployClass Name="Android_VectorizedSplashV31Dark">
<Platform Name="Android">
<RemoteDir>res\drawable-night-anydpi-v31</RemoteDir>
<Operation>1</Operation>
</Platform>
<Platform Name="Android64">
<RemoteDir>res\drawable-night-anydpi-v31</RemoteDir>
<Operation>1</Operation>
</Platform>
</DeployClass>
<DeployClass Name="DebugSymbols">
<Platform Name="iOSSimulator">
<Operation>1</Operation>
Expand Down
25 changes: 24 additions & 1 deletion tests/TestRedisClientU.pas
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// Delphi REDIS Client
//
// Copyright (c) 2015-2021 Daniele Teti
// Copyright (c) 2015-2024 Daniele Teti
//
// https://github.com/danieleteti/delphiredisclient
//
Expand Down Expand Up @@ -122,6 +122,8 @@ TestRedisClient = class(TTestCase)
procedure TestZREVRANGE;
procedure TestZRANGE;

procedure TestSCAN;

// test Redis 3.2+ commands
procedure TestGEODIST;
procedure TestGEOPOS;
Expand Down Expand Up @@ -1123,6 +1125,27 @@ procedure TestRedisClient.TestRPUSH_RPOP;
CheckEquals(False, FRedis.RPOP('mylist', Value));
end;

procedure TestRedisClient.TestSCAN;
begin
FRedis.&SET('A1','v1');
FRedis.&SET('A2','v1');
FRedis.&SET('A3','v1');
CheckTrue(FRedis.EXISTS('A1'));
CheckTrue(FRedis.EXISTS('A2'));
CheckTrue(FRedis.EXISTS('A3'));
FRedis.SCAN('A*',
procedure(Topics: TArray<string>)
begin
if Length(Topics) > 0 then
begin
FRedis.DEL(Topics);
end;
end);
CheckFalse(FRedis.EXISTS('A1'));
CheckFalse(FRedis.EXISTS('A2'));
CheckFalse(FRedis.EXISTS('A3'));
end;

procedure TestRedisClient.TestSDIFF;
var
lRes: TRedisArray;
Expand Down
2 changes: 1 addition & 1 deletion tests/TestRedisMQ.pas
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// Delphi REDIS Client
//
// Copyright (c) 2015-2021 Daniele Teti
// Copyright (c) 2015-2024 Daniele Teti
//
// https://github.com/danieleteti/delphiredisclient
//
Expand Down
2 changes: 1 addition & 1 deletion tests/TestRedisValuesU.pas
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// Delphi REDIS Client
//
// Copyright (c) 2015-2021 Daniele Teti
// Copyright (c) 2015-2024 Daniele Teti
//
// https://github.com/danieleteti/delphiredisclient
//
Expand Down

0 comments on commit 2fee116

Please sign in to comment.