You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
20
+
21
+
View lets you do this by using the `cache_rate` parameter on a router.
22
+
23
+
For example:
24
+
25
+
```py
26
+
from view import new_app
27
+
28
+
app = new_app()
29
+
30
+
@app.get("/", cache_rate=10) # reload this route every 10 requests
31
+
asyncdefindex():
32
+
return"..."
33
+
34
+
app.run()
35
+
```
36
+
37
+
You can see this in more detail by using a route that changes it's responses:
38
+
39
+
```py
40
+
from view import new_app
41
+
42
+
app = new_app()
43
+
count =1
44
+
45
+
@app.get("/", cache_rate=10)
46
+
asyncdefindex():
47
+
global count
48
+
count +=1
49
+
returnstr(count)
50
+
51
+
app.run()
52
+
```
53
+
54
+
In the above example, `index` is only called every 10 requests, so after 20 calls, `count` would be `2`.
55
+
16
56
## Response Protocol
17
57
18
58
If you have some sort of object that you want to wrap a response around, view.py gives you the `__view_response__` protocol. The only requirements are:
0 commit comments