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

Implement 'StockManager' for in-memory cache to avoid unnecessary database interactions #25

Open
Jackson-Wozniak opened this issue Sep 27, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@Jackson-Wozniak
Copy link
Owner

Jackson-Wozniak commented Sep 27, 2024

Currently, the program queries the database to get all stocks, changes their prices, and then saves them for every single cycle. This could be improved by creating a system where stocks are queried at the beginning of the day and kept in memory, changing prices each cycle and then being saved at the end of the day (at which point the Map is flushed). This could be done by using a ConcurrentHashMap, ensuring that controllers and other classes will still be able to retrieve stock information while the prices are being changed. The StockManager class would control all stock-related database interactions, ensuring that stocks are not out-of-date at any point

Note: ConcurrentHashMap would create situations where stock prices are one cycle out-of-date when another class attempts to retrieve stock info during a price-change cycle. This is relatively unimportant because the price change would be insignificant. This solution would ensure thread-safety between the StockMangager and any class that uses it to retrieve stock info.

Notes:

  • StockManager would need a fallback mechanism, where it will get stock info directly from the database if it is not in the HashMap. This is most relevant at the end of the day, when the HashMap is saved to the database and then flushed. Between the end-of-day and the query to fill the HashMap at the beginning of the day, any retrieval attempts to the Map will fail, meaning the fallback is necessary. However, this is a fair tradeoff rather than always keeping the Stocks in the map, because it ensures that new stocks begin changing at the start of the NEXT day after they are added.
  • This approach makes the size of the stock classes far more relevant, and the nature of the current Stock entity means that it grows in size throughout the running of the program (with PriceHistory, NewsEvent, etc.).
@Jackson-Wozniak Jackson-Wozniak added the enhancement New feature or request label Sep 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant