delphi redisclient测试
unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Buttons; type TForm1 = class(TForm) Memo1: TMemo; BitBtn1: TBitBtn; procedure FormCreate(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} uses Redis.Commons, redis.Command, redis.NetLib.INDY, redis.Client; var redis: IRedisClient; cmd: IRedisCommand; procedure log(msg: string); begin Form1.Memo1.Lines.Add(msg); end; procedure TForm1.FormCreate(Sender: TObject); procedure addMsg; begin if not redis.EXISTS('myStream') then begin var xaddCmd := NewRedisCommand('XADD').Add('myStream').Add('*').Add('name').Add('jjw'); var ret := redis.ExecuteWithStringResult(xaddCmd); if ret.HasValue then Log(ret.Value); end; end; procedure getGroups; begin cmd := NewRedisCommand('XINFO').Add('GROUPS').Add('myStream'); //redis 无法处理 var ret := redis.ExecuteAndGetMatrix(cmd); if ret.HasValue then begin var c := Length(ret.Value); var v := ret.Value[0]; for var I := 0 to Length(v.Value)-1 do log(v.Value[I].Value); end; end; procedure clearGroup; begin cmd := NewRedisCommand('XGROUP').Add('DESTROY').Add('myStream').Add('myGroup'); //会清空未读,未确认的消息 var ret := redis.ExecuteWithStringResult(cmd); end; procedure createGroup; begin var createGroupCmd := NewRedisCommand('XGROUP').Add('CREATE').Add('myStream').Add('myGroup').Add('0'); // $:最新消息, 即未读过的。 0:从最后一条读 var ret := redis.ExecuteWithStringResult(createGroupCmd); if ret.HasValue then Log(ret.Value); end; procedure readMsg; begin cmd := NewRedisCommand('XREADGROUP').Add('GROUP').Add('myGroup').Add('cm').Add('COUNT').Add(2).Add('STREAMS').Add('myStream').Add('>'); //0,未ACK(确认)的未读消息 > 未读过的消息 var val := redis.ExecuteAndGetRESPArray(cmd); if val<>nil then memo1.Text := val.ToJSON(); //[["myStream",[["1704960677439-0",["name","haha","age","100"]]]]] // Memo1.Text := val.ToString; //[["myStream",[["1704960677439-0",["name","haha","age","100"]]]]] // var y1 := val[0].ArrayValue[0].Value; //myStream // Log(val[0].Items[1].Items[0].Items[0].Value); //1704960677439-0 // Log(val[0].Items[1].Items[0].Items[1].Items[0].Value); //name end; begin redis := NewRedisClient('192.168.88.129'); getGroups; addMsg; try // if not ExistsGroup(groupName) then //若不判断,捕获异常也可以。 // createGroup; except ; end; readMsg; end; end.
注:
或IRedisClient接口中无ExecuteAndGetMatrix方法需要自行添加。
热门相关:天河大帝 恶明 史上第一宠婚:慕少的娇妻 都市狐仙养成记 都市狐仙养成记