code: def get_cached_dataset(url, version=None, keys=None):
      """Helper to get a cached clone from url
  
      Intended for use from within `cached_dataset` and `cached_url` decorators.
      Clones `url` into user's cache under datalad/tests/`name`. If such a clone
      already exists, don't clone but return the existing one. So, it's supposed
      to cache the original source in order to reduce time and traffic for tests,
      by letting subsequent requests clone from a local location directly.
  
      If it's an annex get the content as provided by `keys`, too.
      Note, that as a transparent cache replacing the repo at URL from the POV of
      a test, we can't address content via paths, since those are valid only with
      respect to a particular worktree. If different tests clone from the same
      cached dataset, each requesting different versions and different paths
      thereof, we run into trouble if the cache itself checks out a particular
      requested version.
  
      Verifies that `version` can be checked out, but doesn't actually do it,
      since the cached dataset is intended to be used as origin instead of the
      original remote at URL by the `cached_dataset` test decorator. Checkout of
      a particular version should happen in its clone.
  
      Parameters
      ----------
      url: str
          URL to clone from
      keys: str or list or None
          (list of) annex keys to get content for.
      version: str or None
          A commit or an object that can be dereferenced to one.
  
      Returns
      -------
      Dataset
      """
  
      # TODO: What about recursive? Might be complicated. We would need to make
      #       sure we can recursively clone _from_ here then, potentially
      #       requiring submodule URL rewrites. Not sure about that ATM.
  
      # TODO: Given that it is supposed to be a cache for the original repo at
      #       `url`, we prob. should make this a bare repository. We don't need
      #       a potentially expensive checkout here. Need to double check
      #       `annex-get --key` in bare repos, though. Plus datalad-clone doesn't
      #       have --bare yet. But we want all the annex/special-remote/ria magic
      #       of datalad. So, plain git-clone --bare is not an option.
  
      if not DATALAD_TESTS_CACHE:
          raise ValueError("Caching disabled by config")
  
      ds = Dataset(DATALAD_TESTS_CACHE / url2filename(url))
  
      if not ds.is_installed():
          ds = Clone()(url, ds.pathobj)
  
      # When/How to update a dataset in cache? If version is a commit SHA and we
      # have it, there's no need for an update. Otherwise it gets tricky, because
      # this is a cache, not a checkout a test would operate on. It needs to
      # behave as if it was the thing at `url` from the point of view of the test
      # using it (cloning/getting content from here). We would need to update all
      # references, not just fetch them!
      #
      # Can we even (cheaply) tell whether `version` is an absolute reference
      # (actual SHA, not a branch/tag)?
      #
      # NOTE: - consider git-clone --mirror, but as w/ --bare: not an option for
      #         datalad-clone yet.
      #       - --reference[-if-able] might also be worth thinking about for
      #         the clone @cached_dataset creates wrt clone in cacheq
      #
      # So, for now fetch, figure whether there actually was something to fetch
      # and if so simply invalidate cache and re-clone/get. Don't overcomplicate
      # things. It's about datasets used in the tests - they shouldn't change too
      # frequently.
      elif any(
          "uptodate" not in c["operations"]
          for c in ds.repo.fetch(DEFAULT_REMOTE)
      ):
          rmtree(ds.path)
          ds = Clone()(url, ds.pathobj)
  
      if version:
          # check whether version is available
          assert ds.repo.commit_exists(version)
      if keys and AnnexRepo.is_valid_repo(ds.path):
          ds.repo.get(keys, key=True)
  
      return ds
  
file_path: datalad/datalad/tests/utils_cached_dataset.py
imports: from datalad import cfg
  from datalad.core.distributed.clone import Clone
  from datalad.distribution.dataset import Dataset
  from datalad.support.annexrepo import AnnexRepo
  from datalad.tests.utils_pytest import DEFAULT_REMOTE, with_tempfile
  from datalad.utils import Path, better_wraps, ensure_list, optional_args, rmtree
label_desc: Use image node_id 2 for calling a global function with example usage: get_cached_dataset(url, version, keys) and returns: ds