Skip to content

Latest commit

 

History

History
68 lines (48 loc) · 2.19 KB

cache_class_loader.rst

File metadata and controls

68 lines (48 loc) · 2.19 KB
.. index::
    single: APC; ApcClassLoader
    single: ClassLoader; ApcClassLoader
    single: ClassLoader; Cache
    single: ClassLoader; XcacheClassLoader
    single: XCache; XcacheClassLoader

Cache a Class Loader

Introduction

Finding the file for a particular class can be an expensive task. Luckily, the ClassLoader component comes with two classes to cache the mapping from a class to its containing file. Both the :class:`Symfony\\Component\\ClassLoader\\ApcClassLoader` and the :class:`Symfony\\Component\\ClassLoader\\XcacheClassLoader` wrap around an object which implements a findFile() method to find the file for a class.

Note

Both the ApcClassLoader and the XcacheClassLoader can be used to cache Composer's autoloader.

ApcClassLoader

ApcClassLoader wraps an existing class loader and caches calls to its findFile() method using APC:

require_once '/path/to/src/Symfony/Component/ClassLoader/ApcClassLoader.php';

// instance of a class that implements a findFile() method, like the ClassLoader
$loader = ...;

// my_prefix is the APC namespace prefix to use
$cachedLoader = new ApcClassLoader('my_prefix', $loader);

// register the cached class loader
$cachedLoader->register();

// deactivate the original, non-cached loader if it was registered previously
$loader->unregister();

XcacheClassLoader

XcacheClassLoader uses XCache to cache a class loader. Registering it is straightforward:

require_once '/path/to/src/Symfony/Component/ClassLoader/XcacheClassLoader.php';

// instance of a class that implements a findFile() method, like the ClassLoader
$loader = ...;

// my_prefix is the XCache namespace
$cachedLoader = new XcacheClassLoader('my_prefix', $loader);

// register the cached class loader
$cachedLoader->register();

// deactivate the original, non-cached loader if it was registered previously
$loader->unregister();