-
Notifications
You must be signed in to change notification settings - Fork 2.8k
perf: model manage #3226
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
perf: model manage #3226
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,15 +40,12 @@ def generate(): | |
| def get_key_pair(): | ||
| rsa_value = rsa_cache.get(cache_key) | ||
| if rsa_value is None: | ||
| lock.acquire() | ||
| rsa_value = rsa_cache.get(cache_key) | ||
| if rsa_value is not None: | ||
| return rsa_value | ||
| try: | ||
| with lock: | ||
| rsa_value = rsa_cache.get(cache_key) | ||
| if rsa_value is not None: | ||
| return rsa_value | ||
| rsa_value = get_key_pair_by_sql() | ||
| rsa_cache.set(cache_key, rsa_value) | ||
| finally: | ||
| lock.release() | ||
| return rsa_value | ||
|
|
||
|
|
||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The code provided has some minor issues that can be addressed:
Suggested Corrections: def get_key_pair():
rsa_value = rsa_cache.get(cache_key)
if rsa_value is None:
with lock:
rsa_value = rsa_cache.get(cache_key)
if rsa_value is not None:
return rsa_value
try:
rsa_value = get_key_pair_by_sql()
rsa_cache.set(cache_key, rsa_value)
except Exception as e:
# Handle the exception here (e.g., log it)
print(f"An error occurred while fetching RSA key pair: {str(e)}")
return rsa_valueBy making these changes, the code will correctly manage its locking behavior and handle potential errors more robustly. |
||
|
|
||
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.
The provided code has several improvements compared to its previous version:
Improved Locking Mechanism: The use of a dictionary
locksalong with thread-safe operations ensures that each_idcan have its own lock object instead of acquiring the global lock. This reduces contention and improves performance when multiple models need access simultaneously.Optimized Clear Timeout Cache Logic: The logic to clear timeouts is wrapped in a separate function and executed asynchronously using
threading.Thread. This prevents blocking other parts of the application while waiting for the cache data to be cleared.Simplified Get Model Method: Directly fetching and caching the model instance inside the method simplifies the control flow, making it more concise and easier to read.
Here's an optimized version of the code:
Key Changes:
get_model.get_model.These changes should improve efficiency and robustness in managing models in your application.