简体   繁体   English

在 clojure 中缓存 api 调用

[英]Caching api calls in clojure

I want to implement caching of api in clojure, In my application I have api's which are called for some of the functionalities.我想在 clojure 中实现 api 的缓存,在我的应用程序中,我有 api,它们被调用用于某些功能。 I want to reduce that api calls.我想减少那个 api 调用。 I want to use clojure.core.cache.wrapped which is implemented upon clojure.core.cache .我想使用在clojure.core.cache.wrapped上实现的clojure.core.cache I want to cache my api call response base on the url.我想根据 url 缓存我的 api 调用响应。 The url is GET and has query inside the url that differentiates the response url 是 GET 并且在 url 中有查询来区分响应

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 .我想以一种根据cid缓存的方式实现。 In above code 3 is max-retries在上面的代码 3 是最大重试次数

With current implementation I am getting below error.通过当前的实现,我得到了以下错误。

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

I got the solution, The main mistake I made here is that I implemented this in get-data-caller我得到了解决方案,我在这里犯的主要错误是我在get-data-caller中实现了这个

lookup-or-miss actually accepts 3 params 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.

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.如果 key 的缓存数据不存在,则第三个 arg 中的 fn 将以 key 作为 arg 调用以获取数据。

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所以这里的lookup-or-miss将在my-cache中搜索req-url键,如果存在它将直接返回存储的值,如果没有则它将调用http-requestreq-url作为arg

So lookup-or-miss will be executed something like this;所以lookup-or-miss将像这样执行;

pseudo code for understanding用于理解的伪代码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM