• mormot当作内存数据库(缓存)使用


    mormot当作内存数据库(缓存)使用

    mormot的TSQLRestStorageInMemory可以作为内存数据库来使用。

    上图是在笔者4代I5笔记本上做的测试,增加10万记录,耗时:562毫秒。

    增加的数据如下图所示(默认是JSON,当然也可以存为二进制格式的):

    下面附上测试代码:

    /// <author>cxg 2018-9-17</author>
    /// mormot 数据缓存
    
    unit Unit1;
    
    interface
    
    uses
      SynCommons, SynDB, mORMot, mORMotDB, SynDBSQLite3, SynSQLite3Static, Winapi.Windows,
      Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
      Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ToolWin, Vcl.ComCtrls;
    
    type
      /// <summary>
      /// 缓存的记录
      /// </summary>
      TPerson = class(TSQLRecord)
      private
        FNo: integer;
        FName: string;
      published
        property No: Integer read FNo write FNo;
        property Name: string read FName write FName;
      end;
    
    type
      TForm1 = class(TForm)
        Label1: TLabel;
        edtId: TEdit;
        Label2: TLabel;
        edtName: TEdit;
        ToolBar1: TToolBar;
        btnAdd: TToolButton;
        btnAdds: TToolButton;
        btnUpdate: TToolButton;
        btnFind: TToolButton;
        btnDelete: TToolButton;
        btnDeleteAll: TToolButton;
        mmo1: TMemo;
        procedure btnAddClick(Sender: TObject);
        procedure FormCreate(Sender: TObject);
        procedure FormDestroy(Sender: TObject);
        procedure btnFindClick(Sender: TObject);
        procedure btnAddsClick(Sender: TObject);
        procedure btnUpdateClick(Sender: TObject);
        procedure btnDeleteAllClick(Sender: TObject);
        procedure btnDeleteClick(Sender: TObject);
      private
        { Private declarations }
        FMemDB: TSQLRestStorageInMemory;
        FModel: TSQLModel;
      public
        { Public declarations }
      end;
    
    var
      Form1: TForm1;
    
    implementation
    
    {$R *.dfm}
    
    procedure TForm1.btnAddClick(Sender: TObject);
    var
      LPerson: TPerson;
    begin
      try
        LPerson := TPerson.Create;
        LPerson.No := StrToInt(edtId.Text);
        LPerson.Name := edtName.Text;
        if FMemDB.Add(LPerson, True) = 0 then
        begin
          ShowMessage('增加失败');
        end
        else
        begin
          edtId.Clear;
          edtName.Clear;
          edtId.SetFocus;
        end;
      finally
        LPerson.Free;
      end;
    end;
    
    procedure TForm1.btnAddsClick(Sender: TObject);
    var
      a, b, c: Cardinal;
      i: Integer;
      LPerson: TPerson;
    begin
      a := GetTickCount;
      try
        LPerson := TPerson.Create;
    
        for i := 0 to 100000 do
        begin
          LPerson.no:= i;
          LPerson.Name := RandomGUID.ToString;
          if FMemDB.Add(LPerson, True) = 0 then
          begin
            mmo1.Lines.Add('失败: ' + i.ToString);
          end;
        end;
        b := GetTickCount;
        c := b - a;
        mmo1.Lines.Add('耗时: ' + c.ToString + ' 毫秒');
      finally
        LPerson.Free;
      end;
    end;
    
    procedure TForm1.btnDeleteAllClick(Sender: TObject);
    begin
      FMemDB.DropValues;
    end;
    
    procedure TForm1.btnDeleteClick(Sender: TObject);
    var
      LNo: string;
    begin
      LNo := '';
      if FMemDB.Delete(TPerson, LNo) then
      begin
        mmo1.Lines.Add('删除成功');
      end else
      begin
        mmo1.Lines.Add('删除失败');
      end;
    
    end;
    
    procedure TForm1.btnFindClick(Sender: TObject);
    var
      LPerson: TPerson;
    begin
      LPerson := TPerson.Create(FMemDB, 'Id=?', [edtId.Text]);
      if LPerson <> nil then
      begin
        edtId.Text := LPerson.no.ToString;
        edtName.Text := LPerson.Name;
      end;
    end;
    
    procedure TForm1.btnUpdateClick(Sender: TObject);
    var
      LPerson: TPerson;
    begin
      LPerson := TPerson.Create;
      LPerson.no := StrToInt(edtId.Text);
      LPerson.Name := edtName.Text;
    
      if FMemDB.Update(LPerson) then
      begin
        mmo1.Lines.Add('更新成功: ' + LPerson.no.ToString);
      end
      else
      begin
        mmo1.Lines.Add('更新失败: ' + LPerson.no.ToString);
      end;
      LPerson.Free;
    end;
    
    procedure TForm1.FormCreate(Sender: TObject);
    begin
      FModel := TSQLModel.Create([TPerson]);
      FMemDB := TSQLRestStorageInMemory.Create(TPerson, nil, 'test.db');
    end;
    
    procedure TForm1.FormDestroy(Sender: TObject);
    begin
      FModel.Free;
      FMemDB.Free;
    end;
    
    end.
    

      

  • 相关阅读:
    信号、事件与状态
    信号处理机制的范式分析
    三寒两倒七分饱
    血热的人吃什么好
    消息、信息与信号的区别
    Busy waiting
    事件的处理机制:单播、广播、链式路由、职责链。
    事件处理:pull与push
    响应式编程
    类、组件、人机交互
  • 原文地址:https://www.cnblogs.com/hnxxcxg/p/9660627.html
Copyright © 2020-2023  润新知