Skip to content

Commit 5c150b4

Browse files
committed
fix: Application caching use Content class directly
1 parent 9db6956 commit 5c150b4

2 files changed

Lines changed: 20 additions & 5 deletions

File tree

oembedpy/application.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import logging
55
import pickle
66
import time
7-
from typing import Dict, Optional
7+
from typing import Dict, Optional, Union
88

99
import httpx
1010
from platformdirs import PlatformDirs
@@ -20,7 +20,7 @@ class Oembed:
2020
"""Application of oEmbed."""
2121

2222
_registry: ProviderRegistry
23-
_cache: Dict[consumer.RequestParameters, CachedContent]
23+
_cache: Dict[consumer.RequestParameters, Union[Content, CachedContent]]
2424
_fallback_type: bool
2525

2626
def __init__(self, fallback_type: bool = False): # noqa: D107
@@ -51,11 +51,17 @@ def fetch(
5151
params.max_height = max_height
5252
#
5353
now = time.mktime(time.localtime())
54-
if params in self._cache and now <= self._cache[params].expired:
55-
return self._cache[params].content
54+
if params in self._cache:
55+
# For comptibility CachedContent
56+
val = self._cache[params]
57+
if isinstance(val, CachedContent):
58+
if now <= val.expired:
59+
return val.content
60+
elif now <= val._expired:
61+
return val
5662
content = consumer.fetch_content(api_url, params, self._fallback_type)
5763
if content.cache_age:
58-
self._cache[params] = CachedContent(now + int(content.cache_age), content)
64+
self._cache[params] = content
5965
return content
6066

6167

oembedpy/types.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,5 +130,14 @@ class HtmlOnly(_Internals, _Optionals, _HtmlOnly):
130130

131131

132132
class CachedContent(NamedTuple):
133+
"""Content object with expired timestamp for cache.
134+
135+
.. deprecated:: 0.9.0
136+
137+
This is internal class for cache, so it keeps to avoid breaking cache data.
138+
I will remove for v1.
139+
Use :class:`Content` instead if you use in other projects.
140+
"""
141+
133142
expired: float
134143
content: Content

0 commit comments

Comments
 (0)