perf: Refine the Model Manager code#3089
Conversation
| ModelManage.up_clear_time = time.time() | ||
|
|
||
| @staticmethod | ||
| def delete_key(_id): |
There was a problem hiding this comment.
Here are some optimizations and improvements:
- Use
try-with-resourcesstyle context managers to automatically release locks:
with _lock:-
Simplify cache set logic by directly using the lock within it:
def get_model(self, _id, get_model): model_instance = self.cache.get(_id) if not model_instance or not model_instance.is_cache_model(): with _lock: model_instance = get_model(_id) self.cache.set( _id, model_instance, timeout=60 * 3600, # Increased from 30 minutes to an hour ) self.clear_timeout_cache() return model_instance elif model_instance.is_cache_model(): self.cache.touch(_id, timeout=60 * 3600) # Increased from 30 minutes to an hour return model_instance else: self.delete_key(_id) return get_model(_id)
-
Move the cache clearing logic into its own method called
clear_expired_keys, so that you can call it explicitly when needed without modifyingget_model. -
Ensure that
ModelManage.up_clear_timeis properly initialized before being used. -
The
time.sleep(3)in your original code was removed because there's no reason for sleeping between each request. If sleep is required somewhere else, ensure that it doesn't interfere with performance or functionality of this critical service.
This optimized version maintains the necessary logic while improving readability and efficiency. Always test thoroughly after making changes to ensure stability!
|
Adding the "do-not-merge/release-note-label-needed" label because no release-note block was detected, please follow our release note process to remove it. DetailsInstructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
perf: Refine the Model Manager code