0

I am trying to delete a flask cache on a specific route if there is an error or if a variable is empty, but i don't understand how to do it.

I have found this, but i don't think it is helpful in my case:

Delete specific cache in Flask-Cache or Flask-Caching

This is my code:

@nsaudio.route('/repeat/<string:text>/<string:chatid>/<string:voice>')
class AudioRepeatClass(Resource):
  @cache.cached(timeout=120, query_string=True)
  def get (self, text: str, chatid: str, voice: str):
    try:
      tts_out = utils.get_tts(text, voice=voice, timeout=120)
      if tts_out is not None:
        return send_file(tts_out, attachment_filename='audio.wav', mimetype='audio/x-wav')
      else:
        resp = make_response("TTS Generation Error!", 500)
        return resp
    except Exception as e:
      return make_response(str(e), 500)

I need to clear the cache when tts_out is None and when there is an Exception

I need the client to call the utils.get_tts method if the precedent request was in error

How to do that?

blastbeng
  • 187
  • 2
  • 11

1 Answers1

0

For Anyone having this proplem, I have just found the solution.

@nsaudio.route('/repeat/<string:text>/<string:chatid>/<string:voice>')
class AudioRepeatClass(Resource):
  @cache.cached(timeout=120, query_string=True)
  def get (self, text: str, chatid: str, voice: str):
    try:
      tts_out = utils.get_tts(text, voice=voice, timeout=120)
      if tts_out is not None:
        return send_file(tts_out, attachment_filename='audio.wav', mimetype='audio/x-wav')
      else:
        @after_this_request
        def clear_cache(response):
          cache.delete_memoized(AudioRepeatClass.get, self, str, str, str)
          return make_response("TTS Generation Error!", 500)
    except Exception as e:
      @after_this_request
      def clear_cache(response):
        cache.delete_memoized(AudioRepeatClass.get, self, str, str, str)
        return make_response(str(e), 500)
blastbeng
  • 187
  • 2
  • 11