Cache¶
The UserCache class is a helper that makes it easier to work with cache storage in Everysk applications. Built on top of BaseSDK, it provides ready-to-use methods for common tasks like saving data, retrieving it, removing items, and updating values in the cache.
Example¶
Here is an example where we use the Market Data search method for performing a search inside the database, then we store the result in cache to be later retrieve if we make the same request:
from everysk.sdk.engines import MarketData
cache = UserCache()
cache_key = 'raw_sector_query'
cached_result = cache.get(cache_key)
if cached_result:
return cached_result
market_data = MarketData()
result = market_data.search(
[["raw_sector", "=", "Technology"]],
fields=["instrument_class", "name", "gics_sector"],
limit=1,
)
cache.set(cache_key, result, timeout=600)
return result
This example shows how to use UserCache to avoid running the same database query multiple times. It first looks for the result in the cache using a key (raw_sector_query). If the data is already cached, it returns it immediately. If not, it runs the MarketData.search query, saves the result in the cache for 600 seconds (or 10 minutes), and then returns it.