-
Notifications
You must be signed in to change notification settings - Fork 15k
chore: add redis for getting built plugin #20118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
@crazywoola CC |
6c1f6af
to
81fc4b2
Compare
Looks like miss the logic when the new |
81fc4b2
to
813d156
Compare
done |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM overall. Good to have this improvement.
Suggested PR title: introducing caching for builtin plugin list
""" | ||
Generate the Redis key for caching. | ||
""" | ||
return BuiltinToolManageService.REDIS_KEY_PREFIX.format(user_id=user_id, tenant_id=tenant_id) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
REDIS_KEY_PREFIX is never used again, which could be removed from BuiltinToolManageService. And simply use the string format style to generate cache key instead.
REDIS_TTL = 60 * 10 # Cache expires in 10 minutes | ||
|
||
@staticmethod | ||
def get_redis_key(user_id: str, tenant_id: str) -> str: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
def get_redis_key(user_id: str, tenant_id: str) -> str: | |
def generate_cache_key(user_id: str, tenant_id: str) -> str: |
prefer the netual concept cache
instead of specific implementation redis
.
redis_key = BuiltinToolManageService.get_redis_key(user_id, tenant_id) | ||
redis_client.delete(redis_key) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better to squash in a single code line for clearer readibility.
redis_key = BuiltinToolManageService.get_redis_key(user_id, tenant_id) | ||
redis_client.delete(redis_key) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto.
from models.tools import BuiltinToolProvider | ||
from services.tools.tools_transform_service import ToolTransformService | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class BuiltinToolManageService: | ||
REDIS_KEY_PREFIX = "builtin_tools:user:{user_id}:tenant:{tenant_id}" | ||
REDIS_TTL = 60 * 10 # Cache expires in 10 minutes |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make this configurable in dify_config
. And prevent a general config name for this.
decrypt_credentials=True, | ||
) | ||
# Deserialize cached data directly into a list of dictionaries | ||
deserialized_data = json.loads(cached_data) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Recommend to use ToolProviderApiEntity.model_validate_json()
to reach a better performance.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TypeAdapter(Sequence[ToolProviderApiEntity])
may simplify it.
# Deserialize cached data directly into a list of dictionaries | ||
deserialized_data = json.loads(cached_data) | ||
return [ToolProviderApiEntity(**item) for item in deserialized_data] | ||
except (json.JSONDecodeError, TypeError) as e: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
json.JSONDecodeError
can be replaced to pydantic.ValidationError
if model_validate_json
was used.
|
||
return BuiltinToolProviderSort.sort(result) | ||
# Serialize and store in Redis | ||
serialized_result = json.dumps([jsonable_encoder(item) for item in result]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pydantic.TypeAdapter(list[Any]).dump_json(result)
is a better choice.
Summary
It will cost long time getting all built plugins
Screenshots
in dev mode
Checklist
Important
Please review the checklist below before submitting your pull request.
dev/reformat
(backend) andcd web && npx lint-staged
(frontend) to appease the lint gods