Caching Data
Before we look at the <x:parse> action in
detail, I’d like to say a few words about the
caching technique used
in this example. The Meerkat data is
updated only on an hourly basis, so it’s pointless
to ask for it more frequently. It’s also expensive
in terms of time and computing resources to import and parse the XML
data. By caching the parsed data for an hour, the web application
gets more responsive and avoids putting load on the Meerkat server
unnecessarily.
The caching technique used here simply creates a timestamp for the
data in the form of a java.util.Date object and
saves it together with the data itself in the application scope,
using standard and JSTL core actions. When a new request is received,
it’s tested to see if the cache is older than the
predefined cache period (one hour in this example). If it is, a fresh
copy is imported, parsed, and saved in the application scope again,
along with the timestamp. Otherwise the cached data is used. You can
use this technique for any type of processing that’s
expensive, for instance retrieving data from a database or performing
complex calculations.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access