CSOCache

CSOCache()

Manage the HTTP response cache for CSO API requests.

This class provides a convenient interface for inspecting and controlling the cache used by pycsodata. The cache stores HTTP responses to reduce API calls and improve performance.

The cache is shared across all instances of CSOCache, CSODataset, and CSOCatalogue. Operations on one instance affect all users of the cache.

Examples

>>> from pycsodata import CSOCache
>>> cache = CSOCache()
>>>
>>> # Check cache statistics
>>> info = cache.info()
>>> print(f"Cache contains {info.size} entries")
>>>
>>> # Clear the cache to force fresh API requests
>>> cache.flush()
>>> print("Cache cleared")

Methods

Name Description
flush Clear all cached HTTP responses.
info Get information about the current cache state.

flush

CSOCache.flush()

Clear all cached HTTP responses.

This forces subsequent API calls to fetch fresh data from the CSO. Useful when you know data has been updated or during testing.

Note

This affects all CSODataset and CSOCatalogue instances, as they share the same cache.

Examples

>>> cache = CSOCache()
>>> cache.flush()  # Clear all cached data
>>> # Now all API calls will fetch fresh data

info

CSOCache.info()

Get information about the current cache state.

Returns

Name Type Description
CacheInfo A CacheInfo object containing: - size: Current number of cached responses. - maxsize: Maximum cache capacity. - ttl_seconds: Time-to-live for cached entries in seconds. - hit_rate: Ratio of cache hits to total requests, or None.

Examples

>>> cache = CSOCache()
>>> info = cache.info()
>>> print(f"Cache is {info.size / info.maxsize:.0%} full")
>>> if info.hit_rate:
...     print(f"Hit rate: {info.hit_rate:.1%}")