def get_data_from_primary_source():
# Simulate fetching data from primary source (e.g., a database)
data = "Data from primary source"
return data
def get_data_with_caching():
# Check if data is already in cache
cached_data = redis_client.get("cached_data_key")
if cached_data is not None:
return cached_data
else:
data = get_data_from_primary_source()
# Store data in cache with a specific expiration time (e.g., 300 seconds)
redis_client.setex("cached_data_key", 300, data)
return data
# Usage
result = get_data_with_caching()
print(result)