Commit e48d6b5045ebdc200b8ad479e7435c8c8ab4ee16
1 parent
f0c889c6
Exists in
master
Add redix key namespace
Showing
4 changed files
with
23 additions
and
25 deletions
Show diff stats
lib/dingtalk/api/base.rb
... | ... | @@ -11,7 +11,7 @@ module Dingtalk |
11 | 11 | end |
12 | 12 | |
13 | 13 | def access_token |
14 | - token = redis.get("#{@corp.corp_id}_#{ACCESS_TOKEN}") | |
14 | + token = redis.get("#{redis_prefix}:#{@corp.corp_id}_#{ACCESS_TOKEN}") | |
15 | 15 | token.to_s.empty? ? set_access_token : token |
16 | 16 | end |
17 | 17 | |
... | ... | @@ -24,25 +24,26 @@ module Dingtalk |
24 | 24 | end |
25 | 25 | |
26 | 26 | def js_ticket |
27 | - ticket = redis.get("#{@corp.corp_id}_#{JS_TICKET}") | |
27 | + ticket = redis.get("#{redis_prefix}:#{@corp.corp_id}_#{JS_TICKET}") | |
28 | 28 | ticket.to_s.empty? ? set_js_ticket : ticket |
29 | 29 | end |
30 | 30 | |
31 | 31 | def set_corp_access_token |
32 | 32 | res = http_get("#{ENDPOINT}/gettoken?corpid=#{@corp.corp_id}&corpsecret=#{@corp.corp_secret}") |
33 | - key = "#{@corp.corp_id}_#{ACCESS_TOKEN}" | |
34 | - redis.set(key, res['access_token']) | |
35 | - redis.expire(key, 6600) | |
36 | - redis.get(key) | |
33 | + key = "#{redis_prefix}:#{@corp.corp_id}_#{ACCESS_TOKEN}" | |
34 | + redis.set(key, res['access_token'], {ex: 6600}) | |
35 | + res['access_token'] | |
37 | 36 | end |
38 | 37 | |
39 | 38 | def set_js_ticket |
40 | - key = "#{@corp.corp_id}_#{JS_TICKET}" | |
39 | + key = "#{redis_prefix}:#{@corp.corp_id}_#{JS_TICKET}" | |
41 | 40 | res = http_get("#{ENDPOINT}/get_jsapi_ticket?access_token=#{access_token}") |
42 | - redis.set(key, res['ticket']) | |
43 | - # redis.expire(key, res['expires_in']) | |
44 | - redis.expire(key, 6600) | |
45 | - redis.get(key) | |
41 | + redis.set(key, res['ticket'], {ex: 6600}) | |
42 | + res['ticket'] | |
43 | + end | |
44 | + | |
45 | + def redis_prefix | |
46 | + Dingtalk.config.redis_prefix || 'dingtalk' | |
46 | 47 | end |
47 | 48 | |
48 | 49 | private | ... | ... |
lib/dingtalk/api/sns.rb
... | ... | @@ -9,15 +9,14 @@ module Dingtalk |
9 | 9 | end |
10 | 10 | |
11 | 11 | def access_token |
12 | - token = redis.get(ACCESS_TOKEN) | |
12 | + token = redis.get("#{redis_prefix}:#{ACCESS_TOKEN}") | |
13 | 13 | token.to_s.empty? ? set_access_token : token |
14 | 14 | end |
15 | 15 | |
16 | 16 | def set_access_token |
17 | 17 | res = http_get("gettoken?appid=#{@app_id}&appsecret=#{@app_secret}") |
18 | - redis.set(ACCESS_TOKEN, res['access_token']) | |
19 | - redis.expire(ACCESS_TOKEN, 6600) | |
20 | - redis.get(ACCESS_TOKEN) | |
18 | + redis.set("#{redis_prefix}:#{ACCESS_TOKEN}", res['access_token'], {ex: 6600}) | |
19 | + res['access_token'] | |
21 | 20 | end |
22 | 21 | |
23 | 22 | def get_persistent_code(code) | ... | ... |
lib/dingtalk/api/suite.rb
... | ... | @@ -18,7 +18,7 @@ module Dingtalk |
18 | 18 | end |
19 | 19 | |
20 | 20 | def suite_access_token |
21 | - token = redis.get(SUITE_ACCESS_TOKEN) | |
21 | + token = redis.get("#{redis_prefix}:#{SUITE_ACCESS_TOKEN}") | |
22 | 22 | token.to_s.empty? ? set_suite_access_token : token |
23 | 23 | end |
24 | 24 | |
... | ... | @@ -30,9 +30,8 @@ module Dingtalk |
30 | 30 | } |
31 | 31 | res = http_post('get_suite_token', params) |
32 | 32 | # TODO globally check response values |
33 | - redis.set(SUITE_ACCESS_TOKEN, res['suite_access_token']) | |
34 | - redis.expire(SUITE_ACCESS_TOKEN, EXPIRATION) | |
35 | - redis.get(SUITE_ACCESS_TOKEN) | |
33 | + redis.set("#{redis_prefix}:#{SUITE_ACCESS_TOKEN}", res['suite_access_token'], {ex: EXPIRATION}) | |
34 | + res['suite_access_token'] | |
36 | 35 | end |
37 | 36 | |
38 | 37 | def set_corp_access_token(corp_id, permanent_code) |
... | ... | @@ -41,9 +40,8 @@ module Dingtalk |
41 | 40 | auth_corpid: corp_id |
42 | 41 | } |
43 | 42 | res = http_post("get_corp_token?suite_access_token=#{suite_access_token}", params) |
44 | - redis.set("#{corp_id}_#{ACCESS_TOKEN}", res['access_token']) | |
45 | - redis.expire("#{corp_id}_#{ACCESS_TOKEN}", EXPIRATION) | |
46 | - redis.get("#{corp_id}_#{ACCESS_TOKEN}") | |
43 | + redis.set("#{redis_prefix}:#{corp_id}_#{ACCESS_TOKEN}", res['access_token'], {ex: EXPIRATION}) | |
44 | + res['access_token'] | |
47 | 45 | end |
48 | 46 | |
49 | 47 | def activate_suite(corp_id, permanent_code) |
... | ... | @@ -66,7 +64,7 @@ module Dingtalk |
66 | 64 | end |
67 | 65 | |
68 | 66 | def suite_ticket |
69 | - redis.get(SUITE_TICKET) | |
67 | + redis.get("#{redis_prefix}:#{SUITE_TICKET}") | |
70 | 68 | end |
71 | 69 | |
72 | 70 | private | ... | ... |
lib/dingtalk/config.rb
... | ... | @@ -3,7 +3,7 @@ module Dingtalk |
3 | 3 | attr_accessor :config |
4 | 4 | |
5 | 5 | def configure |
6 | - yield self.config ||= Config.new | |
6 | + yield self.config ||= Config.new | |
7 | 7 | end |
8 | 8 | |
9 | 9 | def dingtalk_redis |
... | ... | @@ -37,6 +37,6 @@ module Dingtalk |
37 | 37 | end |
38 | 38 | |
39 | 39 | class Config |
40 | - attr_accessor :redis, :redis_options, :suite_key, :suite_secret, :suite_aes_key, :suite_token, :sns_app_id, :sns_app_secret | |
40 | + attr_accessor :redis, :redis_prefix, :redis_options, :suite_key, :suite_secret, :suite_aes_key, :suite_token, :sns_app_id, :sns_app_secret | |
41 | 41 | end |
42 | 42 | end | ... | ... |