code: def test_scipy__cache_behaviour() -> None:
      opt = gpflow.optimizers.Scipy()
      variables1 = _create_variables()
      variables2 = _create_variables()
      counter1 = [0]  # Each closure has its own counter
      counter2 = [0]
      closure1 = _loss_closure_builder(counter1, variables1)
      closure2 = _loss_closure_builder(counter2, variables2)
  
      # Ensure the cache is empty
      assert len(opt.compile_cache) == 0
  
      # Call eval_func multiple times with the same arguments
      for _ in range(4):
          opt.minimize(
              closure1,
              variables1,
              compile=True,
              tf_fun_args={},
              allow_unused_variables=False,
          )
          # Check that the cache was used
          assert len(opt.compile_cache) == 1
  
      # Get the key of the cached tf.function
      key = list(opt.compile_cache.keys())[0]
  
      # Assert that the key is as expected
      assert key == (
          closure1,
          tuple(id(var) for var in variables1),
          frozenset({}.items()),
          False,
      )
  
      # Call eval_func multiple times with the same arguments, but without compile. This should
      # not affect the cache.
      for _ in range(4):
          opt.minimize(
              closure1,
              variables1,
              compile=False,
              tf_fun_args={},
              allow_unused_variables=False,
          )
          # Check that the cache size did not change
          assert len(opt.compile_cache) == 1
  
      # Fill the cache with closure2
      for _ in range(4):
          opt.minimize(
              closure2,
              variables2,
              compile=True,
              tf_fun_args={},
              allow_unused_variables=False,
          )
          # Check that the cache size did change
          assert len(opt.compile_cache) == 2
  
      # The closure should be run
      #   1 (during the first compilation) + 4 (calls without compilation) times
      assert counter1[0] == 1 + 4
      # The closure should only be run once (during the first compilation)
      assert counter2[0] == 1
  
imports: import unittest
  import warnings
  from typing import Any, Callable, Dict, List, Sequence, Tuple
  from unittest.mock import MagicMock
  import numpy
  import pytest
  import tensorflow
  from packaging.version import Version
  import gpflow
  from gpflow.base import AnyNDArray
  from gpflow.config import default_float
  from gpflow.models import GPR, GPModel
  from gpflow.optimizers.scipy import LossClosure
label_desc: Use image node_id 10 for calling a global function with example usage: test_scipy__cache_behaviour() without return types