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: Store a given object with the given key name.
  
  Args:
      key: key of the data object to store.
      data_obj: data object to store.
      pickle_module: module used for pickling metadata and objects, default to `self.pickle_module`.
          this arg is used by `torch.save`, for more details, please check:
          https://pytorch.org/docs/stable/generated/torch.save.html#torch.save.
      pickle_protocol: can be specified to override the default protocol, default to `self.pickle_protocol`.
          this arg is used by `torch.save`, for more details, please check:
          https://pytorch.org/docs/stable/generated/torch.save.html#torch.save.
docstr_tok: ["Store","a","given","object","with","the","given","key","name",".","Args",":","key",":","key","of","the","data","object","to","store",".","data_obj",":","data","object","to","store",".","pickle_module",":","module","used","for","pickling","metadata","and","objects",",","default","to","`","self.pickle_module","`",".","this","arg","is","used","by","`","torch.save","`",",","for","more","details",",","please","check",":","https",":","\/\/pytorch.org\/docs\/stable\/generated\/torch.save.html","#","torch.save",".","pickle_protocol",":","can","be","specified","to","override","the","default","protocol",",","default","to","`","self.pickle_protocol","`",".","this","arg","is","used","by","`","torch.save","`",",","for","more","details",",","please","check",":","https",":","\/\/pytorch.org\/docs\/stable\/generated\/torch.save.html","#","torch.save","."]
code: def store(
      self,
      key: Hashable,
      data_obj: Any,
      pickle_module: ModuleType | None = None,
      pickle_protocol: int | None = None,
  ) -> None:
      """
      Store a given object with the given key name.
  
      Args:
          key: key of the data object to store.
          data_obj: data object to store.
          pickle_module: module used for pickling metadata and objects, default to `self.pickle_module`.
              this arg is used by `torch.save`, for more details, please check:
              https://pytorch.org/docs/stable/generated/torch.save.html#torch.save.
          pickle_protocol: can be specified to override the default protocol, default to `self.pickle_protocol`.
              this arg is used by `torch.save`, for more details, please check:
              https://pytorch.org/docs/stable/generated/torch.save.html#torch.save.
  
      """
      if key in self.cached and not self.allow_overwrite:
          raise RuntimeError(
              "Cached key already exists and overwriting is disabled."
          )
      if self.in_memory:
          self.cached.update({key: {"obj": copy.deepcopy(data_obj)}})
      else:
          fn = os.path.join(
              self.cache_dir, f"state_{key}_{id(self)}.pt"
          )
          self.cached.update({key: {"obj": fn}})
          torch.save(
              obj=data_obj,
              f=fn,
              pickle_module=self.pickle_module
              if pickle_module is None
              else pickle_module,
              pickle_protocol=self.pickle_protocol
              if pickle_protocol is None
              else pickle_protocol,
          )
          # store object's device if relevant
          if hasattr(data_obj, "device"):
              self.cached[key]["device"] = data_obj.device
  
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 2 for calling the StateCacher obj's underlying member method code with example usage: obj.store(key, data_obj, pickle_module, pickle_protocol) without return types