cppcms::cache_interface Class Reference

This class is the major gateway of the application to CppCMS caching abilities. Any access too cache would be done via this class. More...

`#include <cppcms/cache_interface.h>`

Inheritance diagram for cppcms::cache_interface:

## Public Member Functions

void | rise (std::string const &trigger)
void | add_trigger (std::string const &trigger)
void | clear ()
void | reset ()
bool | stats (unsigned &keys, unsigned &triggers)
bool | has_cache ()
bool | nocache ()
bool | fetch_page (std::string const &key)
void | store_page (std::string const &key, int timeout=-1)
bool | fetch_frame (std::string const &key, std::string &result, bool notriggers=false)
void | store_frame (std::string const &key, std::string const &frame, std::set< std::string > const &triggers=std::set< std::string >(), int timeout=-1, bool notriggers=false)
void | store_frame (std::string const &key, std::string const &frame, int timeout, bool notriggers=false)
template<typename Serializable >
bool | fetch_data (std::string const &key, Serializable &data, bool notriggers=false)
template<typename Serializable >
void | store_data (std::string const &key, Serializable const &data, std::set< std::string > const &triggers=std::set< std::string >(), int timeout=-1, bool notriggers=false)
template<typename Serializable >
void | store_data (std::string const &key, Serializable const &data, int timeout, bool notriggers=false)

## Friends

class | triggers_recorder

## Detailed Description

This class is the major gateway of the application to CppCMS caching abilities. Any access too cache would be done via this class.

CppCMS cache model supports following concepts:

- key the unique identification of the object in cache
- timeout – the maximal time the cached object remains valid
- trigger – special key that allows fast invalidation of multiple cache objects.

The first two concepts are quite popular and available in most of Web framework, but the last one is very unique to CppCMS that gives fine grained cache invalidation tools.

Each time the page is created it automatically receives some triggers during the process of creation. When some object is fetched from the cache or stored into it, it adds triggers to the major page. This provides semi-automatic triggers management.

For example:

if(cache().fetch_page("main_page"))
return;
if(!cache().fetch_frame("article_"+id,article)) {
article=generate_article_from_data_base(id);
cache.store_frame("article_"+id,article);
}
// Generate some HTML here using article
cache.store_page("main");

Let's assume that "main_page" wasn't found in cache, then we try to fetch a frame that holds only a single article "article_123", if it is fetched, the result is stored in a string article and the trigger "article_123" is automatically added to set of triggers that "main_page" depends on them.

When the article updated, and "article_123" key is risen, it would automatically invalidate "main_page" as well.

CppCMS cache_interface allows storing arbitrary object in cache, For this purpose they should be "serializable". This can be done by specializing a class cppcms::setialization_traits

## Member Function Documentation

void cppcms::cache_interface::add_trigger | ( | std::string const & | trigger | ) |

Add a trigger trigger to the list of dependencies of current page.

void cppcms::cache_interface::clear | ( |  | ) |

Clear all CppCMS cache - use carefully

template<typename Serializable >

bool cppcms::cache_interface::fetch_data | ( | std::string const & | key,
|  | Serializable & | data,
|  | bool | notriggers = `false`
| ) |  |

inline

Fetch a serializeable object from the cache.

Parameters

  key | – the key that uniquely defines the frame.
  data | – an object store fetched data
  notriggers | – if true, no triggers that an object is dependent on would be added to dependencies of the current page, otherwise (false, default), the all triggers that the object is dependent on, including the key itself would be added as dependent triggers to current rendered page.

Returns
  returns true if the entry was found.

References cppcms::serialization_traits< Object >::load().

bool cppcms::cache_interface::fetch_frame | ( | std::string const & | key,
|  | std::string & | result,
|  | bool | notriggers = `false`
| ) |  |

Fetch a string (usually some HTML part) from the cache.

Parameters

  key | – the key that uniquely defines the frame.
  result | – string to store fetched value
  notriggers | – if true, no triggers that a frame is dependent on would be added to dependencies of the current page, otherwise (false, default), the all triggers that page is dependent on, including the key itself would be added as dependent triggers to current rendered page.

