Module: Redis::Helper
- Defined in:
- lib/redis/helper.rb,
lib/redis/helper/lock.rb,
lib/redis/helper/version.rb
Overview
redisのヘルパー
Defined Under Namespace
Modules: ClassMethods Classes: Lock, LockTimeout, UnknownUniqueValue
Constant Summary collapse
- DEFAULT_UNIQUE_ATTR_NAME =
デフォルトの固有キー名
:id
- REDIS_KEY_DELIMITER =
redisキーの区切り文字
":".freeze
- LOCK_POSTFIX =
ロックを取得に利用する接尾辞
"lock".freeze
- VERSION =
バージョン
"1.3.0".freeze
Class Method Summary collapse
Instance Method Summary collapse
-
#attr_key(attr_name, unique_attr = nil) ⇒ String
instance固有のkeyとattr_nameからkeyを生成する (instanceに複数のkeyを設定したい場合やkeyにattr_nameを含めたい場合に使用する).
-
#instance_key(unique_attr = nil) ⇒ String
instance固有のkeyを生成する (“<Class Name>:<unique key>”).
-
#lock(base_key) { ... } ⇒ Object
特定のkeyをbaseにしたロックをかけてブロック内の処理を実行.
-
#redis ⇒ Redis
Redis.currentへのショートカット.
-
#ttl_to(to_time, from_time = Time.current, unsigned_non_zero: true) ⇒ Integer
引数で指定した時間にexpireするためのttl値を生成.
-
#unique_key(unique_attr = nil) ⇒ Object
インスタンス固有のキーを取得.
Class Method Details
.included(klass) ⇒ Object
44 45 46 |
# File 'lib/redis/helper.rb', line 44 def self.included(klass) klass.extend ClassMethods end |
Instance Method Details
#attr_key(attr_name, unique_attr = nil) ⇒ String
instance固有のkeyとattr_nameからkeyを生成する(instanceに複数のkeyを設定したい場合やkeyにattr_nameを含めたい場合に使用する)
92 93 94 |
# File 'lib/redis/helper.rb', line 92 def attr_key(attr_name, unique_attr = nil) self.class.generate_key(unique_key(unique_attr), attr_name) end |
#instance_key(unique_attr = nil) ⇒ String
instance固有のkeyを生成する (“<Class Name>:<unique key>”)
99 100 101 |
# File 'lib/redis/helper.rb', line 99 def instance_key(unique_attr = nil) self.class.generate_key(unique_key(unique_attr)) end |
#lock(base_key) { ... } ⇒ Object
特定のkeyをbaseにしたロックをかけてブロック内の処理を実行
132 133 134 |
# File 'lib/redis/helper.rb', line 132 def lock(base_key, &block) self.class.lock(base_key, &block) end |
#redis ⇒ Redis
Redis.currentへのショートカット
121 122 123 |
# File 'lib/redis/helper.rb', line 121 def redis self.class.redis end |
#ttl_to(to_time, from_time = Time.current, unsigned_non_zero: true) ⇒ Integer
引数で指定した時間にexpireするためのttl値を生成
113 114 115 116 117 |
# File 'lib/redis/helper.rb', line 113 def ttl_to(to_time, from_time = Time.current, unsigned_non_zero: true) ttl = (to_time - from_time).to_i return ttl if ttl > 0 unsigned_non_zero ? 1 : ttl end |
#unique_key(unique_attr = nil) ⇒ Object
インスタンス固有のキーを取得
138 139 140 141 142 143 144 |
# File 'lib/redis/helper.rb', line 138 def unique_key(unique_attr = nil) attr_name = unique_attr.presence || DEFAULT_UNIQUE_ATTR_NAME if (unique_key = self.public_send(attr_name)).blank? raise UnknownUniqueValue, "unique keyとして指定された値(#{attr_name})が取得できません" end unique_key end |