缓存API呼叫Clojure

发布于 2025-02-07 09:34:56 字数 864 浏览 2 评论 0原文

我想在Clojure中实现API的缓存,在我的应用程序中,我的API被要求使用某些功能。我想减少API调用。我想使用clojure.core.cache.cache.trapped,该是在clojure.core.cache上实现的。我想在URL上缓存我的API呼叫响应基础。 URL在URL内获取并具有查询,以区分

for eg
http://localhost:3000/:clientid/get_data

示例代码


(:require [clojure-mauth-client.request :refer [get!]]
          [clojure.core.cache.wrapped :as cw])


(def my-cache (cw/ttl-cache-factory {} :ttl 60000))

(defn get-data-caller [cid]
  (cw/lookup-or-miss my-cache cid get-data))

(defn get-data [cid]
(let [req-url (str "/api/get-data?id=" cid)
      response (retry-request (sign-credentials #(get! base-url req-url)) 3)]
(println response))))

我要以CID的方式缓存的方式来区分响应 。 在上面的代码3中,是最大收益

,当前实现我要低于错误。

In my current code it is calling the api again and again

I want to implement caching of api in clojure, In my application I have api's which are called for some of the functionalities. I want to reduce that api calls. I want to use clojure.core.cache.wrapped which is implemented upon clojure.core.cache. I want to cache my api call response base on the url.
The url is GET and has query inside the url that differentiates the response

for eg
http://localhost:3000/:clientid/get_data

Sample code


(:require [clojure-mauth-client.request :refer [get!]]
          [clojure.core.cache.wrapped :as cw])


(def my-cache (cw/ttl-cache-factory {} :ttl 60000))

(defn get-data-caller [cid]
  (cw/lookup-or-miss my-cache cid get-data))

(defn get-data [cid]
(let [req-url (str "/api/get-data?id=" cid)
      response (retry-request (sign-credentials #(get! base-url req-url)) 3)]
(println response))))

I want to implement in a way that it caches depending on the cid.
In above code 3 is max-retries

With current implementation I am getting below error.

In my current code it is calling the api again and again

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遗失的美好 2025-02-14 09:34:56

我得到了解决方案,我在这里犯的主要错误是我在
get-data-caller

lookup-or-miss实际上接受3个参数,

lookup-or-miss [cache key fn]

Here 
1. cache is the one that we create.
2. key that we want to use as 'key' in our caching
3. The third has to be the function, that takes 'key' as an arg and gets data for us. 

So lookup-or-miss will first check if the we have cached data for the 'key' passed, if not then, that will be passed as an arg to the third arg (i.e the fn) and get the fresh data.

如果键的缓存数据不存在,则第三个ARG中的FN将与关键为获取数据。

有了以上的理解,我确实在下面重写了我的代码

(:require [clojure-mauth-client.request :refer [get!]]
          [clojure.core.cache.wrapped :as cw])


(def my-cache (cw/ttl-cache-factory {} :ttl 60000))

(defn http 
[url]
(retry-request (sign-credentials #(get! url)) 3))

(defn get-data-caller [cid]
  (get-data cid))

(defn get-data [cid]
(let [req-url (str "/api/get-data?id=" cid)
      response (cw/lookup-or-miss my-cache req-url http-request)]
(println response))))

,因此在此处查找或错过将搜索req-urlmy-cache中,如果存在,它将直接返回值存储的,如果不是这样,它将用req-url作为arg 调用http-request作为arg

,因此查找或失踪将被执行。

伪理解的伪代码

(if (contains? my-cache req-url)
     (:req-url my-cache)
     (http-request req-url))

I got the solution, The main mistake I made here is that I implemented this in
get-data-caller

lookup-or-miss actually accepts 3 params

lookup-or-miss [cache key fn]

Here 
1. cache is the one that we create.
2. key that we want to use as 'key' in our caching
3. The third has to be the function, that takes 'key' as an arg and gets data for us. 

So lookup-or-miss will first check if the we have cached data for the 'key' passed, if not then, that will be passed as an arg to the third arg (i.e the fn) and get the fresh data.

If the cache data is not present for the key, then the fn in the third arg will be called with key as arg to get the data.

With above understanding I did rewrite my code as below

(:require [clojure-mauth-client.request :refer [get!]]
          [clojure.core.cache.wrapped :as cw])


(def my-cache (cw/ttl-cache-factory {} :ttl 60000))

(defn http 
[url]
(retry-request (sign-credentials #(get! url)) 3))

(defn get-data-caller [cid]
  (get-data cid))

(defn get-data [cid]
(let [req-url (str "/api/get-data?id=" cid)
      response (cw/lookup-or-miss my-cache req-url http-request)]
(println response))))

So here lookup-or-miss will search req-url key in my-cache, if present it will directly return the value stored, if not then it will call http-request with req-url as an arg

So lookup-or-miss will be executed something like this;

pseudo code for understanding

(if (contains? my-cache req-url)
     (:req-url my-cache)
     (http-request req-url))

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文