Commit bdfb26e9 by lanrion

Merge pull request #85 from wikimo/master

add config option for redis key expired
parents 555ac834 77af8d4d
...@@ -88,6 +88,10 @@ module WeixinAuthorize ...@@ -88,6 +88,10 @@ module WeixinAuthorize
"https://open.weixin.qq.com#{url}" "https://open.weixin.qq.com#{url}"
end end
def calculate_expire(expires_in)
Time.now + expires_in.to_i - key_expired
end
end end
end end
...@@ -13,6 +13,10 @@ module WeixinAuthorize ...@@ -13,6 +13,10 @@ module WeixinAuthorize
@redis ||= config.redis @redis ||= config.redis
end end
def key_expired
config.key_expired || 100
end
# 可选配置: RestClient timeout, etc. # 可选配置: RestClient timeout, etc.
# key 必须是符号 # key 必须是符号
# 如果出现 RestClient::SSLCertificateNotVerified Exception: SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed # 如果出现 RestClient::SSLCertificateNotVerified Exception: SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed
...@@ -26,6 +30,6 @@ module WeixinAuthorize ...@@ -26,6 +30,6 @@ module WeixinAuthorize
end end
class Config class Config
attr_accessor :redis, :rest_client_options attr_accessor :redis, :rest_client_options, :key_expired
end end
end end
...@@ -32,7 +32,7 @@ module WeixinAuthorize ...@@ -32,7 +32,7 @@ module WeixinAuthorize
def set_jsticket def set_jsticket
result = client.http_get("/ticket/getticket", {type: 1}).result result = client.http_get("/ticket/getticket", {type: 1}).result
client.jsticket = result["ticket"] client.jsticket = result["ticket"]
client.jsticket_expired_at = result["expires_in"] + Time.now.to_i client.jsticket_expired_at = WeixinAuthorize.calculate_expire(result["expires_in"])
end end
end end
......
...@@ -19,7 +19,7 @@ module WeixinAuthorize ...@@ -19,7 +19,7 @@ module WeixinAuthorize
client.access_token, "expired_at", client.access_token, "expired_at",
client.expired_at client.expired_at
) )
weixin_redis.expireat(client.redis_key, client.expired_at.to_i-100) # 提前10秒超时 weixin_redis.expireat(client.redis_key, client.expired_at.to_i)
end end
def access_token def access_token
......
...@@ -47,7 +47,7 @@ module WeixinAuthorize ...@@ -47,7 +47,7 @@ module WeixinAuthorize
def set_access_token(access_token_infos=nil) def set_access_token(access_token_infos=nil)
token_infos = access_token_infos || http_get_access_token.result token_infos = access_token_infos || http_get_access_token.result
client.access_token = token_infos["access_token"] client.access_token = token_infos["access_token"]
client.expired_at = Time.now.to_i + token_infos["expires_in"].to_i client.expired_at = WeixinAuthorize.calculate_expire(token_infos["expires_in"])
end end
def http_get_access_token def http_get_access_token
......
...@@ -55,6 +55,7 @@ redis_with_ns = Redis::Namespace.new("#{namespace}", :redis => redis) ...@@ -55,6 +55,7 @@ redis_with_ns = Redis::Namespace.new("#{namespace}", :redis => redis)
WeixinAuthorize.configure do |config| WeixinAuthorize.configure do |config|
config.redis = redis_with_ns config.redis = redis_with_ns
# config.key_expired = 200
config.rest_client_options = {timeout: 10, open_timeout: 10, verify_ssl: true} config.rest_client_options = {timeout: 10, open_timeout: 10, verify_ssl: true}
end end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment