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

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を含めたい場合に使用する)

Parameters:

  • attr_name (String|Symbol)

    キー名

  • unique_attr (String|Symbol) (defaults to: nil)

    インスタンスの固有キーとして使用するメソッド名

Returns:

  • (String)


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>”)

Parameters:

  • unique_attr (String|Symbol) (defaults to: nil)

    インスタンスの固有キーとして使用するメソッド名

Returns:

  • (String)


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にしたロックをかけてブロック内の処理を実行

Examples:

lock(attr_key(:foo)) {
  # some processing
}

Parameters:

  • base_key (String)

    ロックを取得するリソースのkey

Yields:

  • ロック中に実行する処理のブロック



132
133
134
# File 'lib/redis/helper.rb', line 132

def lock(base_key, &block)
  self.class.lock(base_key, &block)
end

#redisRedis

Redis.currentへのショートカット

Returns:



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値を生成

Examples:

# 2016/10/26 正午にexpireする
redis.setex(key, ttl_to(Time.zone.parse("2016-10-26 12:00:00")), value)
# 24時間後にexpireする
redis.setex(key, ttl_to(1.day.since), value)

Parameters:

  • to_time (Time)

    expireする時間

  • from_time (Time) (defaults to: Time.current)

    現在時間

  • unsigned_non_zero (Boolean) (defaults to: true)

    計算結果のttlが0の場合1を返す

Returns:

  • (Integer)


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

インスタンス固有のキーを取得

Parameters:

  • unique_attr (String|Symbol) (defaults to: nil)

    インスタンスの固有キーとして使用するメソッド名



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