I'm writing a slackbot using bolt_python which consists of writing functions that are called during events. Multiple of the events I want to respond to use the same data that I get from an external API. I'd like to avoid calling the API multiple times for the exact same data, so I'd like to cache it and share the cache.
I could make the cache use a global variable, but no one likes that.
Here's what the version using a global might look like.
dumb_global = {}
@app.command('\foo')
def foo_command(command, logger, say):
if len(dumb_global) < 1:
dumb_global = do_lots_of_io_and_networky_stuff()
say(something_useful(dumb_global))
@app.action('button-click')
def button_click(action, logger, say):
if len(dumb_global) < 1:
dumb_global = do_lots_of_io_and_networky_stuff()
say(something_else_useful(dumb_global))
What's the best way to do this?
Thanks!