[idmap] Cache target apks as they are often reused

idmap2d is being often called for the same targets, e.g. system
apps. Caching those apks makes idmap verification and creation
much faster e.g. for user switching

Test: build + boot + UTs
Bug: 271904589
Change-Id: Ib6f7af385c2389b50d5c74aa08b4bab290580809
diff --git a/cmds/idmap2/idmap2d/Idmap2Service.h b/cmds/idmap2/idmap2d/Idmap2Service.h
index cc8cc5f..a69fa61 100644
--- a/cmds/idmap2/idmap2d/Idmap2Service.h
+++ b/cmds/idmap2/idmap2d/Idmap2Service.h
@@ -75,7 +75,20 @@
  private:
   // idmap2d is killed after a period of inactivity, so any information stored on this class should
   // be able to be recalculated if idmap2 dies and restarts.
-  std::unique_ptr<idmap2::TargetResourceContainer> framework_apk_cache_;
+
+  // A cache item for the resource containers (apks or frros), with all information needed to
+  // detect if it has changed since it was parsed:
+  //  - (dev, inode) pair uniquely identifies a file on a particular device partition (see stat(2)).
+  //  - (mtime, size) ensure the file data hasn't changed inside that file.
+  struct CachedContainer {
+    dev_t dev;
+    ino_t inode;
+    int64_t size;
+    struct timespec mtime;
+    std::unique_ptr<idmap2::TargetResourceContainer> apk;
+  };
+  std::unordered_map<std::string, CachedContainer> container_cache_;
+  std::mutex container_cache_mutex_;
 
   int32_t frro_iter_id_ = 0;
   std::optional<std::filesystem::directory_iterator> frro_iter_;