11---
22title : Quickstart
3- description : Get an ArchiPy application running in under five minutes with a config, a Redis adapter, and a caching decorator .
3+ description : Get an ArchiPy application running in under five minutes with a config, a Redis adapter, and a TTL cache backed by Redis .
44---
55
66# Quickstart
77
88This guide walks you through creating a minimal ArchiPy application from scratch. You will have a running service
9- with a typed config, a Redis adapter, and a TTL cache decorator in under five minutes.
9+ with a typed config, a Redis adapter, and a TTL cache backed by Redis in under five minutes.
1010
1111## Prerequisites
1212
@@ -29,7 +29,7 @@ uv init
2929## Step 2 — Install ArchiPy
3030
3131``` bash
32- uv add " archipy[redis,cache ]"
32+ uv add " archipy[redis]"
3333```
3434
3535## Step 3 — Define the Configuration
@@ -81,36 +81,45 @@ redis = RedisAdapter() # reads config.REDIS automatically
8181logger.info(" Redis adapter ready" )
8282```
8383
84- ## Step 5 — Add a Caching Decorator
84+ ## Step 5 — Add a Caching Layer with Redis
8585
86- Use ` @ttl_cache ` to cache any function result in Redis:
86+ Use ` RedisAdapter ` to cache function results in Redis with a TTL :
8787
8888``` python
8989# logics/user_logic.py
9090import logging
91- from archipy.helpers.decorators.cache import ttl_cache
91+
92+ from adapters.cache_adapter import redis
9293
9394logger = logging.getLogger(__name__ )
9495
96+ _CACHE_TTL = 60 # seconds
97+
9598
96- @ttl_cache (ttl = 60 )
9799def get_user_name (user_id : str ) -> str :
98- """ Fetch a user name from the database (simulated) .
100+ """ Fetch a user name, served from Redis cache when available .
99101
100102 Args:
101103 user_id: Unique user identifier.
102104
103105 Returns:
104106 The user's display name.
105107 """
108+ cache_key = f " user:name: { user_id} "
109+ cached = redis.get(cache_key)
110+ if cached is not None :
111+ return str (cached)
112+
106113 logger.info(" Cache miss — fetching user %s from database" , user_id)
107- return f " User- { user_id} " # replace with a real DB call
114+ name = f " User- { user_id} " # replace with a real DB call
115+ redis.set(cache_key, name, ex = _CACHE_TTL )
116+ return name
108117
109118
110- # First call: cache miss — hits the database
119+ # First call: cache miss — hits the database and stores in Redis
111120name = get_user_name(" 42" )
112121
113- # Second call within 60 seconds: cache hit — returns instantly
122+ # Second call within 60 seconds: cache hit — returns from Redis instantly
114123name = get_user_name(" 42" )
115124```
116125
@@ -152,9 +161,9 @@ python manage.py run
152161You should see:
153162
154163```
155- INFO Cache miss — fetching user 42 from database
156- INFO User-42
157- INFO User-42
164+ INFO:logics.user_logic: Cache miss — fetching user 42 from database
165+ INFO:__main__: User-42
166+ INFO:__main__: User-42
158167```
159168
160169The second call is served from Redis without hitting the database.
0 commit comments