fastapi-cachex 0.1.1__tar.gz → 0.1.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of fastapi-cachex might be problematic. Click here for more details.
- {fastapi_cachex-0.1.1 → fastapi_cachex-0.1.2}/PKG-INFO +48 -5
- {fastapi_cachex-0.1.1 → fastapi_cachex-0.1.2}/README.md +45 -4
- {fastapi_cachex-0.1.1 → fastapi_cachex-0.1.2}/fastapi_cachex/backends/__init__.py +1 -0
- fastapi_cachex-0.1.2/fastapi_cachex/backends/memcached.py +74 -0
- {fastapi_cachex-0.1.1 → fastapi_cachex-0.1.2}/fastapi_cachex/proxy.py +9 -3
- {fastapi_cachex-0.1.1 → fastapi_cachex-0.1.2}/fastapi_cachex.egg-info/PKG-INFO +48 -5
- {fastapi_cachex-0.1.1 → fastapi_cachex-0.1.2}/fastapi_cachex.egg-info/SOURCES.txt +3 -1
- fastapi_cachex-0.1.2/fastapi_cachex.egg-info/requires.txt +5 -0
- {fastapi_cachex-0.1.1 → fastapi_cachex-0.1.2}/pyproject.toml +5 -1
- fastapi_cachex-0.1.2/tests/test_proxybackend.py +101 -0
- fastapi_cachex-0.1.1/fastapi_cachex.egg-info/requires.txt +0 -2
- {fastapi_cachex-0.1.1 → fastapi_cachex-0.1.2}/LICENSE +0 -0
- {fastapi_cachex-0.1.1 → fastapi_cachex-0.1.2}/fastapi_cachex/__init__.py +0 -0
- {fastapi_cachex-0.1.1 → fastapi_cachex-0.1.2}/fastapi_cachex/backends/base.py +0 -0
- {fastapi_cachex-0.1.1 → fastapi_cachex-0.1.2}/fastapi_cachex/backends/memory.py +0 -0
- {fastapi_cachex-0.1.1 → fastapi_cachex-0.1.2}/fastapi_cachex/cache.py +0 -0
- {fastapi_cachex-0.1.1 → fastapi_cachex-0.1.2}/fastapi_cachex/directives.py +0 -0
- {fastapi_cachex-0.1.1 → fastapi_cachex-0.1.2}/fastapi_cachex/exceptions.py +0 -0
- {fastapi_cachex-0.1.1 → fastapi_cachex-0.1.2}/fastapi_cachex/types.py +0 -0
- {fastapi_cachex-0.1.1 → fastapi_cachex-0.1.2}/fastapi_cachex.egg-info/dependency_links.txt +0 -0
- {fastapi_cachex-0.1.1 → fastapi_cachex-0.1.2}/fastapi_cachex.egg-info/top_level.txt +0 -0
- {fastapi_cachex-0.1.1 → fastapi_cachex-0.1.2}/setup.cfg +0 -0
- {fastapi_cachex-0.1.1 → fastapi_cachex-0.1.2}/tests/test_cache.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: fastapi-cachex
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.2
|
|
4
4
|
Summary: A caching library for FastAPI with support for Cache-Control, ETag, and multiple backends.
|
|
5
5
|
Author-email: Allen <s96016641@gmail.com>
|
|
6
6
|
License-Expression: Apache-2.0
|
|
@@ -25,6 +25,8 @@ Description-Content-Type: text/markdown
|
|
|
25
25
|
License-File: LICENSE
|
|
26
26
|
Requires-Dist: fastapi
|
|
27
27
|
Requires-Dist: httpx
|
|
28
|
+
Provides-Extra: memcache
|
|
29
|
+
Requires-Dist: pymemcache; extra == "memcache"
|
|
28
30
|
Dynamic: license-file
|
|
29
31
|
|
|
30
32
|
# FastAPI-Cache X
|
|
@@ -34,8 +36,12 @@ Dynamic: license-file
|
|
|
34
36
|
[](https://github.com/allen0099/FastAPI-CacheX/actions/workflows/test.yml)
|
|
35
37
|
[](https://github.com/allen0099/FastAPI-CacheX/actions/workflows/test.yml)
|
|
36
38
|
|
|
37
|
-
[](https://pepy.tech/project/fastapi-cachex)
|
|
40
|
+
[](https://pepy.tech/project/fastapi-cachex)
|
|
41
|
+
[](https://pepy.tech/project/fastapi-cachex)
|
|
42
|
+
|
|
43
|
+
[](https://pypi.org/project/fastapi-cachex)
|
|
44
|
+
[](https://pypi.org/project/fastapi-cachex/)
|
|
39
45
|
|
|
40
46
|
[English](README.md) | [繁體中文](docs/README.zh-TW.md)
|
|
41
47
|
|
|
@@ -72,17 +78,54 @@ uv pip install fastapi-cachex
|
|
|
72
78
|
|
|
73
79
|
```python
|
|
74
80
|
from fastapi import FastAPI
|
|
75
|
-
from fastapi_cachex import cache
|
|
81
|
+
from fastapi_cachex import cache, BackendProxy
|
|
82
|
+
from fastapi_cachex.backends import MemoryBackend, MemcachedBackend
|
|
76
83
|
|
|
77
84
|
app = FastAPI()
|
|
78
85
|
|
|
86
|
+
# Configure your cache backend
|
|
87
|
+
memory_backend = MemoryBackend() # In-memory cache
|
|
88
|
+
# or
|
|
89
|
+
memcached_backend = MemcachedBackend(servers=["localhost:11211"]) # Memcached
|
|
90
|
+
|
|
91
|
+
# Set the backend you want to use
|
|
92
|
+
BackendProxy.set_backend(memory_backend) # or memcached_backend
|
|
93
|
+
|
|
79
94
|
|
|
80
95
|
@app.get("/")
|
|
81
|
-
@cache()
|
|
96
|
+
@cache(ttl=60) # Cache for 60 seconds
|
|
82
97
|
async def read_root():
|
|
83
98
|
return {"Hello": "World"}
|
|
84
99
|
```
|
|
85
100
|
|
|
101
|
+
## Backend Configuration
|
|
102
|
+
|
|
103
|
+
FastAPI-CacheX supports multiple caching backends. You can easily switch between them using the `BackendProxy`.
|
|
104
|
+
|
|
105
|
+
### In-Memory Cache
|
|
106
|
+
|
|
107
|
+
```python
|
|
108
|
+
from fastapi_cachex.backends import MemoryBackend
|
|
109
|
+
from fastapi_cachex import BackendProxy
|
|
110
|
+
|
|
111
|
+
backend = MemoryBackend()
|
|
112
|
+
BackendProxy.set_backend(backend)
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### Memcached
|
|
116
|
+
|
|
117
|
+
```python
|
|
118
|
+
from fastapi_cachex.backends import MemcachedBackend
|
|
119
|
+
from fastapi_cachex import BackendProxy
|
|
120
|
+
|
|
121
|
+
backend = MemcachedBackend(servers=["localhost:11211"])
|
|
122
|
+
BackendProxy.set_backend(backend)
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
### Redis (Coming Soon)
|
|
126
|
+
|
|
127
|
+
Redis support is under development and will be available in future releases.
|
|
128
|
+
|
|
86
129
|
## Development Guide
|
|
87
130
|
|
|
88
131
|
### Running Tests
|
|
@@ -5,8 +5,12 @@
|
|
|
5
5
|
[](https://github.com/allen0099/FastAPI-CacheX/actions/workflows/test.yml)
|
|
6
6
|
[](https://github.com/allen0099/FastAPI-CacheX/actions/workflows/test.yml)
|
|
7
7
|
|
|
8
|
-
[](https://pepy.tech/project/fastapi-cachex)
|
|
9
|
+
[](https://pepy.tech/project/fastapi-cachex)
|
|
10
|
+
[](https://pepy.tech/project/fastapi-cachex)
|
|
11
|
+
|
|
12
|
+
[](https://pypi.org/project/fastapi-cachex)
|
|
13
|
+
[](https://pypi.org/project/fastapi-cachex/)
|
|
10
14
|
|
|
11
15
|
[English](README.md) | [繁體中文](docs/README.zh-TW.md)
|
|
12
16
|
|
|
@@ -43,17 +47,54 @@ uv pip install fastapi-cachex
|
|
|
43
47
|
|
|
44
48
|
```python
|
|
45
49
|
from fastapi import FastAPI
|
|
46
|
-
from fastapi_cachex import cache
|
|
50
|
+
from fastapi_cachex import cache, BackendProxy
|
|
51
|
+
from fastapi_cachex.backends import MemoryBackend, MemcachedBackend
|
|
47
52
|
|
|
48
53
|
app = FastAPI()
|
|
49
54
|
|
|
55
|
+
# Configure your cache backend
|
|
56
|
+
memory_backend = MemoryBackend() # In-memory cache
|
|
57
|
+
# or
|
|
58
|
+
memcached_backend = MemcachedBackend(servers=["localhost:11211"]) # Memcached
|
|
59
|
+
|
|
60
|
+
# Set the backend you want to use
|
|
61
|
+
BackendProxy.set_backend(memory_backend) # or memcached_backend
|
|
62
|
+
|
|
50
63
|
|
|
51
64
|
@app.get("/")
|
|
52
|
-
@cache()
|
|
65
|
+
@cache(ttl=60) # Cache for 60 seconds
|
|
53
66
|
async def read_root():
|
|
54
67
|
return {"Hello": "World"}
|
|
55
68
|
```
|
|
56
69
|
|
|
70
|
+
## Backend Configuration
|
|
71
|
+
|
|
72
|
+
FastAPI-CacheX supports multiple caching backends. You can easily switch between them using the `BackendProxy`.
|
|
73
|
+
|
|
74
|
+
### In-Memory Cache
|
|
75
|
+
|
|
76
|
+
```python
|
|
77
|
+
from fastapi_cachex.backends import MemoryBackend
|
|
78
|
+
from fastapi_cachex import BackendProxy
|
|
79
|
+
|
|
80
|
+
backend = MemoryBackend()
|
|
81
|
+
BackendProxy.set_backend(backend)
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
### Memcached
|
|
85
|
+
|
|
86
|
+
```python
|
|
87
|
+
from fastapi_cachex.backends import MemcachedBackend
|
|
88
|
+
from fastapi_cachex import BackendProxy
|
|
89
|
+
|
|
90
|
+
backend = MemcachedBackend(servers=["localhost:11211"])
|
|
91
|
+
BackendProxy.set_backend(backend)
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### Redis (Coming Soon)
|
|
95
|
+
|
|
96
|
+
Redis support is under development and will be available in future releases.
|
|
97
|
+
|
|
57
98
|
## Development Guide
|
|
58
99
|
|
|
59
100
|
### Running Tests
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import ast
|
|
2
|
+
from typing import Optional
|
|
3
|
+
|
|
4
|
+
from fastapi_cachex.backends.base import BaseCacheBackend
|
|
5
|
+
from fastapi_cachex.exceptions import CacheXError
|
|
6
|
+
from fastapi_cachex.types import ETagContent
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class MemcachedBackend(BaseCacheBackend):
|
|
10
|
+
"""Memcached backend implementation."""
|
|
11
|
+
|
|
12
|
+
def __init__(self, servers: list[str]) -> None:
|
|
13
|
+
"""Initialize the Memcached backend.
|
|
14
|
+
|
|
15
|
+
Args:
|
|
16
|
+
servers: List of Memcached servers in format ["host:port", ...]
|
|
17
|
+
|
|
18
|
+
Raises:
|
|
19
|
+
CacheXError: If pymemcache is not installed
|
|
20
|
+
"""
|
|
21
|
+
try:
|
|
22
|
+
from pymemcache import HashClient
|
|
23
|
+
except ImportError:
|
|
24
|
+
raise CacheXError(
|
|
25
|
+
"pymemcache is not installed. Please install it with 'pip install pymemcache'"
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
self.client = HashClient(servers)
|
|
29
|
+
|
|
30
|
+
async def get(self, key: str) -> Optional[ETagContent]:
|
|
31
|
+
"""Get value from cache.
|
|
32
|
+
|
|
33
|
+
Args:
|
|
34
|
+
key: Cache key to retrieve
|
|
35
|
+
|
|
36
|
+
Returns:
|
|
37
|
+
Optional[ETagContent]: Cached value with ETag if exists, None otherwise
|
|
38
|
+
"""
|
|
39
|
+
value = self.client.get(key)
|
|
40
|
+
if value is None:
|
|
41
|
+
return None
|
|
42
|
+
|
|
43
|
+
# Memcached stores data as bytes
|
|
44
|
+
# Convert string back to dictionary
|
|
45
|
+
value_dict = ast.literal_eval(value.decode("utf-8"))
|
|
46
|
+
return ETagContent(etag=value_dict["etag"], content=value_dict["content"])
|
|
47
|
+
|
|
48
|
+
async def set(
|
|
49
|
+
self, key: str, value: ETagContent, ttl: Optional[int] = None
|
|
50
|
+
) -> None:
|
|
51
|
+
"""Set value in cache.
|
|
52
|
+
|
|
53
|
+
Args:
|
|
54
|
+
key: Cache key
|
|
55
|
+
value: ETagContent to store
|
|
56
|
+
ttl: Time to live in seconds
|
|
57
|
+
"""
|
|
58
|
+
# Store as dictionary in string format
|
|
59
|
+
data = {"etag": value.etag, "content": value.content}
|
|
60
|
+
self.client.set(
|
|
61
|
+
key, str(data).encode("utf-8"), expire=ttl if ttl is not None else 0
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
async def delete(self, key: str) -> None:
|
|
65
|
+
"""Delete value from cache.
|
|
66
|
+
|
|
67
|
+
Args:
|
|
68
|
+
key: Cache key to delete
|
|
69
|
+
"""
|
|
70
|
+
self.client.delete(key)
|
|
71
|
+
|
|
72
|
+
async def clear(self) -> None:
|
|
73
|
+
"""Clear all values from cache."""
|
|
74
|
+
self.client.flush_all()
|
|
@@ -1,7 +1,9 @@
|
|
|
1
|
+
from typing import Optional
|
|
2
|
+
|
|
1
3
|
from fastapi_cachex.backends import BaseCacheBackend
|
|
2
4
|
from fastapi_cachex.exceptions import BackendNotFoundError
|
|
3
5
|
|
|
4
|
-
_default_backend: BaseCacheBackend
|
|
6
|
+
_default_backend: Optional[BaseCacheBackend] = None
|
|
5
7
|
|
|
6
8
|
|
|
7
9
|
class BackendProxy:
|
|
@@ -17,7 +19,11 @@ class BackendProxy:
|
|
|
17
19
|
return _default_backend
|
|
18
20
|
|
|
19
21
|
@staticmethod
|
|
20
|
-
def set_backend(backend: BaseCacheBackend) -> None:
|
|
21
|
-
"""Set the backend for caching.
|
|
22
|
+
def set_backend(backend: Optional[BaseCacheBackend]) -> None:
|
|
23
|
+
"""Set the backend for caching.
|
|
24
|
+
|
|
25
|
+
Args:
|
|
26
|
+
backend: The backend to use for caching, or None to clear the current backend
|
|
27
|
+
"""
|
|
22
28
|
global _default_backend
|
|
23
29
|
_default_backend = backend
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: fastapi-cachex
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.2
|
|
4
4
|
Summary: A caching library for FastAPI with support for Cache-Control, ETag, and multiple backends.
|
|
5
5
|
Author-email: Allen <s96016641@gmail.com>
|
|
6
6
|
License-Expression: Apache-2.0
|
|
@@ -25,6 +25,8 @@ Description-Content-Type: text/markdown
|
|
|
25
25
|
License-File: LICENSE
|
|
26
26
|
Requires-Dist: fastapi
|
|
27
27
|
Requires-Dist: httpx
|
|
28
|
+
Provides-Extra: memcache
|
|
29
|
+
Requires-Dist: pymemcache; extra == "memcache"
|
|
28
30
|
Dynamic: license-file
|
|
29
31
|
|
|
30
32
|
# FastAPI-Cache X
|
|
@@ -34,8 +36,12 @@ Dynamic: license-file
|
|
|
34
36
|
[](https://github.com/allen0099/FastAPI-CacheX/actions/workflows/test.yml)
|
|
35
37
|
[](https://github.com/allen0099/FastAPI-CacheX/actions/workflows/test.yml)
|
|
36
38
|
|
|
37
|
-
[](https://pepy.tech/project/fastapi-cachex)
|
|
40
|
+
[](https://pepy.tech/project/fastapi-cachex)
|
|
41
|
+
[](https://pepy.tech/project/fastapi-cachex)
|
|
42
|
+
|
|
43
|
+
[](https://pypi.org/project/fastapi-cachex)
|
|
44
|
+
[](https://pypi.org/project/fastapi-cachex/)
|
|
39
45
|
|
|
40
46
|
[English](README.md) | [繁體中文](docs/README.zh-TW.md)
|
|
41
47
|
|
|
@@ -72,17 +78,54 @@ uv pip install fastapi-cachex
|
|
|
72
78
|
|
|
73
79
|
```python
|
|
74
80
|
from fastapi import FastAPI
|
|
75
|
-
from fastapi_cachex import cache
|
|
81
|
+
from fastapi_cachex import cache, BackendProxy
|
|
82
|
+
from fastapi_cachex.backends import MemoryBackend, MemcachedBackend
|
|
76
83
|
|
|
77
84
|
app = FastAPI()
|
|
78
85
|
|
|
86
|
+
# Configure your cache backend
|
|
87
|
+
memory_backend = MemoryBackend() # In-memory cache
|
|
88
|
+
# or
|
|
89
|
+
memcached_backend = MemcachedBackend(servers=["localhost:11211"]) # Memcached
|
|
90
|
+
|
|
91
|
+
# Set the backend you want to use
|
|
92
|
+
BackendProxy.set_backend(memory_backend) # or memcached_backend
|
|
93
|
+
|
|
79
94
|
|
|
80
95
|
@app.get("/")
|
|
81
|
-
@cache()
|
|
96
|
+
@cache(ttl=60) # Cache for 60 seconds
|
|
82
97
|
async def read_root():
|
|
83
98
|
return {"Hello": "World"}
|
|
84
99
|
```
|
|
85
100
|
|
|
101
|
+
## Backend Configuration
|
|
102
|
+
|
|
103
|
+
FastAPI-CacheX supports multiple caching backends. You can easily switch between them using the `BackendProxy`.
|
|
104
|
+
|
|
105
|
+
### In-Memory Cache
|
|
106
|
+
|
|
107
|
+
```python
|
|
108
|
+
from fastapi_cachex.backends import MemoryBackend
|
|
109
|
+
from fastapi_cachex import BackendProxy
|
|
110
|
+
|
|
111
|
+
backend = MemoryBackend()
|
|
112
|
+
BackendProxy.set_backend(backend)
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### Memcached
|
|
116
|
+
|
|
117
|
+
```python
|
|
118
|
+
from fastapi_cachex.backends import MemcachedBackend
|
|
119
|
+
from fastapi_cachex import BackendProxy
|
|
120
|
+
|
|
121
|
+
backend = MemcachedBackend(servers=["localhost:11211"])
|
|
122
|
+
BackendProxy.set_backend(backend)
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
### Redis (Coming Soon)
|
|
126
|
+
|
|
127
|
+
Redis support is under development and will be available in future releases.
|
|
128
|
+
|
|
86
129
|
## Development Guide
|
|
87
130
|
|
|
88
131
|
### Running Tests
|
|
@@ -14,5 +14,7 @@ fastapi_cachex.egg-info/requires.txt
|
|
|
14
14
|
fastapi_cachex.egg-info/top_level.txt
|
|
15
15
|
fastapi_cachex/backends/__init__.py
|
|
16
16
|
fastapi_cachex/backends/base.py
|
|
17
|
+
fastapi_cachex/backends/memcached.py
|
|
17
18
|
fastapi_cachex/backends/memory.py
|
|
18
|
-
tests/test_cache.py
|
|
19
|
+
tests/test_cache.py
|
|
20
|
+
tests/test_proxybackend.py
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "fastapi-cachex"
|
|
3
|
-
version = "0.1.
|
|
3
|
+
version = "0.1.2" # Initial release version
|
|
4
4
|
description = "A caching library for FastAPI with support for Cache-Control, ETag, and multiple backends."
|
|
5
5
|
readme = "README.md"
|
|
6
6
|
requires-python = ">=3.10"
|
|
@@ -37,6 +37,7 @@ Issues = "https://github.com/allen0099/FastAPI-CacheX/issues"
|
|
|
37
37
|
dev = [
|
|
38
38
|
"coverage>=7.8.0",
|
|
39
39
|
"pre-commit>=4.2.0",
|
|
40
|
+
"pymemcache>=4.0.0",
|
|
40
41
|
"pytest>=8.3.5",
|
|
41
42
|
"pytest-asyncio>=0.26.0",
|
|
42
43
|
"pytest-cov>=6.1.0",
|
|
@@ -44,6 +45,9 @@ dev = [
|
|
|
44
45
|
"tox>=4.25.0",
|
|
45
46
|
]
|
|
46
47
|
|
|
48
|
+
[project.optional-dependencies]
|
|
49
|
+
memcache = ["pymemcache"]
|
|
50
|
+
|
|
47
51
|
[tool.pytest.ini_options]
|
|
48
52
|
pythonpath = [
|
|
49
53
|
"."
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import asyncio
|
|
2
|
+
|
|
3
|
+
import pytest
|
|
4
|
+
import pytest_asyncio
|
|
5
|
+
from fastapi import FastAPI
|
|
6
|
+
from fastapi.responses import JSONResponse
|
|
7
|
+
from fastapi.testclient import TestClient
|
|
8
|
+
|
|
9
|
+
from fastapi_cachex import BackendProxy
|
|
10
|
+
from fastapi_cachex import cache
|
|
11
|
+
from fastapi_cachex.backends import MemoryBackend
|
|
12
|
+
from fastapi_cachex.exceptions import BackendNotFoundError
|
|
13
|
+
from fastapi_cachex.types import ETagContent
|
|
14
|
+
|
|
15
|
+
app = FastAPI()
|
|
16
|
+
client = TestClient(app)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
@pytest_asyncio.fixture(autouse=True)
|
|
20
|
+
async def cleanup():
|
|
21
|
+
# Reset backend before each test
|
|
22
|
+
try:
|
|
23
|
+
backend = BackendProxy.get_backend()
|
|
24
|
+
if isinstance(backend, MemoryBackend):
|
|
25
|
+
backend.stop_cleanup()
|
|
26
|
+
# Reset backend by setting it to None
|
|
27
|
+
BackendProxy.set_backend(None)
|
|
28
|
+
except BackendNotFoundError:
|
|
29
|
+
pass
|
|
30
|
+
|
|
31
|
+
yield
|
|
32
|
+
|
|
33
|
+
# Clean up after each test
|
|
34
|
+
try:
|
|
35
|
+
backend = BackendProxy.get_backend()
|
|
36
|
+
if isinstance(backend, MemoryBackend):
|
|
37
|
+
await backend.clear() # Clear all cached data
|
|
38
|
+
backend.stop_cleanup()
|
|
39
|
+
# Reset backend by setting it to None
|
|
40
|
+
BackendProxy.set_backend(None)
|
|
41
|
+
except BackendNotFoundError:
|
|
42
|
+
pass
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def test_backend_switching():
|
|
46
|
+
# Initial state should have no backend
|
|
47
|
+
with pytest.raises(BackendNotFoundError):
|
|
48
|
+
BackendProxy.get_backend()
|
|
49
|
+
|
|
50
|
+
# Set up MemoryBackend
|
|
51
|
+
memory_backend = MemoryBackend()
|
|
52
|
+
BackendProxy.set_backend(memory_backend)
|
|
53
|
+
assert isinstance(BackendProxy.get_backend(), MemoryBackend)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def test_memory_cache():
|
|
57
|
+
@app.get("/test")
|
|
58
|
+
@cache(ttl=60)
|
|
59
|
+
async def test_endpoint():
|
|
60
|
+
return JSONResponse(content={"message": "test"})
|
|
61
|
+
|
|
62
|
+
# Use MemoryBackend
|
|
63
|
+
memory_backend = MemoryBackend()
|
|
64
|
+
BackendProxy.set_backend(memory_backend)
|
|
65
|
+
|
|
66
|
+
# First request should return 200
|
|
67
|
+
response1 = client.get("/test")
|
|
68
|
+
assert response1.status_code == 200
|
|
69
|
+
etag1 = response1.headers["ETag"]
|
|
70
|
+
|
|
71
|
+
# Request with same ETag should return 304
|
|
72
|
+
response2 = client.get("/test", headers={"If-None-Match": etag1})
|
|
73
|
+
assert response2.status_code == 304
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
@pytest.mark.asyncio
|
|
77
|
+
async def test_backend_cleanup():
|
|
78
|
+
# Run cleanup task in async environment
|
|
79
|
+
memory_backend = MemoryBackend()
|
|
80
|
+
BackendProxy.set_backend(memory_backend)
|
|
81
|
+
|
|
82
|
+
# Verify initial state
|
|
83
|
+
assert memory_backend._cleanup_task is None
|
|
84
|
+
|
|
85
|
+
# Set test data
|
|
86
|
+
test_value = ETagContent(etag="test-etag", content="test_value")
|
|
87
|
+
await memory_backend.set("test_key", test_value, ttl=1)
|
|
88
|
+
|
|
89
|
+
# Verify data is stored correctly
|
|
90
|
+
cached_value = await memory_backend.get("test_key")
|
|
91
|
+
assert cached_value is not None
|
|
92
|
+
assert cached_value.content == "test_value"
|
|
93
|
+
|
|
94
|
+
# Wait for data to expire (1 second + extra time)
|
|
95
|
+
await asyncio.sleep(1.1)
|
|
96
|
+
|
|
97
|
+
# Execute cleanup
|
|
98
|
+
await memory_backend.cleanup()
|
|
99
|
+
|
|
100
|
+
# Verify data has been cleaned up
|
|
101
|
+
assert await memory_backend.get("test_key") is None
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|