Skip to content

Commit

Permalink
Session engine realigned
Browse files Browse the repository at this point in the history
  • Loading branch information
danieleteti committed Jan 16, 2025
1 parent 41a5c29 commit d4e0e6b
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 17 deletions.
13 changes: 13 additions & 0 deletions samples/sessions/AppControllerU.pas
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ TApp1MainController = class(TMVCController)
[MVCHTTPMethod([httpGET])]
procedure Index;

[MVCPath('/inc')]
[MVCHTTPMethod([httpGET])]
function DoInc: String;

[MVCPath('/started')]
[MVCHTTPMethod([httpGET])]
function SessionStarted: Boolean;
Expand All @@ -33,8 +37,17 @@ TApp1MainController = class(TMVCController)

implementation

uses
System.SysUtils;

{ TApp1MainController }

function TApp1MainController.DoInc: String;
begin
Session['value'] := (StrToIntDef(Session['value'], 0) + 1).ToString;
Result := Session['value'];
end;

procedure TApp1MainController.DoLogin(username: string);
begin
Session['username'] := username;
Expand Down
2 changes: 1 addition & 1 deletion samples/sessions/SessionSample.dpr
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ procedure RunServer(APort: Integer);
var
LServer: TIdHTTPWebBrokerBridge;
begin
LogI(Format('Starting HTTP Server or port %d', [APort]));
LogI(Format('Listening on http://localhost:%d', [APort]));
LServer := TIdHTTPWebBrokerBridge.Create(nil);
try
LServer.DefaultPort := APort;
Expand Down
2 changes: 1 addition & 1 deletion samples/sessions/SessionSample.dproj
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
<Icon_MainIcon>$(BDS)\bin\delphi_PROJECTICON.ico</Icon_MainIcon>
<DCC_Namespace>System;Xml;Data;Datasnap;Web;Soap;Vcl;Vcl.Imaging;Vcl.Touch;Vcl.Samples;Vcl.Shell;$(DCC_Namespace)</DCC_Namespace>
<DCC_DcuOutput>.\$(Platform)\$(Config)</DCC_DcuOutput>
<DCC_ExeOutput>.\$(Platform)\$(Config)</DCC_ExeOutput>
<DCC_ExeOutput>bin</DCC_ExeOutput>
<DCC_E>false</DCC_E>
<DCC_N>false</DCC_N>
<DCC_S>false</DCC_S>
Expand Down
11 changes: 10 additions & 1 deletion samples/sessions_on_database/WebModuleUnit1.pas
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,16 @@ procedure TWebModule1.WebModuleCreate(Sender: TObject);
Config[TMVCConfigKey.SessionType] := 'dbactiverecord';
end);
MVC.AddController(TApp1MainController);
MVC.AddMiddleware(TMVCActiveRecordMiddleware.Create('mydb'))


{ You need to have a running database engine and a correct configuration in the FDConnectionDefs.ini file.
Also you need to create a dmvc_sessions table using one of SQL DDL script available at }

{ Use the following line for MySQL database connection configured in FDConnectionDefs.ini}
//MVC.AddMiddleware(TMVCActiveRecordMiddleware.Create('mysqldb'));

{ Use the following line for FirebirdSQL database connection configured in FDConnectionDefs.ini}
MVC.AddMiddleware(TMVCActiveRecordMiddleware.Create('firebirddb'));
end;

