Tutorial: Basic Caching
No high performance framework can be build without proper caching. CppCMS provides sophisticated cache API that allows keep cache consistent and updated.
In this tutorial we would show first and simple step in caching --- timeout based caching.
In this tutorial we would calculate factorial and store calculated values in cache
Data
Our input form would include only two fileds: arg
and submit
.
struct input_form : public form { widgets::number<int> arg; widgets::submit submit; ... };
Our view would use this simple form:
struct message : public base_content { long long int fact; int arg; input_form info; };
And our template would be very simple:
<body> <h1>Factorial Calculator</h1> <h2><% arg %>! = <% fact %></h2> <form method="post" action="" > <% form as_p info %> </form> </body>
Code
First of all let's initialize out content
:
data::message c; c.arg=0; c.fact=1;
Then load our form:
if(env->getRequestMethod()=="POST") { c.info.load(*cgi); if(c.info.validate()) { c.arg=c.info.arg.get(); c.info.clear(); }
We load value of arg
from user in order to know what to calculate.
Now, if validation fails, we do not want to use cached data so, we render output and finish:
else { // No cache should be used render("message",c); return; } }
Now, when get the value we need to calculate factorial for we test if it is already cached:
string key="factorial_"+boost::lexical_cast<string>(c.arg); if(cache.fetch_page(key)) return;
If it is cached, fetch_page
would return true and set
output to prepared page.
Note: You should note, then when the page is fetched from the cache it is fetched in compressed on uncompressed format according to user request. Thus, actually, you not only save all operation that are needed to create page, you also save compression time that may be significant for big pages.
Now, if such key does not exists:
we calculate factorial and render our output:
long long int f=1; for(int i=1;i<=c.arg;i++) { f*=i; } c.fact=f; render("message",c);
Now, when output is ready we can store it in cache for future use with timeout of 1 hour:
cache.store_page(key,3600);
Note: In our particular case, we should not set timeout, we can use default, infinite timeout value and call:
cache.store_page(key);
Bug generally our dynamic pages get outdated, thus we need to define timeout or use triggers (triggers are not covered in this tutorial).
Cleaning cache
You can remove cache entry "rising" their keys, for example:
cache.rise("factorial_10");
Would remove entry with key factorial_10
from the memory, however this is not required in case of our simple program.
Configuration
In order to use cache, we need to define caching backed:
So we add to config.txt
following lines:
cache.backend="threaded" cache.limit=100
Type of our backend. Because we use mod-thread, we should define "threaded" and limit it size by number of entries, for example 100. See Configuration::Cache reference for setting up your cache backend correctly.
Future reading
This is a basics of using cache. In general CppCMS cache system supports much more:
- Removing set of keys using triggers
- Automatic dependency tracking of different keys
- Storing (serializing) various C++ objects in cache.
- Storing simple "text frames" in cache for faster rendering of elements like: sidebars, headers, footers, signatures and so on.
See: cppcms::cache_iface for detailed description and examples.