Skip to content
Advertisement

Is it possible to use redis or an alternative cache with HTMLPurifier?

Using HTML Purifier (ezyang/htmlpurifier) or specifically (stevebauman/purify) for Laravel 7 (powered by the former), is it possible to use an alternative cache than the filesystem?

The docs imply that you can use the FS, or null (not recommended) but also that the FS is just an implementation. Are other implementations possible (ideally Redis), and how would they be configured?

Advertisement

Answer

There are currently no official caches in the HTML Purifier library other than “Serializer”. That said, you can write your own extension of HTMLPurifier_DefinitionCache.

To be able to load your custom cache, you would have to register your implementation with DefinitionCacheFactory. As I understand it, this should work:

JavaScript

You can see which methods you would need to implement in DefinitionCache.php, which you’ll need to extend (class YourFullClassName extends HTMLPurifier_DefinitionCache). In the spirit of not leaving most of the answer accessible only in a link, here are the abstract methods of the class that you definitely have to implement:

JavaScript

There are some other methods in that class that you can override if you find a need to override them, e.g. generateKey($config) or isOld($key, $config).

I hope that helps!

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement