Archive for the 'cache' Category
caching with wt

in an elegant design a web applications presentation layer (or equivalent ones) should not make tricks about caching. I see often bad code like following everywhere, especiall written by php kids:

HTML Page::getHtml() {
if(cached)
data = getFramCache()
else
data = getDataFromDatabase()
}

one should create polymorfic database, or use templates to hide caching in the data source pattern.

Widget::onEvent(IdClass id)
{
Datasource *datasoure = Datasource::singleton();
datasource->getData();
}

Datasource::getData(Key &key)
{
//do some sql
}

then you could have
class DatasourceWithCache: public Datasource
********
Data *DatasourceWithCache::getData(Key &key)
{
Data *data = getFromCache();
if(!data)
Datasource::getData(key);
return data;
}

The cache could be shared between threads or processes (!!security issues!!), or just one process.
Your framework should initialize the singleton with the correct datasource. You could create a config file, and specify the activation of the cache. You can implement a datasource based on sthg. like memcache (so much venerated by php kids).

Let’s take a simple example with a web application that would have different views (pages) of the same table data. If you change one cell in the table, all the other cached views should be invalidated in the “classical” approach, whereas with ajax approach, you should invalidate the cache only for that particular cell. If this level of cache is not enough, you could always find a compromise and with a bit more complex cache, one could cache the whole rendered widget data, but here the widget code itself would probably need to help. (and thus a bit in contradiction with the first statement). Anyway there should be a balance between the necessity of the cache.

Another example, a wiki engine, or blog engine. With wt, one would need to cache the pages data, not the decoration around. The decoration would not change during going from one page to another one. So one could specialize the datasource by providing rendered (converted from wiki syntax to html). And thus one would just feed the wiki widget with cached data.