Skip to content

Commit

Permalink
LDEV-4818 add sessionCommit() function
Browse files Browse the repository at this point in the history
  • Loading branch information
zspitzer committed Feb 1, 2025
1 parent cde5d82 commit 167a315
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package lucee.runtime.functions.system;

import lucee.runtime.PageContext;
import lucee.runtime.PageContextImpl;
import lucee.runtime.exp.PageException;
import lucee.runtime.ext.function.Function;

public class SessionCommit implements Function {
private static final long serialVersionUID = -2243745577257724777L;

public static String call(PageContext pc) throws PageException {
((PageContextImpl) pc).sessionScope().touchAfterRequest(pc);
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,16 @@ public IKStorageValue loadData(PageContext pc, String appName, String name, Stri
Object val = cache.getValue(key, null);
if (val instanceof byte[][]) {
ScopeContext.info(log,
"load existing data from cache [" + name + "] to create " + strType + " scope for " + pc.getApplicationContext().getName() + "/" + pc.getCFID());
"Load existing byte data from cache [" + name + "] to create " + strType + " scope for " + pc.getApplicationContext().getName() + "/" + pc.getCFID());
return new IKStorageValue((byte[][]) val);
}
else if (val instanceof IKStorageValue) {
ScopeContext.info(log,
"load existing data from cache [" + name + "] to create " + strType + " scope for " + pc.getApplicationContext().getName() + "/" + pc.getCFID());
"Load existing data from cache [" + name + "] to create " + strType + " scope for " + pc.getApplicationContext().getName() + "/" + pc.getCFID());
return (IKStorageValue) val;
}
else {
ScopeContext.info(log, "create new " + strType + " scope for " + pc.getApplicationContext().getName() + "/" + pc.getCFID() + " in cache [" + name + "]");
ScopeContext.info(log, "Create new [" + strType + "] scope for [" + pc.getApplicationContext().getName() + "/" + pc.getCFID() + "] in cache [" + name + "]");
}
return null;
}
Expand All @@ -70,6 +70,7 @@ else if (existingVal != null) {
cache.remove(key);
}
}
ScopeContext.info(log, "Store scope for [" + pc.getApplicationContext().getName() + "/" + pc.getCFID() + "] in cache [" + name + "]");
}
catch (Exception e) {
ScopeContext.error(log, e);
Expand Down
11 changes: 10 additions & 1 deletion core/src/main/java/resource/fld/core-base.fld
Original file line number Diff line number Diff line change
Expand Up @@ -12646,7 +12646,16 @@ You can find a list of all available timezones in the Lucee administrator (Setti
<type>string</type>
</return>
</function>


<!-- SessionCommit -->
<function>
<name>SessionCommit</name>
<class>lucee.runtime.functions.system.SessionCommit</class>
<description>Force saving the session to storage, useful when sessionCluster is enabled.</description>
<return>
<type>void</type>
</return>
</function>

<!-- SessionStartTime -->
<function>
Expand Down
20 changes: 17 additions & 3 deletions test/tickets/LDEV2135.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ component extends="org.lucee.cfml.test.LuceeTestCase" labels="session" {

function run( testResults , testBox ) {
describe( "Test suite for LDEV-2135 using memory", function() {

//beforeEach(function (currentSpec, data){ _beforeEach(currentSpec, data); });

it( title='thread looses session variables - sessionCluster=false', body=function( currentSpec ) {
test( {sessionCluster: false, sessionStorage: "memory"} );
});
Expand All @@ -12,21 +15,27 @@ component extends="org.lucee.cfml.test.LuceeTestCase" labels="session" {
});

describe( title="Test suite for LDEV-2135 using redis", skip=skipRedis(), body=function() {

//beforeEach(function (currentSpec, data){ _beforeEach(currentSpec, data); });

it( title='thread looses session variables - redis- sessionCluster=false', body=function( currentSpec ) {
test( {sessionCluster: false, sessionStorage: "redis"} );
});

it( title='thread looses session variables - redis - sessionCluster=true', skip=true, body=function( currentSpec ) {
it( title='thread looses session variables - redis - sessionCluster=true', body=function( currentSpec ) {
test( {sessionCluster: true, sessionStorage: "redis"} );
});
});

describe( title="Test suite for LDEV-2135 using memcached", skip=skipMemcached(), body=function() {

//beforeEach(function (currentSpec, data){ _beforeEach(currentSpec, data); });

it( title='thread looses session variables - memcached -sessionCluster=false', body=function( currentSpec ) {
test( {sessionCluster: false, sessionStorage: "memcached"} );
});

it( title='thread looses session variables - memcached -sessionCluster=true', skip=true, body=function( currentSpec ) {
it( title='thread looses session variables - memcached -sessionCluster=true', body=function( currentSpec ) {
test( {sessionCluster: true, sessionStorage: "memcached"} );
});
});
Expand All @@ -44,7 +53,7 @@ component extends="org.lucee.cfml.test.LuceeTestCase" labels="session" {
cfid: first.session.cfid,
cftoken: first.session.cftoken
};

// systemOutput("-- before secondRequest.cfm", true);
var second = _InternalRequest(
template : "#uri#/cfml-session/secondRequest.cfm",
url: args,
Expand Down Expand Up @@ -76,4 +85,9 @@ component extends="org.lucee.cfml.test.LuceeTestCase" labels="session" {
private function skipMemcached(){
return (structCount(server.getTestService( "memcached" )) eq 0);
}

private function _beforeEach(currentSpec, data){
systemOutput("", true);
systemOutput(currentspec, true);
}
}
6 changes: 5 additions & 1 deletion test/tickets/LDEV2135/cfml-session/secondRequest.cfm
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
<cfscript>
//systemOutput(session.toJson(), true);
if (!structKeyExists(session, "ldev3125")){
//systemOutput(session.toJson(), true);
throw "key session.ldev3125 missing";
}
//systemOutput(session.ldev3125.toJson(), true);
echo(session.ldev3125.toJson());
</cfscript>
9 changes: 6 additions & 3 deletions test/tickets/LDEV2135/cfml-session/testThreadSession.cfm
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<cfscript>
//systemOutput(url.toJson(), true);
session.ldev3125 = {};
session.ldev3125.sessionCluster = url.sessionCluster;
session.ldev3125.start = 'survived';
Expand All @@ -13,15 +14,17 @@
thread name="#name#" {
try {
ArrayAppend( session.ldev3125.threads, thread.name );
sleep( 10 );
// sleep( 10 ); // might need to be 1000
throw(type="blah", message="boom");
} catch(any e) {
writedump(session.ldev3125);
dump(session.ldev3125);
}
}
}
session.ldev3125.beforeJoin = 'hello';
thread action="join" name="#_threads.toList()#";
session.ldev3125.afterJoin = 'goodbye';
echo( session.ldev3125.toJson() );
//systemOutput(session.toJson(), true);
sessionCommit();
</cfscript>

0 comments on commit 167a315

Please sign in to comment.