Main  /  Edit  /  History  /   /  Users Area

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


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:

<h1>Factorial Calculator</h1>
<h2><% arg %>! = <% fact %></h2>
<form method="post" action="" >
<% form as_p info %>


First of all let's initialize out content:

data::message c;

Then load our form:

if(env->getRequestMethod()=="POST") {*cgi);
    if( {;;

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

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 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++) {

Now, when output is ready we can store it in cache for future use with timeout of 1 hour:


Note: In our particular case, we should not set timeout, we can use default, infinite timeout value and call:


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:


Would remove entry with key factorial_10 from the memory, however this is not required in case of our simple program.


In order to use cache, we need to define caching backed:

So we add to config.txt following lines:


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:

See: cppcms::cache_iface for detailed description and examples.


CppCMS is a web development framework for performance demanding applications.

Support This Project Logo

Поддержать проект

CppCMS needs You


Main Page

Valid CSS | Valid XHTML 1.0