# Caching

This example shows how to use caching. This can be useful for big projects where you don’t want the c++ to be parsed again and again.

Let’s consider the following c++ file:

    namespace ns{
        int a = 1;
    }

To enable caching, you can use the following code:

    from pygccxml import utils
    from pygccxml import declarations
    from pygccxml import parser

    # Find out the c++ parser
    generator_path, generator_name = utils.find_xml_generator()

    # Configure the xml generator
    xml_generator_config = parser.xml_generator_configuration_t(
        xml_generator_path=generator_path,
        xml_generator=generator_name)

    # The c++ file we want to parse
    filename = "example.hpp"

    file_config = parser.file_configuration_t(
        data=filename,
        content_type=parser.CONTENT_TYPE.CACHED_SOURCE_FILE)

    project_reader = parser.project_reader_t(xml_generator_config)
    decls = project_reader.read_files(
        [file_config],
        compilation_mode=parser.COMPILATION_MODE.FILE_BY_FILE)

    global_namespace = declarations.get_global_namespace(decls)

    value = global_namespace.namespace("ns")
    print("My name is: " + value.name)

The first time you run this example, the c++ file will be read and a xml file will be generated:

INFO Creating xml file “example.hpp.xml” from source file “example.hpp” … INFO Parsing xml file “example.hpp.xml” … My name is: ns

The second time you run the example the xml file will not be regenerated:

INFO Parsing xml file “example.hpp.xml” … My name is: ns

Of course the performance gain will be small for this example, but can be intersting for bigger projects.