fix: changed hash for memoization
This commit is contained in:
parent
843ef15e13
commit
40e3ffbf50
1 changed files with 11 additions and 10 deletions
21
src/backend/langflow/cache/utils.py
vendored
21
src/backend/langflow/cache/utils.py
vendored
|
|
@ -1,5 +1,4 @@
|
||||||
import contextlib
|
import contextlib
|
||||||
import hashlib
|
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import tempfile
|
import tempfile
|
||||||
|
|
@ -8,15 +7,17 @@ from pathlib import Path
|
||||||
import dill # type: ignore
|
import dill # type: ignore
|
||||||
import functools
|
import functools
|
||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
|
import hashlib
|
||||||
|
|
||||||
|
|
||||||
def memoize(maxsize=128):
|
def memoize_dict(maxsize=128):
|
||||||
cache = OrderedDict()
|
cache = OrderedDict()
|
||||||
|
|
||||||
def decorator(func):
|
def decorator(func):
|
||||||
@functools.wraps(func)
|
@functools.wraps(func)
|
||||||
def wrapper(*args, **kwargs):
|
def wrapper(*args, **kwargs):
|
||||||
key = (func.__name__, args, frozenset(kwargs.items()))
|
hashed = compute_dict_hash(args[0])
|
||||||
|
key = (func.__name__, hashed, frozenset(kwargs.items()))
|
||||||
if key not in cache:
|
if key not in cache:
|
||||||
result = func(*args, **kwargs)
|
result = func(*args, **kwargs)
|
||||||
cache[key] = result
|
cache[key] = result
|
||||||
|
|
@ -52,6 +53,13 @@ def clear_old_cache_files(max_cache_size: int = 3):
|
||||||
os.remove(cache_file)
|
os.remove(cache_file)
|
||||||
|
|
||||||
|
|
||||||
|
def compute_dict_hash(graph_data):
|
||||||
|
graph_data = filter_json(graph_data)
|
||||||
|
|
||||||
|
cleaned_graph_json = json.dumps(graph_data, sort_keys=True)
|
||||||
|
return hashlib.sha256(cleaned_graph_json.encode("utf-8")).hexdigest()
|
||||||
|
|
||||||
|
|
||||||
def filter_json(json_data):
|
def filter_json(json_data):
|
||||||
filtered_data = json_data.copy()
|
filtered_data = json_data.copy()
|
||||||
|
|
||||||
|
|
@ -76,13 +84,6 @@ def filter_json(json_data):
|
||||||
return filtered_data
|
return filtered_data
|
||||||
|
|
||||||
|
|
||||||
def compute_hash(graph_data):
|
|
||||||
graph_data = filter_json(graph_data)
|
|
||||||
|
|
||||||
cleaned_graph_json = json.dumps(graph_data, sort_keys=True)
|
|
||||||
return hashlib.sha256(cleaned_graph_json.encode("utf-8")).hexdigest()
|
|
||||||
|
|
||||||
|
|
||||||
def save_cache(hash_val: str, chat_data, clean_old_cache_files: bool):
|
def save_cache(hash_val: str, chat_data, clean_old_cache_files: bool):
|
||||||
cache_path = Path(tempfile.gettempdir()) / f"{PREFIX}_{hash_val}.dill"
|
cache_path = Path(tempfile.gettempdir()) / f"{PREFIX}_{hash_val}.dill"
|
||||||
with cache_path.open("wb") as cache_file:
|
with cache_path.open("wb") as cache_file:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue