Repository: streamlit/streamlit
File: lib/streamlit/runtime/caching/cache_resource_api.py (Python)

Code before:
                    hash_funcs=hash_funcs,
                    on_release=on_release,
                    scope=scope,
                )
            )

        return make_cached_func_wrapper(
            CachedResourceFuncInfo(
                func=func,
                show_spinner=show_spinner,
                show_time=show_time,
                max_entries=max_entries,
                ttl=ttl,
                validate=validate,
                hash_funcs=hash_funcs,
                on_release=on_release,
                scope=scope,
            )
        )

    @gather_metrics("clear_resource_caches")
    def clear(self) -> None:
        """Clear all cache_resource caches."""
        _resource_caches.clear_all()

    def clear_session(self, session_id: str) -> None:
        """Clears all caches for the given session ID."""
        _resource_caches.clear_session(session_id)


class ResourceCache(Cache[R]):
    """Manages cached values for a single st.cache_resource function."""

    def __init__(
        self,
        key: str,
        max_entries: float,
        ttl_seconds: float,
        validate: ValidateFunc | None,
        display_name: str,
        on_release: OnRelease,
    ) -> None:
        super().__init__()

        def wrapped_on_release(result: CachedResult[R]) -> None:
            # Note that exceptions raised here will bubble out to the calling scope,
            # which will then treat them as user script errors.
            # This is also how exceptions thrown when generating cache values are
            # treated.
            on_release(result.value)


Review comment (suggestion):
**question:** is `clear_session` supposed to be exposed to users via the public API? I think there isn't even an official API to retrieve the session ID. In case it should be exposed, it would be good to add a `gather_metrics` decorator to track its usage. cc @jrieke 

Code after:
                    show_spinner=show_spinner,
                    show_time=show_time,
                    max_entries=max_entries,
                    ttl=ttl,
                    validate=validate,
                    hash_funcs=hash_funcs,
                    on_release=on_release,
                    scope=scope,
                )
            )

        return make_cached_func_wrapper(
            CachedResourceFuncInfo(
                func=func,
                show_spinner=show_spinner,
                show_time=show_time,
                max_entries=max_entries,
                ttl=ttl,
                validate=validate,
                hash_funcs=hash_funcs,
                on_release=on_release,
                scope=scope,
            )
        )

    @gather_metrics("clear_resource_caches")
    def clear(self) -> None:
        """Clear all cache_resource caches."""
        _resource_caches.clear_all()


class ResourceCache(Cache[R]):
    """Manages cached values for a single st.cache_resource function."""

    def __init__(
        self,
        key: str,
        max_entries: float,
        ttl_seconds: float,
        validate: ValidateFunc | None,
        display_name: str,
        on_release: OnRelease,
    ) -> None:
        super().__init__()

        def wrapped_on_release(result: CachedResult[R]) -> None:
            # Note that exceptions raised here will bubble out to the calling scope,
            # which will then treat them as user script errors.
            # This is also how exceptions thrown when generating cache values are
            # treated.
            on_release(result.value)