【问题标题】:Delete specific route cache in Flask-Caching删除 Flask-Caching 中的特定路由缓存
【发布时间】:2022-11-25 20:54:31
【问题描述】:

如果出现错误或变量为空,我试图删除特定路径上的烧瓶缓存,但我不知道该怎么做。

我发现了这个,但我认为这对我的情况没有帮助:

Delete specific cache in Flask-Cache or Flask-Caching

这是我的代码:

@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)

当 tts_out 为 None 并且出现异常时,我需要清除缓存

如果先例请求错误,我需要客户端调用 utils.get_tts 方法

怎么做?

【问题讨论】:

    标签: python flask flask-caching


    【解决方案1】:

    对于遇到此问题的任何人,我刚刚找到了解决方案。

    @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)
    

    【讨论】:

      猜你喜欢
      • 2016-07-10
      • 2020-01-20
      • 2012-02-10
      • 1970-01-01
      • 2020-10-29
      • 2016-12-01
      • 2015-03-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多