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:

...
$factory = HTMLPurifier_DefinitionCacheFactory::instance();
$factory->register('YourImplName', 'YourFullClassName');
$config->set('Cache.DefinitionImpl', 'YourImplName');
...

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:

    /**
     * Adds a definition object to the cache
     * @param HTMLPurifier_Definition $def
     * @param HTMLPurifier_Config $config
     */
    abstract public function add($def, $config);

    /**
     * Unconditionally saves a definition object to the cache
     * @param HTMLPurifier_Definition $def
     * @param HTMLPurifier_Config $config
     */
    abstract public function set($def, $config);

    /**
     * Replace an object in the cache
     * @param HTMLPurifier_Definition $def
     * @param HTMLPurifier_Config $config
     */
    abstract public function replace($def, $config);

    /**
     * Retrieves a definition object from the cache
     * @param HTMLPurifier_Config $config
     */
    abstract public function get($config);

    /**
     * Removes a definition object to the cache
     * @param HTMLPurifier_Config $config
     */
    abstract public function remove($config);

    /**
     * Clears all objects from cache
     * @param HTMLPurifier_Config $config
     */
    abstract public function flush($config);

    /**
     * Clears all expired (older version or revision) objects from cache
     * @note Be careful implementing this method as flush. Flush must
     *       not interfere with other Definition types, and cleanup()
     *       should not be repeatedly called by userland code.
     * @param HTMLPurifier_Config $config
     */
    abstract public function cleanup($config);

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