Caching in Nexar
Tip
This is a bit of a more advanced topic, involving the actual under the hood API calls to Riot, which Nexar tries to hide from the user.
By and large, simply use the SMART_CACHE_CONFIG:
This keeps a local, persistent storage of data to reduce API calls.
Nexar includes built-in caching functionality to reduce API calls and improve performance. Cached responses are either stored locally (sqlite file) or in memory, and reused until they expire.
Via CacheConfig
, one can specify a default cache time, pick your storage backend, and configure endpoint TTLs (time-to-live's).
Quick Start
By default, Nexar uses "dumb" caching, which caches all responses for an hour to an SQLite file in your CWD, regardless of the endpoint:
import asyncio
from nexar.client import NexarClient
from nexar.enums import Region
client = NexarClient(
riot_api_key="your_api_key",
default_region=Region.NA1,
)
async def main() -> None:
async with client:
# First call hits the API
player = await client.get_player("bexli", "bex")
# Second call uses cached data
player = await client.get_player("bexli", "bex")
if __name__ == "__main__":
asyncio.run(main())
Cache Backends
Nexar supports two cache backends:
- SQLite (default): Persistent cache stored in a file
from nexar.cache import SMART_CACHE_CONFIG
client = NexarClient(
riot_api_key="your_api_key",
cache_config=SMART_CACHE_CONFIG,
default_region=Region.NA1,
)
- Memory: Fast in-memory cache (cleared when application exits)
from nexar.cache import SMART_CACHE_CONFIG_MEMORY
client = NexarClient(
riot_api_key="your_api_key",
cache_config=SMART_CACHE_CONFIG_MEMORY,
default_region=Region.NA1,
)
Custom Cache Configuration
Note
For creating your own endpoint config, consult the Riot API docs
You can create your own cache configuration, and set things like the path to your database file or a custom endpoint durations, etc.
from nexar import CacheConfig
# SQLite cache with custom settings
custom_config = CacheConfig(
backend="sqlite",
cache_dir="/var/cache/nexar/",
expire_after=7200, # Default 2 hours
endpoint_config={
"/riot/account/v1/accounts/by-riot-id": {"expire_after": None}, # Never cache Riot ID
},
)
# Memory cache with custom expiration
memory_config = CacheConfig(
backend="memory",
expire_after=900, # 15 minutes
)
On, even easier, use the same endpoint config as the "smart" presets
from nexar.cache import SMART_CACHE_ENDPOINTS
# SQLite cache with custom settings
custom_config = CacheConfig(
backend="sqlite",
cache_dir="./my_cache",
endpoint_config=SMART_CACHE_ENDPOINTS,
)
# Memory cache with custom expiration
memory_config = CacheConfig(
backend="memory",
endpoint_config=SMART_CACHE_ENDPOINTS,
)
Predefined Configurations
DEFAULT_CACHE_CONFIG
Uses SQLite backend with 1-hour expiration for all endpoints.
SMART_CACHE_CONFIG/SMART_CACHE_CONFIG_MEMORY
Intelligently caches different endpoint types for optimal durations:
- Account/Summoner data: 24 hours (rarely changes)
- Match data: Forever (matches are immutable)
- League entries: 5 minutes (rankings change frequently)
- Match IDs: 1 minute (new matches appear)
MEMORY_CACHE_CONFIG
Uses in-memory caching with 30-minute expiration.
NO_CACHE_CONFIG
Disables caching entirely - every request hits the API.
Cache Management
async with NexarClient(riot_api_key="your_api_key") as client:
# Get cache information
info = await client.get_cache_info()
# Clear cached data
await client.clear_cache()
# View API call statistics
client.print_api_call_summary()
Best Practices
- Use SMART_CACHE_CONFIG for most applications
- Use memory backend for short-lived scripts or when you don't want persistent files
- Customize expiration times based on how frequently your data changes
- Disable caching for specific endpoints that need real-time data