Charles Robertson
01/23/2023, 11:43 AMcacheRegionNew()
cacheRegionExists()
Because, a web admin password would be required in Lucee, otherwise.
Does anyone know a way to emulate the functionality of:
cacheRegionRemove()
https://docs.lucee.org/reference/functions/cacheregionremove.html
In Lucee, because this is the third & final method that requires a web admin password? I don't want to use the administrator GUI. This needs to be done programmatically?
I was thinking perhaps something along the lines of:
variables.szCacheName = "CONFIGSETTINGS";
variables.szApplicationName = "LA-EPORTFOLIO";
variables.szPropertiesCachename = ucase(variables.szApplicationName) & "_" & ucase(variables.szCacheName);
variables.stuCacheProps = {
CLEARONFLUSH = true,
DISKPERSISTENT = true,
ETERNAL = true,
MAXELEMENTSINMEMORY = 1000000,
MAXELEMENTSONDISK = 0,
OBJECTTYPE = "OBJECT",
OVERFLOWTODISK = true,
STATISTICS = true,
TIMETOLIVESECONDS = 0,
TIMETOIDLESECONDS = 0
};
variables.cacheExists = StructKeyExists(this,"cache") AND StructKeyExists(this.cache,"connections") AND StructKeyExists(this.cache.connections,variables.szPropertiesCachename) ? true : false;
if(NOT variables.cacheExists){
this.cache.connections[variables.szPropertiesCachename] = {
class: 'org.lucee.extension.cache.eh.EHCache',
custom: variables.stuCacheProps,
default: 'object'
};
this.cache.object = 'default';
}
variables.isDeleted = StructDelete( this.cache.connections, variables.szPropertiesCachename, true );
But, would this actually destroy the cache connection?zackster
01/23/2023, 11:48 AM<cfapplication action="update" caches={what ever}>Charles Robertson
01/23/2023, 11:53 AM<cfapplication action="update" cache="#{
connections: {
'#variables.szPropertiesCachename#': null
}
}#">
https://docs.lucee.org/guides/cookbooks/application-context-update.html
Do you think I can use the <cfapplication /> tag anywhere? I mean like inside a CFC or template?Charles Robertson
01/23/2023, 12:00 PMcfapplication(action="update", cache={
connections[variables.szPropertiesCachename]: null
});zackster
01/23/2023, 12:01 PMapplication action="update" etcCharles Robertson
01/23/2023, 7:53 PMFunctions
The following functions, from Adobe CFML, are not supported by Lucee:
cacheregionexists
cacheregionnew
cacheregionremove
ā¦
Is this true? The reason I ask, is I am having real trouble trying to get:
cacheRegionExists()
cacheRegionRemove()
To work? I mean I can initialise a cache in Lucee, but when I try and use:
cacheRegionExists(cacheName, webAdminPassword)
It always return false? š¤Charles Robertson
01/24/2023, 11:31 AMCacheRegionExists(), I have written this:
<cffunction name="CacheRegionExists" access="public" returntype="boolean" output="false" hint="Does the cache region exist in Lucee.">
<cfargument name="cacheName" default="" />
<cfset var local = {} />
<cfset local.cacheRegionExists = false />
<cfset local.cacheNamesProperties = CacheGetProperties(arguments.cacheName) />
<cfif IsArray(local.cacheNamesProperties) AND ArrayLen(local.cacheNamesProperties) AND IsStruct(local.cacheNamesProperties[1]) AND StructKeyExists(local.cacheNamesProperties[1], "name") AND CompareNoCase(local.cacheNamesProperties[1].name, arguments.cacheName) EQ 0>
<cfset local.cacheRegionExists = true />
</cfif>
<cfreturn local.cacheRegionExists />
</cffunction>
I am just not 100% sure that this is robust enough?
I have even added the exact same Cache in the Administrator GUI and the BIF CacheRegionExists(), still doesn't return true?Charles Robertson
01/24/2023, 11:37 AMzackster
01/24/2023, 11:37 AMCharles Robertson
01/24/2023, 11:37 AMzackster
01/24/2023, 11:37 AMCharles Robertson
01/24/2023, 11:42 AMzackster
01/24/2023, 11:44 AMCharles Robertson
01/24/2023, 12:27 PMCacheRegionExists()
In ACF it works perfectly, but in Lucee, it always returns false.
I am happy to write a bug report š
Do you have the link for bug reports in Lucee. Thanks...zackster
01/24/2023, 12:30 PMzackster
01/24/2023, 12:31 PMCharles Robertson
01/24/2023, 12:34 PMGetApplicationSettings()
So, that's great. It means I was moving in the right direction! šzackster
01/24/2023, 12:35 PMzackster
01/24/2023, 12:35 PMzackster
01/24/2023, 12:36 PMCharles Robertson
01/24/2023, 12:45 PMHow do we remove cache regions in Lucee?
I have tried deleting the cache property from GetApplicationSettings(), but I have a feeling this is a read-only struct?
Can you think of anyway, we can emulate ACFs CacheRegionRemove(), in Lucee?
I did try:
<cfapplication
action="update"
caches=""
/>
But this had no effect?zackster
01/24/2023, 12:51 PMgetApplicationSettings and do an application action=update cache=<value from get application settings>zackster
01/24/2023, 12:52 PMCharles Robertson
01/24/2023, 1:20 PMGetApplicationSettings()
Like:
<cffunction name="GetCacheLuceeApplication" access="public" returntype="any" output="false" hint="Get cache region in Lucee, using application struct">
<cfargument name="szPropertiesCachename" default="" />
<cfset var local = {} />
<cfset local.cacheConnection = {} />
<cfset local.applicationSettings = GetApplicationSettings() />
<cfif StructKeyExists(local.applicationSettings, "cache") AND StructKeyExists(local.applicationSettings.cache, "connections") AND StructKeyExists(local.applicationSettings.cache.connections, arguments.szPropertiesCachename)>
<cfset local.cacheConnection = local.applicationSettings.cache.connections[arguments.szPropertiesCachename] />
</cfif>
<cfreturn local.cacheConnection />
</cffunction>
<cfapplication action="update" cache="#GetCacheLuceeApplication('someCacheName')#" />
Doesn't that update the application's cache with the current value?
How does this delete the cache?
Unfortunately, there is no:
<cfapplication action="delete" cache="#GetCacheLuceeApplication('someCacheName')#" />
I tried doing:
<cfapplication action="update" cache="null" />
And, again, this failed to work?zackster
01/24/2023, 3:05 PMCharles Robertson
01/24/2023, 3:06 PMCharles Robertson
01/24/2023, 5:41 PMzackster
01/24/2023, 5:49 PMCharles Robertson
01/24/2023, 5:49 PMCharles Robertson
01/24/2023, 6:24 PM<cfcomponent output="false" displayname="Application">
<cfscript>
variables.nSessionTimeout = CreateTimeSpan(0, 0, 30, 0);
request.szSessionTimeoutSecs = DateDiff("s", 0, variables.nSessionTimeout);
variables.nServerName = "lucee539166";
this.name = variables.nServerName;
this.applicationTimeout = CreateTimeSpan(30, 0, 0, 0);
this.serverSideFormValidation = true;
this.sessionManagement = true;
this.setClientCookies = true;
this.scriptProtect = "all";
this.timeout = 60;
this.sessionTimeout = variables.nSessionTimeout;
this.clientManagement = true;
this.clientStorage = "cookie";
this.loginStorage = "session";
variables.szCacheName = "CONFIGSETTINGS";
variables.szApplicationName = "LA-EPORTFOLIO";
variables.szPropertiesCachename = ucase(variables.szApplicationName) & "_" & ucase(variables.szCacheName);
variables.stuCacheProps = {
CLEARONFLUSH = true,
DISKPERSISTENT = true,
ETERNAL = true,
MAXELEMENTSINMEMORY = 1000000,
MAXELEMENTSONDISK = 0,
OBJECTTYPE = "OBJECT",
OVERFLOWTODISK = true,
STATISTICS = true,
TIMETOLIVESECONDS = 0,
TIMETOIDLESECONDS = 0
};
variables.cacheExists = StructKeyExists(this,"cache") AND StructKeyExists(this.cache,"connections") AND StructKeyExists(this.cache.connections,variables.szPropertiesCachename) ? true : false;
if(NOT variables.cacheExists){
this.cache.connections[variables.szPropertiesCachename] = {
class: 'org.lucee.extension.cache.eh.EHCache',
custom: variables.stuCacheProps,
default: 'object'
};
this.cache.object = 'default';
}
</cfscript>
<cffunction name="onApplicationEnd" access="public" output="false" returnType="void">
<cfargument name="applicationScope" type="any" required="true" />
<cfreturn true />
</cffunction>
<cffunction name="onApplicationStart" access="public" output="false" returnType="boolean">
<cfscript>
application.szCache = variables.cache;
return true;
</cfscript>
</cffunction>
<cffunction name="onRequestEnd" access="public" output="false" returntype="boolean">
<cfargument name="targetPage" type="string" required="true" />
<cfreturn true />
</cffunction>
<cffunction name="onRequestStart" access="public" output="false" returntype="boolean">
<cfargument name="targetPage" type="string" required="true" />
<cfreturn true />
</cffunction>
<cffunction name="onRequest">
<cfargument name="targetPage" type="String" required="true" />
<cfscript>
request.nServerName = variables.nServerName;
request.szPropertiesCachename = variables.szPropertiesCachename;
request.szCache = variables.szCache;
if(!(StructKeyExists(application, "szCache")) OR StructKeyExists(url, "appreload")){
application.szCache = variables.szCache;
}
</cfscript>
<cfinclude template="#arguments.targetPage#">
</cffunction>
</cfcomponent>
And then once it has been created, I try this in a template. These methods are actually in components, but I have put everything on one page, for this demo:
index.cfm
<cffunction name="CacheApplicationExistsLucee" access="public" returntype="boolean" output="false" hint="Does the application cache region exist in Lucee">
<cfargument name="szPropertiesCachename" default="" />
<cfset var local = {} />
<cfset local.szPropertiesCachename = arguments.szPropertiesCachename />
<cfset local.cacheApplicationExists = false />
<cftry>
<cfset local.applicationSettings = GetApplicationSettings() />
<cfif StructKeyExists(local.applicationSettings, "cache") AND StructKeyExists(local.applicationSettings.cache, "connections") AND StructKeyExists(local.applicationSettings.cache.connections, arguments.szPropertiesCachename)>
<cfset local.cacheApplicationExists = true />
</cfif>
<cfcatch>
<cfset local.cacheApplicationExists = true />
</cfcatch>
</cftry>
<cfreturn local.cacheApplicationExists />
</cffunction>
<cffunction name="CacheRegionExistsLucee" access="public" returntype="boolean" output="false" hint="Does the cache region exist in Lucee">
<cfargument name="szPropertiesCachename" default="" />
<cfset var local = {} />
<cfset local.szPropertiesCachename = arguments.szPropertiesCachename />
<cfset local.cacheRegionExists = false />
<cftry>
<cfset local.cacheNamesProperties = CacheGetProperties(local.szPropertiesCachename) />
<cfif IsArray(local.cacheNamesProperties) AND ArrayLen(local.cacheNamesProperties) AND IsStruct(local.cacheNamesProperties[1]) AND StructKeyExists(local.cacheNamesProperties[1], "name") AND CompareNoCase(local.cacheNamesProperties[1].name, local.szPropertiesCachename) EQ 0>
<cfset local.cacheRegionExists = true />
</cfif>
<cfcatch>
<cfset local.cacheRegionExists = true />
</cfcatch>
</cftry>
<cfreturn local.cacheRegionExists />
</cffunction>
<cffunction name="CacheRegionRemoveLucee" access="public" returntype="boolean" output="false" hint="Removes the cache region in Lucee">
<cfargument name="szPropertiesCachename" default="" />
<cfset var local = {} />
<cfset local.isDeleted = false />
<cftry>
<!--- I have also used CacheApplicationExistsLucee(), but it makes no difference --->
<cfset local.cacheRegionExists = CacheRegionExistsLucee(arguments.szPropertiesCachename) />
<cfif local.cacheRegionExists>
<!--- Note: remove only the named cache connection and the cache object, if it belongs to this cache name --->
<cfset local.applicationSettings = GetApplicationSettings() />
<cfset local.isConnectionDeleted = false />
<cfset local.isConnectionDeleted = StructDelete(local.applicationSettings.cache.connections, arguments.szPropertiesCachename, true) />
<cfset local.isObjectDeleted = false />
<cfif StructKeyExists(local.applicationSettings, "cache") AND StructKeyExists(local.applicationSettings.cache, "object") AND CompareNoCase(local.applicationSettings.cache.object, arguments.szPropertiesCachename) EQ 0>
<cfset local.isObjectDeleted = StructDelete(local.applicationSettings.cache, "object", true) />
</cfif>
<cfset local.applicationSettings = GetApplicationSettings() />
<cftry>
<cfset local.cacheSegment = local.applicationSettings.cache />
<cfset local.attributeCollection = {
action: "update",
cache: local.cacheSegment
} />
<cfapplication attributeCollection="#local.attributeCollection#" />
<cfcatch>
<!--- Note: if the section above throws an error, it means that there aren't any other cache connections, which means that the cache value can be an empty struct --->
<cfset local.cacheEmpty = {} />
<cfset local.attributeCollection = {
action: "update",
cache: local.cacheEmpty
} />
<cfapplication attributeCollection="#local.attributeCollection#" />
</cfcatch>
</cftry>
<cftry>
<cfset local.cacheRegionExists = CacheRegionExistsLucee(arguments.szPropertiesCachename) />
<cfif NOT local.cacheRegionExists>
<cfset local.isDeleted = true />
</cfif>
<cfcatch>
<cfset local.isDeleted = true />
</cfcatch>
</cftry>
</cfif>
<cfcatch></cfcatch>
</cftry>
<cfreturn local.isDeleted />
</cffunction>
<cfscript>
// this function prevents an error being thrown, when there is no such region, after it has been removed in ACF
function _CacheGetProperties() {
var local = {};
local.obj = {};
try{
local.obj = CacheGetProperties(request.szPropertiesCachename);
}
catch(any e){
}
return local.obj;
}
variables.obj = {
"1_cacheGetPropertiesBefore": CacheGetProperties(request.szPropertiesCachename),
"2_cacheRegionExistsBefore": CacheRegionExistsLucee(request.szPropertiesCachename),
"3_cacheRegionRemove": CacheRegionRemoveLucee(request.szPropertiesCachename),
"4_cacheRegionExistsAfter": CacheRegionExistsLucee(request.szPropertiesCachename),
"5_cacheGetPropertiesAfter": _CacheGetProperties()
};
WriteDump(var="#variables.obj#");
</cfscript>