Skip to content
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

cache data #26

Open
yovanoc opened this issue Dec 26, 2024 · 2 comments
Open

cache data #26

yovanoc opened this issue Dec 26, 2024 · 2 comments

Comments

@yovanoc
Copy link

yovanoc commented Dec 26, 2024

would be cool to cache financialdataset data

@tomgutt
Copy link

tomgutt commented Dec 31, 2024

Cache Implementation Details

I've implemented a robust caching solution to optimize API calls and improve performance. Here's an overview of the changes:

Installation

  1. Create a new directory src/tools/ if it doesn't exist
  2. Add two new files:
    • src/tools/cache.py - Contains the caching implementation
    • src/tools/api.py - Contains the API functions with caching integration

Cache Implementation (cache.py)

  • Created a FileCache class that handles persistent caching of API responses
  • Cache entries are stored as JSON files in a .cache directory
  • Files are organized by ticker symbol and endpoint type for easy management
  • Each cache entry includes a timestamp for TTL (Time To Live) validation
  • Default TTL is 90 days, but this is configurable
  • Cache keys are generated using a combination of request parameters and MD5 hashing
  • Includes logging for cache hits, misses, and errors

API Integration (api.py)

  • Added caching to all API endpoints (financial metrics, insider trades, prices, etc.)
  • Each API function now checks the cache before making a network request
  • Successful API responses are automatically cached for future use
  • Cache keys are constructed using relevant function parameters
  • Maintains the same API interface, making the caching layer transparent to users

Benefits

  1. Reduced API calls and associated costs
  2. Faster response times for repeated queries
  3. Less network bandwidth usage
  4. Offline functionality for recently cached data
  5. Organized cache structure for easy debugging and maintenance

How TTL Works

The Time To Live (TTL) is set to 90 days, meaning:

  • When data is first requested, it's fetched from the API and cached
  • Subsequent requests for the same data within 90 days will use the cached version
  • After 90 days, the cache is considered expired, and a new API request will be made

The caching system is completely transparent to end users - they continue to use the API functions as before, but now get the performance benefits of caching automatically.

Files (I'm not sure how to upload py files here)

cache.py: cache.txt
api.py: api.txt

@yovanoc
Copy link
Author

yovanoc commented Dec 31, 2024

Seems good to me! hope it will be integrated!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants