Skip to content
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

Add cache #373

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
62 changes: 62 additions & 0 deletions curl_cffi/requests/cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Inspired by requests-cache, but much simpler

import sqlite3
from abc import abstractmethod
from dataclasses import dataclass
from typing import Literal

CacheBackend = Literal["memory", "file", "sqlite", "redis"]


@dataclass
class CacheSettings:
backend: CacheBackend


class BaseBackend:
def __init__(self):
self.type = type

@abstractmethod
def __getitem__(self, key):
raise NotImplementedError()

@abstractmethod
def __setitem__(self, key, value):
raise NotImplementedError()

@abstractmethod
def __delitem__(self, key, value):
raise NotImplementedError()

@abstractmethod
def __iter__(self, key, value):
raise NotImplementedError()

@abstractmethod
def __len__(self, key, value):
raise NotImplementedError()

@abstractmethod
def clear(self, key, value):
raise NotImplementedError()


class MemeoryBackend(BaseBackend):
pass


class FileBackend(BaseBackend):
pass


class SqliteBackend(BaseBackend):
def __init__(self, filename: str, **kwargs):
self.filename = filename
self._db = sqlite3.connect(**kwargs)


class RedisBackend(BaseBackend):
def ___init__(self, host: str, port: int):
self.host = host
self.port = port
9 changes: 8 additions & 1 deletion curl_cffi/requests/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,15 @@
)
from urllib.parse import ParseResult, parse_qsl, quote, unquote, urlencode, urljoin, urlparse

from typing_extensions import Unpack
# Unpack introduced in 3.11
try:
from typing import Unpack
except ImportError:
from typing_extensions import Unpack

from .. import AsyncCurl, Curl, CurlError, CurlHttpVersion, CurlInfo, CurlOpt, CurlSslVersion
from ..curl import CURL_WRITEFUNC_ERROR, CurlMime
from .cache import CacheStrategy
from .cookies import Cookies, CookieTypes, CurlMorsel
from .exceptions import ImpersonateError, RequestException, SessionClosed, code2error
from .headers import Headers, HeaderTypes
Expand Down Expand Up @@ -221,6 +226,7 @@ def __init__(
debug: bool = False,
interface: Optional[str] = None,
cert: Optional[Union[str, Tuple[str, str]]] = None,
cache: Optional[CacheStrategy] = None,
):
self.headers = Headers(headers)
self.cookies = Cookies(cookies)
Expand All @@ -244,6 +250,7 @@ def __init__(
self.debug = debug
self.interface = interface
self.cert = cert
self.cache = cache

if proxy and proxies:
raise TypeError("Cannot specify both 'proxy' and 'proxies'")
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ license = { file = "LICENSE" }
dependencies = [
"cffi>=1.12.0",
"certifi>=2024.2.2",
"typing_extensions>=4.7.0",
]
readme = "README.md"
requires-python = ">=3.8"
Expand Down
Empty file added tests/unittest/test_cache.py
Empty file.
Loading