22.7. Using a Custom CacheDir Class

SCons' internal CacheDir class can be extended to support customization around the details of caching behaviors, for example using compressed cache files, encrypted cache files, gathering statistics and data, or many other aspects.

To create your own custom CacheDir class, your custom class must be a subclass of SCons internal SCons.CacheDir.CacheDir class. You can then pass your custom CacheDir class to the CacheDir method or set the environment construction variable $CACHEDIR_CLASS to the class before configuring the cache in that environment. SCons will internally invoke and use your custom class when performing cache operations. The below example shows a simple use case of overriding the copy_from_cache method to record the total number of bytes pulled from the cache.

        import SCons
        import os

        class CustomCacheDir(SCons.CacheDir.CacheDir):
            total_retrieved = 0

            @classmethod
            def copy_from_cache(cls, env, src, dst):
                # record total bytes pulled from cache
                cls.total_retrieved += os.stat(src).st_size
                super().copy_from_cache(env, src, dst)

        env = Environment()
        env.CacheDir('scons-cache', CustomCacheDir)
        # ...