| Chapter 3: Caching in Servlet and JSP
Caching is one of the most important techniques that you must consider to improve performance of your application. Cache refers to temporary data storage. It means you can store processed data in the memory and use it without processing for every request. Although it is common knowledge that caching improves performance, the most common question is what data to cache and where to cache.
You can cache static or semi-static data. Static data remains constant over multiple user requests. In most web applications, it is typically represented in the form of static html files and sometimes depending on the application design it is stored in a database. If it is stored in a database then for every request a Servlet or a JSP generates a page, which is redundant and unnecessary. So static data is the ideal type of data to be cached. Dynamic data is generated as per user request basis and cannot be cached.
The third type of data is the semi-static data that is often treated as dynamic data. This is neither static nor dynamic. An example for the semi dynamic data is any news website like CNN.com. The data on a news page does not change often and changes when extra news is added to the present page. So it should be cached until the news is updated.
After getting a clear picture about which data to cache, the question is where
to cache the data. In a web application, the caching location can be
the browser, proxy server or application server. Caching boosts web
application performance by reducing bandwidth and application server
resources thus reducing your application cost. The closer it is to the
user, better the performance. This has to be carefully designed and
implemented based on what and how much data to cache. We will discuss
two recipes in this chapter. The first recipe shows how to cache whole
response in the browser and proxy server and avoid using application
server resources. The second recipe shows how to cache static or semi-static
sub-sections in JSP and reduce processing resources.
This chapter discusses the following recipes to solve these issues when you design a web application.
|