We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Volt + Cache (redis/memcached)

Hi all,

I'm building a Phalcon app that will be hosted in a HA cluster: multiple Nginx, multiple database nodes, remote datastores...

As such I'm using a cache server (Redis) to manage data cache and php sessions. The last step of my cache strategy is to get Volt (or actually any template engine) to write/save the compiled files in Redis. If I need to keep those files to disk I'll have to use a nfs share between my web servers, and I'm pretty sure I'll lose a fair amount of milisseconds for each page.

Has anyone implemented something similar ? Is there something in Phalcon to get me started? Cheers

If you're using opcache, Volt templates are stored in shared memory segment, you don't need to store them in Redis or any other data cache to store them.



24.8k

Well, Redis is fast and full of interesting features, it seems to be a better choice for the type of app I'm building.

I need to store the frgments somewhere accessible by all my webservers, otherwise I'll have to re-generate and duplicate those files on each server (being memory or local disks). That's hardly doable and not ideal for a scalable web cluster.

Plus it could be an issue since I'll have a lot of pages to cache: lots of memory used on all servers. And it will make updates and deployment scripts more complicated since I'll have to "clean" data on all the servers instead of only flushing the cache servers.



34.6k
Accepted
answer

If you want to store the output of executed Volt that's different than store Volt templates in Redis. You can't store Volt because Volt is a meta-language of PHP.

Volt is in fact PHP code and Redis is not faster than opcache or apc to store/retrieve PHP code.

If you want to store fragments of output produced by PHP (Volt), check this: https://docs.phalcon.io/en/latest/reference/views.html#caching-view-fragments



24.8k

@Andres You're a god. That's exactly what I was looking for.

Sorry if I was not clear enough, that's the output files I was talking about.

Thank you very nuch.