def _get_cached_transport(cls, service_name, metrics, settings, cache_time_in_seconds): """ Caches configured transports for up to cache_time_in_seconds to prevent the bottleneck. """ cache_key = (service_name, dict_to_hashable(settings)) if cache_key not in cls._transport_cache or cls._transport_cache[cache_key][0] < time.time(): cls._transport_cache[cache_key] = ( time.time() + cache_time_in_seconds, cls._construct_transport(service_name, metrics, settings, 'client.transport.initialize_cache'), ) return cls._transport_cache[cache_key][1]
def backend_layer(self): # type: () -> BaseRedisClient if self._backend_layer is None: cache_key = (self.backend_type, dict_to_hashable(cast(Dict[Hashable, Any], self.backend_layer_kwargs))) if cache_key not in self._backend_layer_cache: with self._get_timer('backend.initialize'): backend_layer_kwargs = deepcopy(self.backend_layer_kwargs) if self.backend_type == REDIS_BACKEND_TYPE_SENTINEL: self._backend_layer_cache[cache_key] = SentinelRedisClient(**backend_layer_kwargs) else: self._backend_layer_cache[cache_key] = StandardRedisClient(**backend_layer_kwargs) self._backend_layer = self._backend_layer_cache[cache_key] # Each time the backend layer is accessed, use _this_ transport's metrics recorder for the backend layer self._backend_layer.metrics_counter_getter = self._get_counter return self._backend_layer
def test_that_it_works(self): d1 = { u'hosts': [u'1.2.3.4', u'5.6.7.8', u'9.10.11.12'], u'connection_kwargs': { u'password': u'abc123meta', u'socket_connect_timeout': 20 }, u'sentinel_services': [u'service1', u'service2'], u'sentinel_failover_retries': 6, } # type: Dict[Hashable, Any] d2 = { u'sentinel_services': [u'service1', u'service2'], u'sentinel_failover_retries': 6, u'connection_kwargs': { u'password': u'abc123meta', u'socket_connect_timeout': 20 }, u'hosts': [u'1.2.3.4', u'5.6.7.8', u'9.10.11.12'], } # type: Dict[Hashable, Any] d3 = { u'connection_kwargs': { u'socket_connect_timeout': 20, u'password': u'abc123meta' }, u'sentinel_services': [u'service1', u'service2'], u'sentinel_failover_retries': 6, u'hosts': [u'1.2.3.4', u'5.6.7.8', u'9.10.11.12'], } # type: Dict[Hashable, Any] d4 = { u'connection_kwargs': { u'socket_connect_timeout': 20, u'password': u'abc123meta' }, u'sentinel_services': [u'service1', u'service2'], u'sentinel_failover_retries': 6, u'hosts': [u'1.2.3.4', u'5.6.7.8', u'9.10.11.1'], # <- only difference is here } # type: Dict[Hashable, Any] self.assertEqual(d1, d2) self.assertEqual(d1, d3) self.assertNotEqual(d1, d4) self.assertNotEqual(d2, d4) self.assertNotEqual(d3, d4) self.assertEqual(dict_to_hashable(d1), dict_to_hashable(d2)) self.assertEqual(dict_to_hashable(d1), dict_to_hashable(d3)) self.assertNotEqual(dict_to_hashable(d1), dict_to_hashable(d4)) self.assertNotEqual(dict_to_hashable(d2), dict_to_hashable(d4)) self.assertNotEqual(dict_to_hashable(d3), dict_to_hashable(d4)) cache = {dict_to_hashable(d1): 'hello'} # type: Dict[Hashable, Any] self.assertIn(dict_to_hashable(d1), cache) self.assertIn(dict_to_hashable(d2), cache) self.assertIn(dict_to_hashable(d3), cache) self.assertNotIn(dict_to_hashable(d4), cache) cache[dict_to_hashable(d4)] = 'goodbye' self.assertIn(dict_to_hashable(d1), cache) self.assertIn(dict_to_hashable(d2), cache) self.assertIn(dict_to_hashable(d3), cache) self.assertIn(dict_to_hashable(d4), cache) self.assertEqual('hello', cache[dict_to_hashable(d1)]) self.assertEqual('hello', cache[dict_to_hashable(d2)]) self.assertEqual('hello', cache[dict_to_hashable(d3)]) self.assertEqual('goodbye', cache[dict_to_hashable(d4)]) self.assertEqual(2, len(cache)) cache = {('a_type', dict_to_hashable(d1)): 'hello'} self.assertIn(('a_type', dict_to_hashable(d1)), cache) self.assertIn(('a_type', dict_to_hashable(d2)), cache) self.assertIn(('a_type', dict_to_hashable(d3)), cache) self.assertNotIn(('b_type', dict_to_hashable(d1)), cache) self.assertNotIn(('a_type', dict_to_hashable(d4)), cache) cache[('a_type', dict_to_hashable(d4))] = 'goodbye' self.assertIn(('a_type', dict_to_hashable(d1)), cache) self.assertIn(('a_type', dict_to_hashable(d2)), cache) self.assertIn(('a_type', dict_to_hashable(d3)), cache) self.assertNotIn(('b_type', dict_to_hashable(d1)), cache) self.assertIn(('a_type', dict_to_hashable(d4)), cache) self.assertEqual('hello', cache[('a_type', dict_to_hashable(d1))]) self.assertEqual('hello', cache[('a_type', dict_to_hashable(d2))]) self.assertEqual('hello', cache[('a_type', dict_to_hashable(d3))]) self.assertEqual('goodbye', cache[('a_type', dict_to_hashable(d4))]) self.assertEqual(2, len(cache))