end.
2 changes: 1 addition & 1 deletion sources/MVCFramework.Serializer.HTML.pas
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ procedure TMVCHTMLSerializer.DeserializeObject(const ASerializedObject: string;

procedure TMVCHTMLSerializer.RaiseNotImplemented;
begin
raise EMVCException.Create('Not Implemented');
raise EMVCException.Create('Not Implemented for ' + ClassName);
end;

procedure TMVCHTMLSerializer.RegisterTypeSerializer(const ATypeInfo: PTypeInfo;
Expand Down
13 changes: 6 additions & 7 deletions sources/MVCFramework.Session.Database.pas
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ procedure TMVCWebSessionDatabase.InsertIntoDB;
begin
MarkAsUsed;
fSessionData.Insert;
LogW('InsertIntoDB');
end;

procedure TMVCWebSessionDatabase.InternalApplyChanges;
Expand All @@ -159,19 +158,19 @@ procedure TMVCWebSessionDatabase.MarkAsUsed;
begin
if fSessionData.SessionExpiration.HasValue then
begin
lFutureExpiration := Now() + OneMinute * Timeout;
if FormatDateTime('yyyymmddhhnn', lFutureExpiration) > FormatDateTime('yyyymmddhhnn', fSessionData.SessionExpiration) then
lFutureExpiration := RecodeMillisecond(RecodeSecond(Now() + OneMinute * fTimeout, 0), 0);
if lFutureExpiration > fSessionData.SessionExpiration.Value then
begin
inherited;
fSessionData.RefreshSessionExpiration;
ExpirationTimeStamp := fSessionData.SessionExpiration;
end;
end;
end;

procedure TMVCWebSessionDatabase.UpdateToDB;
begin
fSessionData.Update(True);
LogW('UpdateToDB');
end;

procedure TMVCWebSessionDatabase.SetItems(const AKey, AValue: string);
Expand All @@ -196,7 +195,6 @@ class function TMVCWebSessionDatabase.TryFindSessionID(const aSessionID: String)
lSess: TMVCSessionActiveRecord;
begin
inherited;
LogW('ReadFromDB - EXISTS');
lSess := TMVCActiveRecord.SelectOneByRQL<TMVCSessionActiveRecord>(Format('eq(session_id, "%s")', [aSessionID]), False);
try
Result := Assigned(lSess);
Expand Down Expand Up @@ -234,7 +232,6 @@ class function TMVCWebSessionDatabase.CreateFromSessionID(const aSessionId: stri
lSessDB: TMVCSessionActiveRecord;
begin
Result := nil;
LogW('ReadFromDB');
lSessDB := TMVCActiveRecord.GetByPK<TMVCSessionActiveRecord>(aSessionId, False);
if lSessDB <> nil then
begin
Expand Down Expand Up @@ -287,7 +284,9 @@ procedure TMVCSessionActiveRecord.RefreshSessionExpiration;
if fTimeout = 0 then
fSessionExpiration.Clear
else
fSessionExpiration := RecodeSecond(Now() + OneMinute * fTimeout, 0);
begin
fSessionExpiration := RecodeMillisecond(RecodeSecond(Now() + OneMinute * fTimeout, 0), 0);
end;
end;

procedure TMVCSessionActiveRecord.SetTimeout(const Value: Cardinal);
Expand Down
14 changes: 8 additions & 6 deletions sources/MVCFramework.Session.pas
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ EMVCSession = class(EMVCException)
TMVCWebSession = class abstract
strict private
fChanged: Boolean;
fExpirationTimeStamp: TDateTime;
fExpirationTimeStamp: NullableTDateTime;
protected
fSessionId: string;
fTimeout: UInt64;
Expand Down Expand Up @@ -258,7 +258,7 @@ procedure TMVCWebSession.InternalApplyChanges;
function TMVCWebSession.IsExpired: Boolean;
begin
if ExpirationTimeStamp.HasValue then
Result := ExpirationTimeStamp.Value < RecodeSecond(Now(), 0)
Result := ExpirationTimeStamp.Value < RecodeMilliSecond(RecodeSecond(Now(), 0), 0)
else
Result := False;
end;
Expand Down Expand Up @@ -297,10 +297,12 @@ procedure TMVCWebSession.StopSession;

procedure TMVCWebSession.RefreshSessionExpiration;
begin
fExpirationTimeStamp := RecodeSecond(Now() + OneMinute * fTimeout, 0);
if fTimeout > 0 then
fExpirationTimeStamp := RecodeMilliSecond(RecodeSecond(Now() + OneMinute * fTimeout, 0), 0)
else
fExpirationTimeStamp.Clear;
end;


function TMVCWebSession.ToString: string;
begin
Result := String.Join(',', Keys);
Expand All @@ -322,8 +324,8 @@ function TMVCWebSessionMemory.Clone: TMVCWebSession;
begin
lMemSess := TMVCWebSessionMemory.Create;
try
lMemSess.FSessionId := Self.FSessionId;
lMemSess.FTimeout := Self.FTimeout;
lMemSess.fSessionId := SessionId;
lMemSess.Timeout := Timeout;
for var lItem in Self.Data do
begin
lMemSess.Data.Add(lItem.Key, lItem.Value);
Expand Down

0 comments on commit d4e0e6b

Please sign in to comment.