以钉钉 API 调用举个例子(js ticket 需要缓存):
def get_js_ticket_1(access_token)
Rails.logger.info 'fetching dingding js ticket'
res = Net::HTTP.get(URI.parse("https://oapi.dingtalk.com/get_jsapi_ticket?access_token=#{access_token}"))
if res
result = ActiveSupport::JSON.decode(res)
p result
if result and result['errcode'] == 0
return result['ticket']
end
end
raise 'Cannot get access token for Dingding'
end
def get_cached_js_ticket
Rails.cache.fetch('dingding_js_t', expires_in: 1.hour) do
ak = get_access_token
get_js_ticket_1(ak)
end
end
这里get_cached_js_ticket
是没有参数的,可以正确执行缓存。
但是:
def get_js_ticket_2(access_token)
Rails.cache.fetch('dingding_js_t', expires_in: 1.hour) do
Rails.logger.info 'fetching dingding js ticket'
res = Net::HTTP.get(URI.parse("https://oapi.dingtalk.com/get_jsapi_ticket?access_token=#{access_token}"))
if res
result = ActiveSupport::JSON.decode(res)
p result
if result and result['errcode'] == 0
return result['ticket']
end
end
raise 'Cannot get access token for Dingding'
end
end
则不能成功缓存。
感觉好黑科技啊,内层的方法如何检测外层方法有没有参数?怎么实现的?