The cache not work in Workflow.arun.
I’m not sure whether this is a bug or an issue on my end.
import asyncio
from typing import AsyncIterator
from agno.agent import RunResponse
from agno.storage.sqlite import SqliteStorage
from agno.utils.log import logger
from agno.workflow import Workflow
class CacheWorkflow(Workflow):
# Purely descriptive, not used by the workflow
description: str = "A workflow that caches previous outputs"
async def arun(self, message: str) -> AsyncIterator[RunResponse]:
logger.info(f"Checking cache for '{message}'")
# Check if the output is already cached
if self.session_state.get(message):
logger.info(f"Cache hit for '{message}'")
yield RunResponse(run_id=self.run_id, content=self.session_state.get(message))
return
logger.info(f"Cache miss for '{message}'")
# Cache the output after response is yielded
self.session_state[message] = "HAHA..."
async def main():
workflow = CacheWorkflow(
session_id="my-session-id",
storage=SqliteStorage(
table_name="workflow_sessions",
db_file="tmp/workflows.db",
),
)
async for item in workflow.arun(message="Tell me a joke."):
print(item.content)
workflow1 = CacheWorkflow(
session_id="my-session-id",
storage=SqliteStorage(
table_name="workflow_sessions",
db_file="tmp/workflows.db",
),
)
async for item in workflow1.arun(message="Tell me a joke."):
print(item.content)
if __name__ == "__main__":
asyncio.run(main())
output
INFO Checking cache for 'Tell me a joke.'
INFO Cache miss for 'Tell me a joke.'
INFO Checking cache for 'Tell me a joke.'
INFO Cache miss for 'Tell me a joke.'
agno version: agno==1.6.4