Commit c9a22383e6fd283f8e2b1ace4352214761dc1ad6

Authored by lanrion
1 parent 7494c737
Exists in master

添加redis配置

Showing 2 changed files with 44 additions and 1 deletions   Show diff stats
@@ -12,7 +12,30 @@ gem "qy_wechat_api", git: "https://github.com/lanrion/qy_wechat_api.git" @@ -12,7 +12,30 @@ gem "qy_wechat_api", git: "https://github.com/lanrion/qy_wechat_api.git"
12 12
13 **暂未对access_token做缓存处理,为了确保在开发过程不会出现token过期问题,请不要使用全局变量存储group_client。** 13 **暂未对access_token做缓存处理,为了确保在开发过程不会出现token过期问题,请不要使用全局变量存储group_client。**
14 14
15 -# 基本用法 15 +# 配置
  16 +此项配置,仅用于你使用redis存储token,否则不需要!
  17 +
  18 +在:your_project_name/config/initializers/qy_wechat_api.rb 添加:
  19 +
  20 +```ruby
  21 +# don't forget change namespace
  22 +namespace = "your_project_name:qy_wechat_api"
  23 +redis = Redis.new(:host => "127.0.0.1", :port => "6379", :db => 15)
  24 +
  25 +# cleanup keys in the current namespace when restart server everytime.
  26 +exist_keys = redis.keys("#{namespace}:*")
  27 +exist_keys.each{|key|redis.del(key)}
  28 +
  29 +# Give a special namespace as prefix for Redis key, when your have more than one project used qy_wechat_api, this config will make them work fine.
  30 +redis = Redis::Namespace.new("#{namespace}", :redis => redis)
  31 +
  32 +QyWechatApi.configure do |config|
  33 + config.redis = redis
  34 +end
  35 +```
  36 +
  37 +
  38 +# API基本用法
16 39
17 请务必结合:http://qydev.weixin.qq.com/wiki/index.php 理解以下API参数使用。 40 请务必结合:http://qydev.weixin.qq.com/wiki/index.php 理解以下API参数使用。
18 41
lib/qy_wechat_api/config.rb 0 → 100644
@@ -0,0 +1,20 @@ @@ -0,0 +1,20 @@
  1 +module QyWechatApi
  2 +
  3 + class << self
  4 +
  5 + attr_accessor :config
  6 +
  7 + def configure
  8 + yield self.config ||= Config.new
  9 + end
  10 +
  11 + def weixin_redis
  12 + return nil if QyWechatApi.config.nil?
  13 + @redis ||= QyWechatApi.config.redis
  14 + end
  15 + end
  16 +
  17 + class Config
  18 + attr_accessor :redis
  19 + end
  20 +end