Skip to content

Commit fb4d115

Browse files
authored
✨ Feature: use hishel always revalidate option by default (#224)
1 parent 7e4b00e commit fb4d115

3 files changed

Lines changed: 40 additions & 9 deletions

File tree

githubkit/cache/base.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
from hishel import AsyncBaseStorage, BaseStorage, Controller
66

7+
from githubkit.typing import HishelControllerOptions
8+
79

810
class BaseCache(abc.ABC):
911
@abc.abstractmethod
@@ -42,12 +44,18 @@ def get_async_cache_storage(self) -> AsyncBaseCache:
4244
"""
4345
raise NotImplementedError
4446

45-
def get_hishel_controller(self) -> Optional[Controller]:
47+
def get_hishel_controller_options(self) -> HishelControllerOptions:
48+
"""Get the hishel controller options"""
49+
# set always revalidate by default
50+
# See: https://hishel.com/examples/github/
51+
return HishelControllerOptions(always_revalidate=True)
52+
53+
def get_hishel_controller(self) -> Controller:
4654
"""Get the hishel controller instance
4755
48-
Return `None` to use the default controller
56+
Get the controller options from `get_hishel_controller_options` method
4957
"""
50-
return None
58+
return Controller(**self.get_hishel_controller_options())
5159

5260
@abc.abstractmethod
5361
def get_hishel_storage(self) -> BaseStorage:

githubkit/cache/redis.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
from typing import TYPE_CHECKING, Any, NoReturn, Optional
44
from typing_extensions import override
55

6-
from hishel import AsyncBaseStorage, AsyncRedisStorage, Controller, RedisStorage
6+
from hishel import AsyncBaseStorage, AsyncRedisStorage, RedisStorage
77

88
from githubkit.exception import CacheUnsupportedError
9+
from githubkit.typing import HishelControllerOptions
910
from githubkit.utils import hishel_key_generator_with_prefix
1011

1112
from .base import AsyncBaseCache, BaseCache, BaseCacheStrategy
@@ -82,14 +83,16 @@ def get_async_cache_storage(self) -> NoReturn:
8283
)
8384

8485
@override
85-
def get_hishel_controller(self) -> Optional[Controller]:
86+
def get_hishel_controller_options(self) -> HishelControllerOptions:
87+
options = super().get_hishel_controller_options()
88+
8689
if self.prefix is not None:
87-
return Controller(
88-
key_generator=partial(
89-
hishel_key_generator_with_prefix, prefix=self.prefix
90-
)
90+
options["key_generator"] = partial(
91+
hishel_key_generator_with_prefix, prefix=self.prefix
9192
)
9293

94+
return options
95+
9396
@override
9497
def get_hishel_storage(self) -> RedisStorage:
9598
return RedisStorage(client=self.client)

githubkit/typing.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,29 @@
22
from datetime import timedelta
33
from typing import (
44
IO,
5+
TYPE_CHECKING,
56
Annotated,
67
Callable,
78
Literal,
89
NamedTuple,
910
Optional,
11+
TypedDict,
1012
TypeVar,
1113
Union,
1214
)
1315
from typing_extensions import TypeAlias
1416

17+
import httpcore
1518
import httpx
1619
from pydantic import Field
1720

1821
from .compat import PYDANTIC_V2
1922
from .exception import GitHubException
2023
from .utils import Unset
2124

25+
if TYPE_CHECKING:
26+
from hishel._utils import BaseClock
27+
2228
T = TypeVar("T")
2329
H = TypeVar("H", bound=Hashable)
2430

@@ -89,3 +95,17 @@ class RetryOption(NamedTuple):
8995

9096

9197
RetryDecisionFunc: TypeAlias = Callable[[GitHubException, int], RetryOption]
98+
99+
100+
class HishelControllerOptions(TypedDict, total=False):
101+
"""Options for the hishel controller."""
102+
103+
cacheable_methods: Optional[list[str]]
104+
cacheable_status_codes: Optional[list[int]]
105+
cache_private: bool
106+
allow_heuristics: bool
107+
clock: Optional["BaseClock"]
108+
allow_stale: bool
109+
always_revalidate: bool
110+
force_cache: bool
111+
key_generator: Optional[Callable[[httpcore.Request, Optional[bytes]], str]]

0 commit comments

Comments
 (0)