What Is the TTL for User Profile Cache in Nitter?
Nitter caches user profiles for exactly one hour (3,600 seconds), defined by the baseCacheTime constant in the Redis caching layer.
Nitter is an alternative Twitter front-end that relies on aggressive caching to reduce API load and improve response times. Understanding the TTL for user profile cache in Nitter helps developers and self-hosters predict data freshness and debug stale profile issues. The implementation uses Redis as the backend store with hardcoded expiration logic that applies uniformly to all cached user objects.
Where the TTL Is Defined
The expiration duration lives in src/redis_cache.nim as the global constant baseCacheTime.
# src/redis_cache.nim (lines 9-10)
const
baseCacheTime = 60 * 60 # 3600 seconds = 1 hour
This constant represents the baseline freshness window for all volatile cache entries. The value is calculated as 60 * 60 to produce 3,600 seconds, ensuring profiles never persist longer than one hour regardless of traffic patterns.
How the TTL Is Applied
When Nitter fetches a user from Twitter’s API, it serializes the User object and writes it to Redis using the cache(User) procedure. This function constructs a key prefixed with p: (e.g., p:example_user) and applies the TTL via the setEx command.
In src/redis_cache.nim (lines 92-97), the implementation resembles:
proc cache*(user: User) {.async.} =
let key = "p:" & user.username
await redis.setEx(key, baseCacheTime, user.serialize())
The setEx atomic operation stores the serialized user data and sets the expiration atomically. If a subsequent request arrives after 3,600 seconds have elapsed, Redis automatically deletes the key, forcing Nitter to re-fetch fresh data from the upstream API.
Working with the Cache Programmatically
Self-hosters extending Nitter can interact with this caching layer directly. The following Nim examples demonstrate manual cache inspection and refresh patterns:
Writing a profile with the standard TTL:
import redis_cache, types
let user = await getGraphUser("example_user")
await cache(user) # Automatically expires after baseCacheTime (3600s)
Checking for a valid cached entry:
let cached = await get("p:" & "example_user")
if cached != redisNil:
let user = cached.deserialize(User)
# user is guaranteed to be less than 1 hour old
else:
# Cache miss or TTL expired; fetch fresh data
let fresh = await getGraphUser("example_user")
await cache(fresh)
Because the TTL is immutable at runtime, you cannot extend the lifetime of a specific entry without modifying the source and recompiling.
Related Source Files
The caching system spans several modules:
src/redis_cache.nim– ContainsbaseCacheTime, thecache(User)procedure, and Redis connection management.src/types.nim– Defines theUserobject structure that gets serialized and stored.src/api.nimandsrc/apiutils.nim– ProvidegetGraphUser()and related functions that populate the cache when misses occur.
Summary
- Nitter stores user profiles in Redis using the key pattern
p:<username>. - The TTL is hardcoded to 3,600 seconds (1 hour) via the
baseCacheTimeconstant. - The
cache(User)procedure insrc/redis_cache.nimapplies this TTL usingsetEx. - After expiration, Redis evicts the key automatically, triggering a fresh fetch from Twitter’s API on the next request.
- There is no runtime configuration to adjust this duration; changing the TTL requires modifying the source code.
Frequently Asked Questions
How long does Nitter cache a user profile?
Nitter caches each user profile for one hour. The exact duration is defined by baseCacheTime = 60 * 60 seconds in src/redis_cache.nim, and it applies uniformly to every profile stored in Redis.
Can I change the user profile cache duration?
No, not without recompiling. The TTL is a compile-time constant (baseCacheTime) rather than a configuration parameter. To alter the duration, you must edit src/redis_cache.nim, change the value, and rebuild the Nitter binary.
What happens when a cached profile expires?
When the 3,600-second TTL elapses, Redis automatically deletes the p:<username> key. The next request for that user triggers a cache miss, causing Nitter to invoke getGraphUser() (or similar) to retrieve fresh data from Twitter’s API and repopulate the cache with a new 1-hour expiration.
Where does Nitter store cached user data?
Cached profiles reside in Redis under keys prefixed with p:. For example, the user example_user is stored at key p:example_user. This naming convention allows the cache(User) and retrieval logic to isolate profile data from other cached content like tweets or search results.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →