class_docstr: Class to cache and retrieve the state of an object.
  
  Objects can either be stored in memory or on disk. If stored on disk, they can be
  stored in a given directory, or alternatively a temporary location will be used.
  
  If necessary/possible, restored objects will be returned to their original device.
  
  Example:
  
  >>> state_cacher = StateCacher(memory_cache, cache_dir=cache_dir)
  >>> state_cacher.store("model", model.state_dict())
  >>> model.load_state_dict(state_cacher.retrieve("model"))
class_docstr_tok: ["Class","to","cache","and","retrieve","the","state","of","an","object",".","Objects","can","either","be","stored","in","memory","or","on","disk",".","If","stored","on","disk",",","they","can","be","stored","in","a","given","directory",",","or","alternatively","a","temporary","location","will","be","used",".","If","necessary\/possible",",","restored","objects","will","be","returned","to","their","original","device",".","Example",":",">",">",">","state_cacher","=","StateCacher","(","memory_cache",",","cache_dir=cache_dir",")",">",">",">","state_cacher.store","(","``","model","''",",","model.state_dict","(",")",")",">",">",">","model.load_state_dict","(","state_cacher.retrieve","(","``","model","''",")",")"]
docstr: Retrieve the object stored under a given key name.
docstr_tok: ["Retrieve","the","object","stored","under","a","given","key","name","."]
code: def retrieve(self, key: Hashable) -> Any:
      """Retrieve the object stored under a given key name."""
      if key not in self.cached:
          raise KeyError(f"Target {key} was not cached.")
  
      if self.in_memory:
          return self.cached[key]["obj"]
  
      fn = self.cached[key]["obj"]  # pytype: disable=attribute-error
      if not os.path.exists(fn):  # pytype: disable=wrong-arg-types
          raise RuntimeError(
              f"Failed to load state in {fn}. File doesn't exist anymore."
          )
      data_obj = torch.load(
          fn, map_location=lambda storage, location: storage
      )
      # copy back to device if necessary
      if "device" in self.cached[key]:
          data_obj = data_obj.to(self.cached[key]["device"])
      return data_obj
  
file_path: MONAI/monai/utils/state_cacher.py
imports: from __future__ import annotations
  import copy
  import os
  import pickle
  import tempfile
  from types import ModuleType
  from typing import Any, Hashable
  import torch
  from torch.serialization import DEFAULT_PROTOCOL
  from monai.config.type_definitions import PathLike
label_desc: Use image node_id 3 for calling the StateCacher obj's underlying member method code with example usage: obj.retrieve(key) and returns: data_obj, self