function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
TaoDuhTaoDuh 

site vs admin page controller dilemma

In my site code I have a bunch methods that look like this (with some extra error checking):

public SomeObj__c myobj {
  get {
    if (myobj == null) {
      myobj = [SELECT Id, ... FROM SomeObj__c WHERE Id = :Id LIMIT 1];
    }
  return myobj;
}
private set;
}


I understand the above to be a good practice.  However I'm running into a caching dilemma when it comes to sites and the key issue is the line... if (myobj == null)

The above is in an abstract base class which is extended by both the display and the admin controllers.  In the admin pages, everything works great.  My save methods all save properly and the expected values are displayed on the page.  Subsequent views of the corresponding page on the site also display the proper data.  Nice.

However, the issue occurs when the data editor edits a page and wants to see his changes. Imagine this scenario.
1) editor goes to admin page salesforce.com/apex/EditMyStuff?id=12345 <http://salesforce.com/apex/EditMyStuff?id=12345>  and makes changes
2) editor then goes to see changes at the site mysite.force.com/ViewMyStuff?id=12345 <http://mysite.force.com/ViewMyStuff?id=12345>  -- looks almost perfect
3) go back to admin page and make another change
4) refresh site...oops, no change.  Because the controller value is not null it does not re-retrieve

I can think of two solutions here.
#1 Completely separate the controllers for the public/private sides.  Leave out the if (myobj == null) lines on the public side and just have it retrieve every single time.
PROS: works as users expect
CONS: duplication of code, inefficient database utilization for most common use case

#2 Add a method to clear out all of the instance variables. Add a "Preview" button to the admin pages and train users to look there for changes rather than live site
PROS: keeps the efficiencies
CONS: training/support cost (our users are not very savvy)

Am I overlooking some 3rd way?  A better pattern perhaps?  Someone else out there must have a site with this problem.

BulentBulent

if the page content is refreshed frequently you can disable caching or reduce the caching duration via the cache and expires page attributes