-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ENG-1129: aixplain sdk caching functions (#324)
* fixed corrupted file * added languages and licenses * made changes according to comments * changes to constants and re-added json checker * changes to constants and re-added json checker * added process after save json * Fixes in the caching function * Move Cache folder --------- Co-authored-by: Thiago Castro Ferreira <[email protected]>
- Loading branch information
1 parent
3852e2d
commit 7732dad
Showing
6 changed files
with
92 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import os | ||
import json | ||
import time | ||
import logging | ||
|
||
CACHE_DURATION = 24 * 60 * 60 | ||
CACHE_FOLDER = ".aixplain_cache" | ||
|
||
|
||
def save_to_cache(cache_file, data): | ||
try: | ||
os.makedirs(os.path.dirname(cache_file), exist_ok=True) | ||
with open(cache_file, "w") as f: | ||
json.dump({"timestamp": time.time(), "data": data}, f) | ||
except Exception as e: | ||
logging.error(f"Failed to save cache to {cache_file}: {e}") | ||
|
||
|
||
def load_from_cache(cache_file): | ||
if os.path.exists(cache_file) is True: | ||
with open(cache_file, "r") as f: | ||
cache_data = json.load(f) | ||
if time.time() - cache_data["timestamp"] < CACHE_DURATION: | ||
return cache_data["data"] | ||
else: | ||
return None | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters