Say I have a JSF backing bean, request scope, that has a List as a member.
In the backing bean, I have a method that is bound to a button on the page.
When the button is first pressed, the method uses an EJB to populate the list.
Upon subsequent presses of the button, I do not want the method to call the DB.
So, I added a check:
if(list == null || list.size() == 0)
//populate the list
Otherwise, the list will already be populated, so no DB call necessary.
Is this satisfactory for a caching mechanism?
Because the bean is request scope, its members will only exist for the lifetime of a single request. So, the data will be fetched every time a page is requested.
One thing you could do would be have the cached data in a different scope (such as session or application).
Sample code:
That way, you could easily reference the data from a request scope bean while keeping request-level artifacts separate.
Some frameworks add support for page scope, which keeps data tied to the lifetime of the view. That may be another option, depending on your needs.