Main  /  Edit version 3  /  Edit version 4  /   /  Users Area

Difference "Basic Caching" ver. 3 versus ver. 4

Content:

<!--toc-->
## 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:
- Handing new data
- Handling cached data
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](/wikipp/en/page/cppcms_1x_config#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 common 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.
<!--toc-->
## 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:
- Handing new data
- Handling cached data
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](/wikipp/en/page/cppcms_1x_config#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 common 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\_interface](/cppcms_ref/1.0.2/classcppcms_1_1cache__interface.html) and [cppcms::triggers\_recorder](/cppcms_ref/1.0.2/classcppcms_1_1triggers__recorder.html) for detailed description and examples.

Sidebar:

← [Sessions](http://cppcms.com/wikipp/en/page/cppcms_1x_sessions)
[Serialization](http://cppcms.com/wikipp/en/page/cppcms_1x_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