Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def get_current_btc(self, time: datetime = None) -> Decimal:
- """
- Get how much btc this investment is currently worth.
- :param time: the time you want data regarding. Cant ever be after closed_date
- :return: current worth in btc
- """
- time, cache_time_name = _default_time_and_cache_name(self._default_time(time))
- def inner_method():
- fraction = self.get_fraction(time=time)
- if fraction != 0:
- return fraction * \
- GlobalBalance.objects.get_btc_by_time(time=time)
- return Decimal(0)
- return get_and_or_set_cache(f"investment_{self.pk}_{cache_time_name}_current_btc",
- inner_method,
- 60 * 5)
- def get_and_or_set_cache(key: str, setterMethod: FunctionType, timeout: int = None) -> Any:
- """
- Method for getting the content of the cache and if it's empty setting it, so it's use-able for the next request.
- :param key: the key of value in the cache
- :param setterMethod: method used to set the cache value if the cache is invalid
- :param timeout: how long the cache is valid - none if forever
- :return: the value of the cache or the setterMethod if the cache is invalid
- """
- output = cache.get(key)
- if not output:
- output = setterMethod()
- cache.set(key, output, timeout)
- return output
Add Comment
Please, Sign In to add comment