@@ -200,74 +200,6 @@ def _connection_reduce_fn(val,import_fn):
200200_DEFAULT_TLS_SESSION_CACHE_TTL = 3600 # 1 hour in seconds
201201
202202
203- # Abstract base classes for TLS session caching - implementations in cassandra.tls
204- from abc import ABC , abstractmethod
205-
206-
207- class TLSSessionCacheBase (ABC ):
208- """
209- Abstract base class for TLS session caching.
210-
211- Implementations should provide thread-safe caching of TLS sessions
212- to enable session resumption for faster reconnections.
213- """
214-
215- @abstractmethod
216- def get_session (self , endpoint ):
217- """
218- Get a cached TLS session for the given endpoint.
219-
220- Args:
221- endpoint: The EndPoint object representing the connection target
222-
223- Returns:
224- ssl.SSLSession object if a valid cached session exists, None otherwise
225- """
226- pass
227-
228- @abstractmethod
229- def set_session (self , endpoint , session ):
230- """
231- Store a TLS session for the given endpoint.
232-
233- Args:
234- endpoint: The EndPoint object representing the connection target
235- session: The ssl.SSLSession object to cache
236- """
237- pass
238-
239- @abstractmethod
240- def clear_expired (self ):
241- """Remove all expired sessions from the cache."""
242- pass
243-
244- @abstractmethod
245- def clear (self ):
246- """Clear all sessions from the cache."""
247- pass
248-
249- @abstractmethod
250- def size (self ):
251- """Return the current number of cached sessions."""
252- pass
253-
254-
255- class TLSSessionCacheOptionsBase (ABC ):
256- """
257- Abstract base class for TLS session cache configuration options.
258- """
259-
260- @abstractmethod
261- def create_cache (self ):
262- """
263- Build and return a TLSSessionCache implementation.
264-
265- Returns:
266- TLSSessionCache: A configured session cache instance
267- """
268- pass
269-
270-
271203class NoHostAvailable (Exception ):
272204 """
273205 Raised when an operation is attempted but all connections are
@@ -947,29 +879,56 @@ def default_retry_policy(self, policy):
947879 .. versionadded:: 3.17.0
948880 """
949881
882+ tls_session_cache_enabled = True
883+ """
884+ Enable or disable TLS session caching for faster reconnections.
885+ When enabled (default), TLS sessions are cached and reused for subsequent
886+ connections to the same endpoint, reducing handshake latency.
887+
888+ Set to False to disable session caching entirely.
889+
890+ .. versionadded:: 3.30.0
891+ """
892+
893+ tls_session_cache_size = _DEFAULT_TLS_SESSION_CACHE_SIZE
894+ """
895+ Maximum number of TLS sessions to cache. Default is 100.
896+ When the cache is full, the least recently used session is evicted.
897+
898+ .. versionadded:: 3.30.0
899+ """
900+
901+ tls_session_cache_ttl = _DEFAULT_TLS_SESSION_CACHE_TTL
902+ """
903+ Time-to-live for cached TLS sessions in seconds. Default is 3600 (1 hour).
904+ Sessions older than this value will not be reused.
905+
906+ .. versionadded:: 3.30.0
907+ """
908+
950909 tls_session_cache_options = None
951910 """
952- TLS session cache configuration. Set to an instance of :class:`~.TLSSessionCacheOptions`
953- to configure session caching behavior. If None (default), a default configuration will
954- be used when SSL/TLS is enabled .
955-
956- The default configuration caches sessions by host and port, with a maximum of 100 sessions
957- and a TTL of 3600 seconds (1 hour) .
958-
959- To disable session caching entirely, set this to False .
960-
911+ Advanced TLS session cache configuration. Set to an instance of
912+ :class:`~cassandra.tls.TLSSessionCacheOptions` for fine-grained control over
913+ session caching behavior (e.g., cache_by_host_only option) .
914+
915+ If None (default), a cache is created using :attr:`~.tls_session_cache_size`
916+ and :attr:`~.tls_session_cache_ttl` when SSL/TLS is enabled .
917+
918+ This option takes precedence over the individual tls_session_cache_* parameters .
919+
961920 Example::
962-
921+
963922 from cassandra.tls import TLSSessionCacheOptions
964-
923+
965924 # Cache by host only (ignoring port)
966925 options = TLSSessionCacheOptions(
967926 max_size=200,
968927 ttl=7200,
969928 cache_by_host_only=True
970929 )
971930 cluster = Cluster(ssl_context=ssl_context, tls_session_cache_options=options)
972-
931+
973932 .. versionadded:: 3.30.0
974933 """
975934
@@ -1302,6 +1261,9 @@ def __init__(self,
13021261 idle_heartbeat_timeout = 30 ,
13031262 no_compact = False ,
13041263 ssl_context = None ,
1264+ tls_session_cache_enabled = True ,
1265+ tls_session_cache_size = _DEFAULT_TLS_SESSION_CACHE_SIZE ,
1266+ tls_session_cache_ttl = _DEFAULT_TLS_SESSION_CACHE_TTL ,
13051267 tls_session_cache_options = None ,
13061268 endpoint_factory = None ,
13071269 application_name = None ,
@@ -1519,25 +1481,28 @@ def __init__(self,
15191481
15201482 self .ssl_options = ssl_options
15211483 self .ssl_context = ssl_context
1484+ self .tls_session_cache_enabled = tls_session_cache_enabled
1485+ self .tls_session_cache_size = tls_session_cache_size
1486+ self .tls_session_cache_ttl = tls_session_cache_ttl
15221487 self .tls_session_cache_options = tls_session_cache_options
1523-
1524- # Initialize TLS session cache if SSL is enabled
1488+
1489+ # Initialize TLS session cache if SSL is enabled and caching is enabled
15251490 self ._tls_session_cache = None
1526- if (ssl_context or ssl_options ) and tls_session_cache_options is not False :
1491+ if (ssl_context or ssl_options ) and tls_session_cache_enabled :
15271492 from cassandra .tls import TLSSessionCacheOptions
1528-
1529- # Use provided options or create default
1530- if tls_session_cache_options is None :
1493+
1494+ # Use provided options object or create one from individual parameters
1495+ if tls_session_cache_options is not None :
1496+ cache_options = tls_session_cache_options
1497+ else :
15311498 cache_options = TLSSessionCacheOptions (
1532- max_size = _DEFAULT_TLS_SESSION_CACHE_SIZE ,
1533- ttl = _DEFAULT_TLS_SESSION_CACHE_TTL ,
1499+ max_size = tls_session_cache_size ,
1500+ ttl = tls_session_cache_ttl ,
15341501 cache_by_host_only = False
15351502 )
1536- else :
1537- cache_options = tls_session_cache_options
1538-
1503+
15391504 self ._tls_session_cache = cache_options .create_cache ()
1540-
1505+
15411506 self .sockopts = sockopts
15421507 self .cql_version = cql_version
15431508 self .max_schema_agreement_wait = max_schema_agreement_wait
0 commit comments