5

I have a few YAML files to be parsed from a service.

But at every page hit, those YAML files are parsed and converted to a PHP Array. I understand Symfony keeps the default YAML files cached, so it doesn't parse every yaml file at every page hit.

I was wondering what is the best practice here. Is there a way to have my YAML files parsed only once and stored with Symfony2's cache? If so, could I please be pointed in the right direction?

3
  • I have not much knowledge about it, but what about JmsSerializer (and the JmsSerializerBundle)? Commented Apr 8, 2013 at 18:33
  • Not sure if you can somehow utilize Symfony2's caching system, but you can definitely use services from bundles like SonataCacheBundle, to store parsed data. Commented Apr 8, 2013 at 18:45
  • I'd be able to serialize my arrays, but I'm not sure how to store them on the cache and load them. Also, SonataCacheBundle doesn't seem to be quite well documented. I'll check it out. Commented Apr 8, 2013 at 19:32

1 Answer 1

3

It all depends on how you want to do the caching.

  • HTTP caching really only makes sense when in the web context and when the particular output of the action where you're parsing and adding these files to an array is relatively static. If both of those conditions are met, then that is the best way to go.

  • You can easily use APC to do your caching. It makes caching the contents of a variable quite simple. Doctrine provides a cache abstraction around APC, or you can use native PHP functions.

in config.yml

 services:
        cache:
            class: Doctrine\Common\Cache\ApcCache

then in your controller or service:

if ($yamlArray = $this->get('cache')->fetch('foo')) {
    $yamlArray = unserialize($yamlArray);
} else {
    // do the work
    $this->get('cache')->save('foo', serialize($yamlArray));
}
  • Make your own caching service and hook it into the Symfony cache commands

Details on creating a cache warmer

Details on cache_clearer(added but not well documented).

Sign up to request clarification or add additional context in comments.

4 Comments

Those are not templates I'm trying to cache, they're YAML files, not TWIG files. Anyway, the principles of APC caching seem to apply. I'd load the YAML file from the disk, parse it, serialize the array, store it on the APC cache... but how do I check if the serialized version is outdated (or, the YAML configuration has changed and needs to be reloaded/reparsed)? Would I store the file's crc along with the serialized array or something? Won't it be kinda slow on the production environment? I'd love to just run cache:clear --env=prod and have it rebuilt on the production environment :(
I've updated my answer to address the fact that you asked about YAML not Twig, (Mondays make my eyes go cross apparently).
Re: but how do I check if the serialized version is outdated, you can store the object in the cache along with an md5_file() or crc32(). For example, $this->get('cache')->save('foo_' . md5_file($file), $yamlArray), then use $this->get('cache')->fetch('foo_' . md5_file($file)) when fetching.
Storing a hash as @theunraveler has mentioned is definitely the easiest way. Let the hash algorithm do the work for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.