@@ -502,9 +502,13 @@ def _get_natoms_2(self, ntypes: int) -> tuple[int, np.ndarray]:
502502 return natoms , natoms_vec
503503
504504 def _get_memmap (self , path : DPPath ) -> np .memmap :
505- """Get or create a memory-mapped object for a given npy file."""
506- abs_path_str = str (Path (str (path )).absolute ())
507- return self ._create_memmap (abs_path_str )
505+ """Get or create a memory-mapped object for a given npy file.
506+ Uses file path and modification time as cache keys to detect file changes
507+ and invalidate cache when files are modified.
508+ """
509+ abs_path = Path (str (path )).absolute ()
510+ file_mtime = abs_path .stat ().st_mtime
511+ return self ._create_memmap (str (abs_path ), str (file_mtime ))
508512
509513 def _get_subdata (
510514 self , data : dict [str , Any ], idx : Optional [np .ndarray ] = None
@@ -962,14 +966,16 @@ def _check_mode(self, set_path: DPPath) -> bool:
962966
963967 @staticmethod
964968 @functools .lru_cache (maxsize = LRU_CACHE_SIZE )
965- def _create_memmap (path_str : str ) -> np .memmap :
969+ def _create_memmap (path_str : str , mtime_str : str ) -> np .memmap :
966970 """A cached helper function to create memmap objects.
967971 Using lru_cache to limit the number of open file handles.
968972
969973 Parameters
970974 ----------
971975 path_str
972976 The file path as a string.
977+ mtime_str
978+ The modification time as a string, used for cache invalidation.
973979 """
974980 with open (path_str , "rb" ) as f :
975981 version = np .lib .format .read_magic (f )
0 commit comments