Add cache functionality to Component class

This commit is contained in:
Gabriel Luiz Freitas Almeida 2023-11-23 10:57:11 -03:00
commit 946bf03190

View file

@ -1,6 +1,8 @@
import ast import ast
import operator
from typing import Any, ClassVar, Optional from typing import Any, ClassVar, Optional
from cachetools import TTLCache, cachedmethod
from fastapi import HTTPException from fastapi import HTTPException
from langflow.interface.custom.code_parser import CodeParser from langflow.interface.custom.code_parser import CodeParser
@ -24,9 +26,11 @@ class Component:
field_config: dict = {} field_config: dict = {}
def __init__(self, **data): def __init__(self, **data):
self.cache = TTLCache(maxsize=1024, ttl=60)
for key, value in data.items(): for key, value in data.items():
setattr(self, key, value) setattr(self, key, value)
@cachedmethod(operator.attrgetter("cache"))
def get_code_tree(self, code: str): def get_code_tree(self, code: str):
parser = CodeParser(code) parser = CodeParser(code)
return parser.parse_code() return parser.parse_code()