Oct 22, 2008

Missing cHash? Make plugin USER_INT!

Yesterday I encountered yet another unusual TYPO3 problem. An external web site had to link to pages on a TYPO3 site providing various parameters for an extension on one page. The extension is cacheable because it does certain network queries and it will be slow to do them for each request. So extension uses cHash for proper caching.

The problem was that the external site could not create cHash. cHash creation is not trivial and involves using site–specific encryption key. So the external site could not ptovide cHash and displayed content was wrong: it was like the page was called without any parameter.

What could I do?
There were several choices:
  • make external site send proper cHash. Difficult and not secure to let others know the encryption key
  • make plugin non–cached. Bad because affects performance
  • make plugin cacheable in general but non–cached only if cHash is missing

I decided on the last choice. Plugin will be cacheable only if cHash exists in the URL. But how do I do it? When plugin is cached, it is not called. So there is no way to check cHash for each request from the plugin.

Fortunately my experience with TYPO3 gave me the answer in 30 seconds. I will use conditions. Here is how it looks like:

plugin.tx_myext_pi1 = USER
[globalVar = GP:cHash = ]
plugin.tx_myext_pi1 = USER_INT

So when user clicks normal links, cHash is present and plugin is cached. When request comes from the external server, plugin is not cached because cHash is missing.

Why did I use this approach and not this:

plugin.tx_myext_pi1 = USER
[globalVar = GP:cHash = ]
config.no_cache = 1

The answer is simple. The page contains also many other elements. They still could be cached. This improves performance. no_cache is always bad. So I do not use it and do not recommend anyone to use it. Always try to find the other way. It usually exists.

A warning

While the above TypoScript works, do not use it unless you really need it. Do it only if you understand why you have to use it and what implication you have if you use this code. I published this code only to show that there are different solution to the problem and the first obvious solution (no_cache) is not good.

No comments:

Post a Comment