Returns
  returns true if the entry was found.

bool cppcms::cache_interface::fetch_page | ( | std::string const & | key | ) |

Fetch a page from the cache with a key key. If the page exists, it is written to output and true is returned. Otherwise false is returned.

bool cppcms::cache_interface::has_cache | ( |  | ) |

Returns true if caching system is enabled

bool cppcms::cache_interface::nocache | ( |  | ) |

Opposite of has_cache

void cppcms::cache_interface::reset | ( |  | ) |

Remove all triggers added to current page so far

void cppcms::cache_interface::rise | ( | std::string const & | trigger | ) |

Rise a trigger trigger. All cached objects that depend on this trigger would be invalidated

bool cppcms::cache_interface::stats | ( | unsigned & | keys,
|  | unsigned & | triggers
| ) |  |

Get statistics about items stored in cache. May require O(n) complexity, use with care.

Parameters

  keys | – the number of items stored in cache
  triggers | – the number of various triggers existing in the cache.

Returns false if caching system is disabled.

template<typename Serializable >

void cppcms::cache_interface::store_data | ( | std::string const & | key,
|  | Serializable const & | data,
|  | std::set< std::string > const & | triggers = `std::set<std::string>()`,
|  | int | timeout = `-1`,
|  | bool | notriggers = `false`
| ) |  |

inline

Store a serializeable object to the cache.

Parameters

  key | – the key that uniquely defines the object.
  data | – the actual object
  triggers | – the set of triggers that the key should depend on (key is added automatically)
  timeout | – maximal object lifetime, -1 is infinity
  notriggers | – if notriggers is true no frame dependent triggers would be added to the current page trigger set. Otherwise (default) current page would depend on the key and triggers as its dependent triggers.

References cppcms::serialization_traits< Object >::save().

template<typename Serializable >

void cppcms::cache_interface::store_data | ( | std::string const & | key,
|  | Serializable const & | data,
|  | int | timeout,
|  | bool | notriggers = `false`
| ) |  |

inline

Store a serializeable object to the cache.

Parameters

  key | – the key that uniquely defines the object.
  data | – the actual object
  timeout | – maximal object lifetime, -1 is infinity
  notriggers | – if notriggers is true key added to the current page trigger set. Otherwise (default) current page would depend on the key

void cppcms::cache_interface::store_frame | ( | std::string const & | key,
|  | std::string const & | frame,
|  | std::set< std::string > const & | triggers = `std::set< std::string >()`,
|  | int | timeout = `-1`,
|  | bool | notriggers = `false`
| ) |  |

Store a string (usually some HTML part) to the cache.

Parameters

  key | – the key that uniquely defines the frame.
  frame | – the actual value
  triggers | – the set of triggers that the key should depend on (key is added automatically)
  timeout | – maximal object lifetime, -1 is infinity
  notriggers | – if notriggers is true no frame dependent triggers would be added to the current page trigger set. Otherwise (default) current page would depend on the key and triggers as its dependent triggers.

void cppcms::cache_interface::store_frame | ( | std::string const & | key,
|  | std::string const & | frame,
|  | int | timeout,
|  | bool | notriggers = `false`
| ) |  |

Store a string (usually some HTML part) to the cache.

Parameters

  key | – the key that uniquely defines the frame.
  frame | – the actual value
  timeout | – maximal object lifetime, -1 is infinity
  notriggers | – if notriggers is true key added to the current page trigger set. Otherwise (default) current page would depend on the key

void cppcms::cache_interface::store_page | ( | std::string const & | key,
|  | int | timeout = `-1`
| ) |  |

Store page with key akey in cache, with timeout timeout.

This function stores a page with dependencies on all triggers that were added so far.

Parameters

  key | – the key that defines the cache.
  timeout | – maximal valid time of the page. timeout=-1 means infinite. Use with care.

Note: store_page does not rise the trigger key, only replaces the value.

The documentation for this class was generated from the following file:

- cppcms/cache_interface.h