We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

Cache keys for flexibility and date expiry

Looking to use file cach now and switch to something else later. How do you build a key to handle anything you can use in a file directory and be compativle with other caches?

My data comes from files like /abc/xyz.zip and /a/b/c/xy.z.bzip2. I need to cache some of te archive contents. If I use - to replace /, I would have cache keys like the following. abc-xyz-content_part_1.txt abc-xyz-content_part_2.txt a-b-c-xy-z-content_part_1.txt a-b-c-xy-z-content_part_2.txt

With something like this, the expanded - based key could overlap with a valid file name from another directory. I might use a double dash, --, for the directory separators. Does this sound reasonable? Would it be incompatible with any of the cache backends?

I see there was an issue raised back in 2013 to make cache handle subdirectories. That will simplify the file backend, so long as it is compatible with other backends.

edited Jul '17

One other question. Can cache handle the date checking for the source? When the source is updated, do I have to perform my own date/time check and invalidate the cache?

Hi @peter

Q1: if you have a file cache and that not have a lot of files you can use a only one directory. But if you have thousands is better use nested directories to improve performance on accesing .

Q2: no, you have to delete the cache, then when some request need the system create a new cache. For example in a model you can add an behavior to delete the cache on update/delete

Good luck


i found a bit about nested directories in the cache documentation. That may be needed in the future. Currently I replace / with --. I prefer something that can be used on memcache and other backends unchanged. Do other cache backends have the equivalent structure?


Using the file backend with the None frontend. Fast. None lets you store images with no interference. The source of the files includes severl formats. They all go in the cache then code decides where the files will be long term.

I tested the default frontend shown in the documentation. That converts everything to serialized string. You store an image then browse the cache directory but cannot look at the image. Doh! The None frontend removes that problem and overhead.

From what I can see, the cache expiry leaves files in the cache. The lifetime supplied to the cache only changes the files retreived by cache->get. You have to do something else to remove the files. I guess a cron job could sweep the directory clean.