Skip to content

Feature: use hishel always revalidate option by default #224

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions githubkit/cache/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

from hishel import AsyncBaseStorage, BaseStorage, Controller

from githubkit.typing import HishelControllerOptions


class BaseCache(abc.ABC):
@abc.abstractmethod
Expand Down Expand Up @@ -42,12 +44,18 @@ def get_async_cache_storage(self) -> AsyncBaseCache:
"""
raise NotImplementedError

def get_hishel_controller(self) -> Optional[Controller]:
def get_hishel_controller_options(self) -> HishelControllerOptions:
"""Get the hishel controller options"""
# set always revalidate by default
# See: https://hishel.com/examples/github/
return HishelControllerOptions(always_revalidate=True)

def get_hishel_controller(self) -> Controller:
"""Get the hishel controller instance

Return `None` to use the default controller
Get the controller options from `get_hishel_controller_options` method
"""
return None
return Controller(**self.get_hishel_controller_options())

@abc.abstractmethod
def get_hishel_storage(self) -> BaseStorage:
Expand Down
15 changes: 9 additions & 6 deletions githubkit/cache/redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
from typing import TYPE_CHECKING, Any, NoReturn, Optional
from typing_extensions import override

from hishel import AsyncBaseStorage, AsyncRedisStorage, Controller, RedisStorage
from hishel import AsyncBaseStorage, AsyncRedisStorage, RedisStorage

from githubkit.exception import CacheUnsupportedError
from githubkit.typing import HishelControllerOptions
from githubkit.utils import hishel_key_generator_with_prefix

from .base import AsyncBaseCache, BaseCache, BaseCacheStrategy
Expand Down Expand Up @@ -82,14 +83,16 @@ def get_async_cache_storage(self) -> NoReturn:
)

@override
def get_hishel_controller(self) -> Optional[Controller]:
def get_hishel_controller_options(self) -> HishelControllerOptions:
options = super().get_hishel_controller_options()

if self.prefix is not None:
return Controller(
key_generator=partial(
hishel_key_generator_with_prefix, prefix=self.prefix
)
options["key_generator"] = partial(
hishel_key_generator_with_prefix, prefix=self.prefix
)

return options

@override
def get_hishel_storage(self) -> RedisStorage:
return RedisStorage(client=self.client)
Expand Down
20 changes: 20 additions & 0 deletions githubkit/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,29 @@
from datetime import timedelta
from typing import (
IO,
TYPE_CHECKING,
Annotated,
Callable,
Literal,
NamedTuple,
Optional,
TypedDict,
TypeVar,
Union,
)
from typing_extensions import TypeAlias

import httpcore
import httpx
from pydantic import Field

from .compat import PYDANTIC_V2
from .exception import GitHubException
from .utils import Unset

if TYPE_CHECKING:
from hishel._utils import BaseClock

T = TypeVar("T")
H = TypeVar("H", bound=Hashable)

Expand Down Expand Up @@ -89,3 +95,17 @@ class RetryOption(NamedTuple):


RetryDecisionFunc: TypeAlias = Callable[[GitHubException, int], RetryOption]


class HishelControllerOptions(TypedDict, total=False):
"""Options for the hishel controller."""

cacheable_methods: Optional[list[str]]
cacheable_status_codes: Optional[list[int]]
cache_private: bool
allow_heuristics: bool
clock: Optional["BaseClock"]
allow_stale: bool
always_revalidate: bool
force_cache: bool
key_generator: Optional[Callable[[httpcore.Request, Optional[bytes]], str]]