pkg: implement a cache for packages and use it to break circular dependencies