How to do asynchronous update of the cache. Api is not blocked, and return cache directly each time

  1. api queryquestion is always cache, except for the first time.

  2. Whenever the question is queried later, the cache is updated asynchronously.

    (qp query
    (fn [metadata]
    (save-results-xform start-time-ms metadata query-hash (rff metadata)))
    context)

  • I ran the above code in sync. Show the error "Error reducing result rows default.clj:60".
  • I ran the above code in async(a new thread). Show the error "Exception in thread "Thread-26" java.lang.AssertionError: Assert failed: ((some-fn keyword? string?)"
    at metabase.driver$the_driver.invokeStatic(driver.clj:97)

What do I need to do?

Solved, the first error is because after the cache data is returned to the response, the question data of the asynchronous request is returned to the response
The second error is the wrong use of the Thread class library, which can be used asynchronously with a normal function

We have implemented the cache never expires, and each time the query data exceeds a certain time frame, the process of updating the cache will be run asynchronously

  1. If the query has a cache, return the cache
  2. And query new question data
  3. Then update cache data to query_cache

See this link for a specific implementation,