Main  /  Edit  /  History  /   /  Users Area

Basic Caching

Introduction

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 cppcms::form {
    cppcms::widgets::numeric<int> arg;
    cppcms::widgets::submit submit;
    ...

Our view would use this simple form:

struct message : public cppcms::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 %>
</form>

Code

The code would consist of two parts:

First of all let's initialize out content:

content::message c;
c.arg=0;
c.fact=1;

Then load our form:

if(request().request_method()=="POST") {
    c.info.load(context());
    if(c.info.validate()) {
        c.arg=c.info.arg.value();
        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 had already did it and it is cached:

std::ostringstream key;
key << "factorial_" << c.arg;
if(cache().fetch_page(key.str()))
    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 or 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.str(),3600);

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

cache().store_page(key.str());

But 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 backend:

So we add to config.js following lines:

"cache" : {
    "backend" : "thread_shared",
    "limit" : 100,
}

Type of our backend: thread_shared. We should also define it size as 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_interface and cppcms::triggers_recorder for detailed description and examples.


Sessions | Top | Serialization

About

CppCMS is a web development framework for performance demanding applications.

Support This Project

SourceForge.net Logo

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

CppCMS needs You


Navigation

Main Page



Valid CSS | Valid XHTML 1.0