Lazarus项目使用fpRedis操作Redis

个人备忘 2023-04-15 944 次浏览 次点赞

在Delphi下可以使用开源控件delphiredisclient进行Redis的操作,使用比较简单这里就不说了。那么Lazarus下目前只找到一个更新停止于6年前的一个开源项目fpRedis(基于12年前开源的redis_client.fpc),虽然老点但是能用。
gitgub地址https://github.com/MFernstrom/fpRedis

我自己修改的那个版本在https://github.com/mestars/fpredis
具体使用示例如下:

引用单元rd_protocol, rd_commands, rd_types

procedure TForm1.BtnConnectClick(Sender: TObject);
var
  RedisConnection: TRedisConnection;
  IO: TRedisIO; 
  return: TRedisReturnType;   
begin
  IO := TRedisIO.Create; 
  IO.Connect;
  RedisConnection := TRedisConnection.Create(IO);
  //RedisConnection.Logger := Log;

  return := RedisConnection.Ping;
  print_return('ping');

  return := RedisConnection.Auth('');
  print_return('Auth');

  return := RedisConnection.Select(0);
  print_return('Select');

  return := RedisConnection.Echo('Hello World');
  print_return('Echo');

  return := RedisConnection.Echo('"Hello"W"orld');
  print_return('Echo');
  // Test Last !
  return := RedisConnection.Quit;
  print_return('Quit');

  if IO.Connected then
    IO.Disconnect;
  RedisConnection.Free;
end; 
procedure TForm1.BtnServerClick(Sender: TObject);
var
      server: TRedisServer;
      RedisConnection :TRedisConnection;
      IO: TRedisIO; 
      return: TRedisReturnType;   
begin
      IO := TRedisIO.Create;          
      IO.Connect;
      RedisConnection := TRedisConnection.Create(IO);
      //验证密码
      return := RedisConnection.Auth('mypassword');
      print_return('Auth');
      //选中库
      return := RedisConnection.Select(0);   
      print_return('Select');

      server := TRedisServer.Create(IO);
      server.Socket.Connect('127.0.0.1','6379');
      server.Socket.Listen;
     //自己扩展改写的(server.send_command2(const command: string): TRedisReturnType)
     //比如下面这个其实是调用的server.send_command2('SET',['mykey','myvalue'])
      return :=server.setValue('mykey','myvalue');
      print_return('config SET mykey');
    
      //return :=server.rd_debug('SET','mykey','myvalue');
      //print_return('config SET mestars');
      
      //比如下面这个其实是调用的server.send_command2('GET',['mykey'])
       return :=server.getValue('mykey');
       print_return('GET mestars');
    
      if IO.Connected then
        IO.Disconnect;
      RedisConnection.Free;
      server.Free; 
      IO.Free;  
 end;

总之就是原来的开源控件实现了发送命令的接口,结合着redis的命令就可以自己自由扩展使用了.

接上,今天又发现一个新的fpredis,github地址https://github.com/isyscore/fpredis,使用起来相对于以前那个的fpredis简单了不少,有点像delphiRedisClient了,使用示例如下:

var
  api: TFPRedis;
  key: String = 'mykey';
  b: Boolean;
  s: String;
begin
  api := TFPRedis.Create('127.0.0.1', 6379, 'mypassword');
  b := api.Connect();
  WriteLn('Connected: ', b);

  b := api.&Set(key, 'myvalue');
  WriteLn('Set: ', b);

  b := api.Exists(key);
  WriteLn('Exists: ', b);

  s := api.Get(key);
  WriteLn('Get: ', s);

  api.Free;
end.

                              

作者明确:All Unix-bases OS are supported. 经测试发现在windows下编译确实有一个问题:

project1.lpr(24,1) Error: Can't open object file: libhiredis_x86-64_win64.a

缺少支持Windows的库,控件的staticlibs目录里只有libhiredis_x86-64_darwin.a和libhiredis_x86-64_linux.a....暂时还不知道哪里能找到支持Windows的那个。


本文由 王守红 创作,采用 知识共享署名 3.0,可自由转载、引用,但需署名作者且注明文章出处。

还不快抢沙发

添加新评论