#1 2013-12-29 01:34:37

richard6688
Member
Registered: 2011-04-05
Posts: 31

SQLite cache

hi,
  When I want to make some test, duto cache, the time is varied. Try to set Server UseCache to False failed. It seems not ready for disable cache.
  Is there a plan to make this feature ready?

Offline

#2 2013-12-29 16:25:21

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,242
Website

Re: SQLite cache

There are several levels of cache.
See the SAD 1.18 pdf about that.
Or the older (probably out of date) http://blog.synopse.info/post/2012/02/14/ORM-cache

Offline

#3 2013-12-30 00:23:36

richard6688
Member
Registered: 2011-04-05
Posts: 31

Re: SQLite cache

What I mean is Cache in server side, the class of TSQLDataBase, which has a public property UseCache

  TSQLDataBase = class
   ......
    /// if this property is set, all ExecuteJSON() responses will be cached
    // - cache is flushed on any write access to the DB (any not SELECT statement)
    // - cache is consistent only if ExecuteJSON() Expand parameter is constant
    // - cache is used by TSQLDataBase.ExecuteJSON() and TSQLTableDB.Create()
    property UseCache: boolean read GetUseCache write SetUseCache;

   
   The class seems not check the fCache=nil, so, cannot set this property to false.

Offline

#4 2013-12-30 08:40:05

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,242
Website

Re: SQLite cache

fCache=nil is checked in [get]UseCache:

function TSQLDataBase.GetUseCache: boolean;
begin
  result := (Self<>nil) and (fCache<>nil);
end;

procedure TSQLDataBase.SetUseCache(const Value: boolean);
begin
  if self<>nil then
    if Value<>UseCache then
      if Value then
        fCache := TSynCache.Create(16384*1024,true) else
        FreeAndNil(fCache);
end;

Code sounds correct here.

Offline

#5 2013-12-30 09:57:58

richard6688
Member
Registered: 2011-04-05
Posts: 31

Re: SQLite cache

Please ref

function TSQLDataBase.LockJSON(const aSQL: RawUTF8; aResultCount: PPtrInt): RawUTF8;
begin
  if self=nil then
    exit; // avoid GPF in case of call from a static-only server
  EnterCriticalSection(fLock); // cache access is also protected by fLock
  try
    if isSelect(pointer(aSQL)) then begin
      result := fCache.Find(aSQL,aResultCount); // try to get JSON result from cache
      if result<>'' then begin

fCache not checked for nil

Offline

#6 2013-12-30 14:38:53

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,242
Website

Re: SQLite cache

Please read the code!

function TSynCache.Find(const aKey: RawUTF8; aResultTag: PPtrInt): RawUTF8;
var added: boolean;
begin
  if (self=nil) or (aKey='') then begin
    fFindLastAddedIndex := -1;
    result := '';
  end else begin
  (...)

So fCache is checked for nil !

Offline

#7 2013-12-31 02:42:46

richard6688
Member
Registered: 2011-04-05
Posts: 31

Re: SQLite cache

Sorry, I don't know we can call the method of class which is nil. Usally, it's not safe. Thank you for knowing this.

But here, do have the problem, you assigned value to fFindLastAddedIndex  which get av.
the line

fFindLastAddedIndex := -1;

shall be removed.

Offline

#8 2013-12-31 10:43:37

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,242
Website

Re: SQLite cache

richard6688 wrote:

Sorry, I don't know we can call the method of class which is nil. Usally, it's not safe. Thank you for knowing this.

It is always safe, unless the method is virtual.
If you come from a Java background, where every method is virtual, you can easily be confused.
But in Delphi, it is pretty valid and convenient to call a non virtual method with self=nil.


richard6688 wrote:

But here, do have the problem, you assigned value to fFindLastAddedIndex  which get av.
the line

fFindLastAddedIndex := -1;

shall be removed.

AFAIK fFindLastAddedIndex use is just correct, and will never get an access violation.
I've added some comments to help you understand the algorithm.
See http://synopse.info/fossil/info/19ac0042ff

Do not make assumption: use the debugger to find out what is happenning if it is not clear what the code does.
And provide some code to reproduce any problem, if there is any.

Offline

#9 2013-12-31 14:04:13

richard6688
Member
Registered: 2011-04-05
Posts: 31

Re: SQLite cache

I certainly not make assumption, I use XE to trace here.  fFindLastAddedIndex  is not accessible. assigned value to fFindLastAddedIndex  do get AV.

this problem just occurred by set Usecache=False. maybe late I can give a code to test it.

anyway, here wish you happy new year!

Happy new year to erverybody here!

Offline

#10 2013-12-31 14:28:16

richard6688
Member
Registered: 2011-04-05
Posts: 31

Re: SQLite cache

test code, use mormot sample, 02 - Embedded SQLite3 ORM

program Project02;

uses
  Forms,
  SysUtils,
  mORMot,
  mORMotSQLite3, SynSQLite3Static,
  Unit1 in '..\01 - In Memory ORM\Unit1.pas' {Form1},
  SampleData in '..\01 - In Memory ORM\SampleData.pas';

{$R *.res}

begin
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Form1.Caption := ' Sample 02 - Embedded SQLite3 ORM';
  Form1.Database := TSQLRestServerDB.Create(Form1.Model,
    ChangeFileExt(paramstr(0),'.db3'));
  TSQLRestServerDB(Form1.Database).CreateMissingTables(0);
  TSQLRestServerDB(Form1.Database).DB.UseCache:=false;    //add this line to disable cache
  Application.Run;
end.

write ok, but read av occurred

Offline

#11 2013-12-31 15:24:40

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,242
Website

Re: SQLite cache

Yes!
With a simple test program, it was easy to find what you meant, and finally fix the issue!
See http://synopse.info/fossil/info/7f89ac7467

Thanks for the feedback!
smile

Offline

Board footer

Powered by FluxBB