Cutelee  6.1.0
Public Member Functions | List of all members
Cutelee::CachingLoaderDecorator Class Reference

Implements a loader decorator which caches compiled Template objects. More...

#include <cutelee/cachingloaderdecorator.h>

Inheritance diagram for Cutelee::CachingLoaderDecorator:
Inheritance graph
[legend]

Public Member Functions

 CachingLoaderDecorator (std::shared_ptr< AbstractTemplateLoader > loader)
 
 ~CachingLoaderDecorator () override
 
bool canLoadTemplate (const QString &name) const override
 
void clear ()
 
std::pair< QString, QStringgetMediaUri (const QString &fileName) const override
 
bool isEmpty () const
 
Template loadByName (const QString &name, const Cutelee::Engine *engine) const override
 
int size () const
 
- Public Member Functions inherited from Cutelee::AbstractTemplateLoader
virtual ~AbstractTemplateLoader ()
 

Detailed Description

The CachingLoaderDecorator can be used with any implementation of Cutelee::AbstractTemplateLoader.

auto loader = std::shared_ptr<Cutelee::FileSystemTemplateLoader>::create();
loader->setTemplateDirs({"/path/one", "/path/two"});
auto cache =
std::shared_ptr<Cutelee::CachingLoaderDecorator>::create( loader );
m_engine->addTemplateLoader( cache );

The effect is that templates do not need to be loaded from the filesystem and compiled into Template objects on each access, but may be returned from the cache instead.

This can be significant if loading Templates in a loop, or loading the same Template very often in an application.

<ul>
{% for item in list %}
<li>{% include "itemtemplate.html" %}</li>
{% endfor %}
</ul>

If the loading of Templates is a bottleneck in an application, it may make sense to use the caching decorator.

Author
Stephen Kelly steve.nosp@m.ire@.nosp@m.gmail.nosp@m..com

Definition at line 70 of file cachingloaderdecorator.h.

Constructor & Destructor Documentation

◆ CachingLoaderDecorator()

CachingLoaderDecorator::CachingLoaderDecorator ( std::shared_ptr< AbstractTemplateLoader loader)

Constructor

Definition at line 46 of file cachingloaderdecorator.cpp.

◆ ~CachingLoaderDecorator()

CachingLoaderDecorator::~CachingLoaderDecorator ( )
override

Destructor

Definition at line 51 of file cachingloaderdecorator.cpp.

Member Function Documentation

◆ canLoadTemplate()

bool CachingLoaderDecorator::canLoadTemplate ( const QString name) const
overridevirtual

Return true if a Template identified by name exists and can be loaded.

Implements Cutelee::AbstractTemplateLoader.

Definition at line 53 of file cachingloaderdecorator.cpp.

◆ clear()

void CachingLoaderDecorator::clear ( )

Clears the Templates objects cached in the decorator.

Definition at line 59 of file cachingloaderdecorator.cpp.

◆ getMediaUri()

std::pair< QString, QString > CachingLoaderDecorator::getMediaUri ( const QString fileName) const
overridevirtual

Return a complete URI for media identified by fileName.

Implements Cutelee::AbstractTemplateLoader.

Definition at line 78 of file cachingloaderdecorator.cpp.

◆ isEmpty()

bool CachingLoaderDecorator::isEmpty ( ) const

Returns whether the cache is empty.

Definition at line 71 of file cachingloaderdecorator.cpp.

◆ loadByName()

Template CachingLoaderDecorator::loadByName ( const QString name,
const Cutelee::Engine engine 
) const
overridevirtual

Load a Template called name. Return an invalid Template if no content by that name exists.

Implements Cutelee::AbstractTemplateLoader.

Definition at line 85 of file cachingloaderdecorator.cpp.

◆ size()

int CachingLoaderDecorator::size ( ) const

Returns the number of Template objects cached in the decorator.

Definition at line 65 of file cachingloaderdecorator.cpp.