/ justapi / examples / plugin_cache.py
swadhinbiswas docs(examples): add 10 progressive tutorial examples and test project · 7/9/2026
b48e461
42 lines · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from justapi import JustAPIApp

class RedisCachePlugin:
    """A simple plugin to cache responses in an in-memory dict (simulating Redis)."""
    def __init__(self):
        self.cache = {}

    def build(self, app: JustAPIApp):
        print("[RedisCachePlugin] Building plugin...")
        # In a real app we might inject this cache into the app state

    async def on_startup(self):
        print("[RedisCachePlugin] Connecting to simulated Redis...")
        self.cache.clear()

    async def on_shutdown(self):
        print("[RedisCachePlugin] Disconnecting simulated Redis...")
        self.cache.clear()

    def get(self, key):
        return self.cache.get(key)
        
    def set(self, key, value):
        self.cache[key] = value

if __name__ == "__main__":
    app = JustAPIApp()
    cache_plugin = RedisCachePlugin()
    app.use(cache_plugin)

    def cached_hello(request):
        key = "hello"
        if cached := cache_plugin.get(key):
            return {"message": cached, "cached": True}
            
        value = "world"
        cache_plugin.set(key, value)
        return {"message": value, "cached": False}

    app.get("/hello", cached_hello)
    print("Run this file using a python runner to see it in action.")