Disclaimer

The views and opinions expressed in this blog are my own and do not necessarily reflect those of my employer. The views and opinions expressed by visitors to this blog are theirs and do not necessarily reflect my own

Wednesday, November 14, 2007

Clear Page Cache When Using Tabs & Sub-tabs (new solution)

Previously I posted a solution for clearing page cache of a target page when a user clicks a sub-tab. While my solution works there is much cleaner way of achieving this involving the use of APEX_UTIL.CLEAR_PAGE_CACHE().

Using the same scenario, consider that we need to clear the page cache of the page that we will be re-directed to, before it loads. The solution is very similar but requires no modification of the target page.

Firstly, create a new application page process:
Navigate to Applications -> Shared Components -> Application Processes and hit “Create” in the application builder. Create a process which runs “On Submit: After Page Submission – Before Computations and Validations”. In Process Source type the following
IF (:REQUEST = 'T_YOUR_TAB_NAME’) THEN
APEX_UTIL.CLEAR_PAGE_CACHE(your_page_number);
END IF;

T_YOUR_TAB_NAME can be identified from the developer mode or using firebug in Firefox on the specific tab your interested in. That's it! Now when a user clicks on a sub-tab the target page's, page cache will be cleared before it loads. To clear the cache for another tab, just add a new block to the application process you have created.

6 comments:

Anthony Rayner said...

Kris,

Nice post. I have a small suggestion for making it a little more generic. You could retrieve the tab's main page id back from the APEX repository based on the REQUEST value, then just issue the CLEAR_PAGE_CACHE on that, like this:

DECLARE
l_tab_page NUMBER;
BEGIN
SELECT tab_page
INTO l_tab_page
FROM apex_application_tabs
WHERE application_id = :APP_ID
AND tab_name = :REQUEST;

APEX_UTIL.CLEAR_PAGE_CACHE(l_tab_page);

END;

Then in order to restrict when this happens, you could add a condition to the process, like:
Type:
'Request is contained within Expression 1'

Expression 1:
[TAB_NAME_1],[TAB_NAME_2]...

So all the developer would need to do would be to add these 'Expression 1' values.

Anthony.

Anthony Rayner said...

Ok, so been thinking about this a bit more. And I have a further abstraction.

What would be the ideal way to do this? Define that the tab should clear cache with the tab definition in Shared Components and not have to worry about adding logic anywhere else? This can be done like this:

Change the application process to include a no_data_found exception handler:

DECLARE
l_tab_page NUMBER;
BEGIN
SELECT tab_page
INTO l_tab_page
FROM apex_application_tabs
WHERE application_id = :APP_ID
AND tab_name = :REQUEST;

APEX_UTIL.CLEAR_PAGE_CACHE(l_tab_page);

EXCEPTION
WHEN NO_DATA_FOUND THEN
null;
END;

Then change the condition to the following:

Type: 'Exists (SQL query returns at least one row)
Expression 1:
SELECT 1
FROM apex_application_tabs
WHERE application_id = :APP_ID
AND tab_name = :REQUEST
AND component_comment like '%$ClearCache$%'

Then the important part, in order to define a tab as clear cache, just add the following to the 'Comments' for the tab:

$ClearCache$

The developer would then not need to do anything more to the application process, just add this flag in the tab definition to be picked up by the exists condition.

Anthony.

Anthony Rayner said...

Forgot to mention, with the last solution you would need to ensure that when the application is exported, export comments is set to 'Yes'.

Anthony.

Kris said...

Nice! Thanks Anthony, When are you presenting at the UK OUG by the way?

Kris

Anthony Rayner said...

Kris,

Next Tuesday 4th December. 11-15 to 12-15. Still got some work to do on it. Be really glad when it's done and dusted!

Are you going? Would be good to catch up.

Anthony

Arifa said...

My REQUEST value never sets to tab name. I am using APEX 4.1.
Please help. How can I set the REQUEST value on tab click.