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: Constructor.
  
  Args:
      in_memory: boolean to determine if the object will be cached in memory or on
          disk.
      cache_dir: directory for data to be cached if `in_memory==False`. Defaults
          to using a temporary directory. Any created files will be deleted during
          the `StateCacher`'s destructor.
      allow_overwrite: allow the cache to be overwritten. If set to `False`, an
          error will be thrown if a matching already exists in the list of cached
          objects.
      pickle_module: module used for pickling metadata and objects, default to `pickle`.
          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 `2`.
          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: ["Constructor",".","Args",":","in_memory",":","boolean","to","determine","if","the","object","will","be","cached","in","memory","or","on","disk",".","cache_dir",":","directory","for","data","to","be","cached","if","`","in_memory==False","`",".","Defaults","to","using","a","temporary","directory",".","Any","created","files","will","be","deleted","during","the","`","StateCacher","`","'s","destructor",".","allow_overwrite",":","allow","the","cache","to","be","overwritten",".","If","set","to","`","False","`",",","an","error","will","be","thrown","if","a","matching","already","exists","in","the","list","of","cached","objects",".","pickle_module",":","module","used","for","pickling","metadata","and","objects",",","default","to","`","pickle","`",".","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","`","2","`",".","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 __init__(
      self,
      in_memory: bool,
      cache_dir: PathLike | None = None,
      allow_overwrite: bool = True,
      pickle_module: ModuleType = pickle,
      pickle_protocol: int = DEFAULT_PROTOCOL,
  ) -> None:
      """Constructor.
  
      Args:
          in_memory: boolean to determine if the object will be cached in memory or on
              disk.
          cache_dir: directory for data to be cached if `in_memory==False`. Defaults
              to using a temporary directory. Any created files will be deleted during
              the `StateCacher`'s destructor.
          allow_overwrite: allow the cache to be overwritten. If set to `False`, an
              error will be thrown if a matching already exists in the list of cached
              objects.
          pickle_module: module used for pickling metadata and objects, default to `pickle`.
              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 `2`.
              this arg is used by `torch.save`, for more details, please check:
              https://pytorch.org/docs/stable/generated/torch.save.html#torch.save.
  
      """
      self.in_memory = in_memory
      self.cache_dir = (
          tempfile.gettempdir() if cache_dir is None else cache_dir
      )
      if not os.path.isdir(self.cache_dir):
          raise ValueError(
              "Given `cache_dir` is not a valid directory."
          )
  
      self.allow_overwrite = allow_overwrite
      self.pickle_module = pickle_module
      self.pickle_protocol = pickle_protocol
      self.cached: dict = {}
  
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 1 to create a new StateCacher object  with example: obj = StateCacher(in_memory, cache_dir, allow_overwrite, pickle_module, pickle_protocol)