Skip to content

Commit

Permalink
refactor(candle): streamline caching logic and ensure immediate mater…
Browse files Browse the repository at this point in the history
…ialization of database results
  • Loading branch information
saleh-mir committed Dec 15, 2024
1 parent 604bbd8 commit cb70258
Showing 1 changed file with 14 additions and 17 deletions.
31 changes: 14 additions & 17 deletions jesse/services/candle.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,22 +257,19 @@ def _get_candles_from_db(
key = jh.key(exchange, symbol)
cache_key = f"{start_date_timestamp}-{finish_date_timestamp}-{key}"
cached_value = cache.get_value(cache_key)
else:
cached_value = None

# if cache exists use cache_value
if cached_value:
candles_tuple = cached_value
else:
candles_tuple = Candle.select(
Candle.timestamp, Candle.open, Candle.close, Candle.high, Candle.low,
Candle.volume
).where(
Candle.exchange == exchange,
Candle.symbol == symbol,
Candle.timeframe == '1m' or Candle.timeframe.is_null(),
Candle.timestamp.between(start_date_timestamp, finish_date_timestamp)
).order_by(Candle.timestamp.asc()).tuples()
if cached_value:
return np.array(cached_value)

# Always materialize the database results immediately
candles_tuple = list(Candle.select(
Candle.timestamp, Candle.open, Candle.close, Candle.high, Candle.low,
Candle.volume
).where(
Candle.exchange == exchange,
Candle.symbol == symbol,
Candle.timeframe == '1m' or Candle.timeframe.is_null(),
Candle.timestamp.between(start_date_timestamp, finish_date_timestamp)
).order_by(Candle.timestamp.asc()).tuples())

# validate the dates
if start_date_timestamp == finish_date_timestamp:
Expand All @@ -284,7 +281,7 @@ def _get_candles_from_db(

if caching:
# cache for 1 week it for near future calls
cache.set_value(cache_key, tuple(candles_tuple), expire_seconds=60 * 60 * 24 * 7)
cache.set_value(cache_key, candles_tuple, expire_seconds=60 * 60 * 24 * 7)

return np.array(candles_tuple)

Expand Down

0 comments on commit cb70258

Please sign in to comment.