Skip to content

Commit ce5cea5

Browse files
committed
remove decorator because its stupid
1 parent 7566751 commit ce5cea5

5 files changed

Lines changed: 12 additions & 58 deletions

File tree

CHANGELOG.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313
- Added the `templates` function
1414
- Added support for `attrs` in type validation
1515
- Added documentation for caching
16-
- Added `App.cache`
1716
- Added the `cache_rate` parameter to routers
1817
- Removed `psutil` and `plotext` as a global dependency
1918
- Added `fancy` optional dependencies

docs/building-projects/responses.md

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,50 +18,40 @@ async def index():
1818

1919
Sometimes, computing the response for a route can be expensive or unnecessary. For this, view.py, along with many other web frameworks, provide the ability to cache responses.
2020

21-
View lets you do this in one of two ways:
21+
View lets you do this by using the `cache_rate` parameter on a router.
2222

23-
- Using the `cache` decorator (standard or direct).
24-
- Using the `cache_rate` parameter on a router.
25-
26-
For example, with the `cache` decorator
23+
For example:
2724

2825
```py
29-
from view import new_app, cache
26+
from view import new_app
3027

3128
app = new_app()
3229

33-
@app.get("/")
34-
@cache(10) # reload this route every 10 requests
30+
@app.get("/", cache_rate=10) # reload this route every 10 requests
3531
async def index():
3632
return "..."
3733

3834
app.run()
3935
```
4036

41-
Once again, a direct variation of `cache` is available to prevent an import:
42-
43-
```py
44-
@app.get("/")
45-
@app.cache(10) # equivalent to the above
46-
async def index():
47-
return "..."
48-
```
49-
50-
Finally, you may use the `cache_rate` parameter on any router:
37+
You can see this in more detail by using a route that changes it's responses:
5138

5239
```py
5340
from view import new_app
5441

5542
app = new_app()
43+
count = 1
5644

57-
@app.get("/", cache_rate=10) # reload this route every 10 requests
45+
@app.get("/", cache_rate=10)
5846
async def index():
59-
return "..."
47+
global count
48+
count += 1
49+
return str(count)
6050

6151
app.run()
6252
```
6353

64-
There is no difference between these two, and is simply up to personal preference. Note that the `cache` decorator will override the `cache_rate` parameter.
54+
In the above example, `index` is only called every 10 requests, so after 20 calls, `count` would be `2`.
6555

6656
## Response Protocol
6757

src/view/app.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
from .routing import body as body_impl
4444
from .routing import delete, get, options, patch, post, put
4545
from .routing import query as query_impl
46-
from .routing import cache as cache_impl
4746
from .typing import Callback, DocsType
4847
from .util import enable_debug
4948

@@ -354,10 +353,6 @@ def put(self, path: str, doc: str | None = None, *, cache_rate: int = -1):
354353
def options(self, path: str, doc: str | None = None, *, cache_rate: int = -1):
355354
"""Set a OPTIONS route."""
356355
return self._method_wrapper(path, doc, cache_rate, options)
357-
358-
def cache(self, amount: int):
359-
"""Set the cache rate for a route. For example, if this is 10, the route will only be called every 10 requests."""
360-
return cache_impl(amount)
361356

362357
def _set_log_arg(self, kwargs: _LogArgs, key: str) -> None:
363358
if key not in kwargs:

src/view/routing.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
"query",
2323
"body",
2424
"route_types",
25-
"cache",
2625
"BodyParam",
2726
)
2827

@@ -323,12 +322,3 @@ def inner(r: RouteOrCallable) -> Route:
323322

324323
return inner
325324

326-
327-
def cache(amount: int):
328-
"""Set the cache rate for a route. For example, if this is 10, the route will only be called every 10 requests."""
329-
def inner(r: RouteOrCallable) -> Route:
330-
route = _ensure_route(r)
331-
route.cache_rate = amount
332-
return route
333-
334-
return inner

tests/test_app.py

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from typing_extensions import NotRequired
66
from ward import test
77

8-
from view import BodyParam, Response, body, new_app, query, cache, get
8+
from view import BodyParam, Response, body, new_app, query, get
99

1010

1111
@test("responses")
@@ -546,20 +546,6 @@ async def _():
546546
app = new_app()
547547
count = 0
548548

549-
@app.get("/")
550-
@cache(10)
551-
async def index():
552-
nonlocal count
553-
count += 1
554-
return str(count)
555-
556-
@app.get("/direct")
557-
@app.cache(10)
558-
async def direct():
559-
nonlocal count
560-
count += 1
561-
return str(count)
562-
563549
@app.get("/param", cache_rate=10)
564550
async def param():
565551
nonlocal count
@@ -574,12 +560,6 @@ async def param_std():
574560

575561

576562
async with app.test() as test:
577-
results = [(await test.get("/")).message for _ in range(10)]
578-
assert all(i == results[0] for i in results)
579-
580-
results = [(await test.get("/direct")).message for _ in range(10)]
581-
assert all(i == results[0] for i in results)
582-
583563
results = [(await test.get("/param")).message for _ in range(10)]
584564
assert all(i == results[0] for i in results)
585565

0 commit comments

Comments
 (0)