Skip to content

Cache combined puli.json files of all installed packages #148

Open
@webmozart

Description

@webmozart

Currently, whenever information about installed packages needs to be accessed, the complete PackageManager must be instantiated and loaded with the puli.json files of all installed packages. This process is a bit expensive, first of all due to file system accesses, and second due to validation and migration of the contents of each file. Vendor dependencies only (potentially) change when running "composer update", so doing that work every time that the manager boots is quite useless.

I suggest that we cache the contents of all puli.json file in a puli.json.cache file. For that, we need the following classes:

  • CacheFile: represents the puli.json.cache file
  • CacheManager: manages loading and generating CacheFiles
  • Config::CACHE_FILE: new configuration entry that stores the path of the cache file
  • DefaultConfig: CACHE_FILE is '{$puli-dir}/puli.json.cache' by default

Here's more information on the methods in the new classes/interfaces:

namespace Puli\Manager\Api\Cache;

interface CacheManager
{
    public function getContext();

    public function getCacheFile();

    public function refreshCacheFile();

    public function getPackage($name);

    public function getRootPackage();

    public function getPackages();

    public function findPackages(Expression $expr);

    public function hasPackage($name);

    public function hasPackages(Expression $expr = null);
}
  • The *Package*() methods are similar to those in PackageManager
  • refreshCacheFile() generates a new cache file if (i) none exists or (ii) the change time of any of the puli.json files is newer than that of the cache file
  • getCacheFile() reads and returns the cache file. If none exists -> refreshCacheFile().

The manager interface is implemented by Puli\Manager\Cache\CacheManagerImpl. New instances are returned by Puli::getCacheManager().

namespace Puli\Manager\Api\Cache;

class CacheFile
{
    /**
     * @var string|null
     */
    private $path;

    /**
     * @var PackageFile[]
     */
    private $packageFiles = array();

    public function __construct($path = null)

    public function getPath()

    public function setPackageFiles(array $packageFiles)

    public function addPackageFile(PackageFile $packageFile)

    public function removePackageFile($packageName)

    public function clearPackageFiles()

    public function getPackageFile($packageName)

    public function getPackageFiles()

    public function hasPackageFile($packageName)

    public function hasPackageFiles()
}
  • The cache file is similar to other such classes in the manager, like ConfigFile, RootPackageFile or PackageFile.

Unlike PackageFile, which has a version number and can be migrated between different versions, CacheFile always contains the PackageFile JSON in the latest version.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions