Class: RedisNotifier::Base
- Inherits:
-
Object
- Object
- RedisNotifier::Base
- Defined in:
- lib/redis_notifier.rb
Constant Summary collapse
- NOTIFY_KEYSPACE_EVENTS =
'notify-keyspace-events'
Instance Attribute Summary collapse
-
#callbacks ⇒ Object
readonly
Returns the value of attribute callbacks.
-
#db ⇒ Object
readonly
Returns the value of attribute db.
-
#listened_channels ⇒ Object
readonly
Returns the value of attribute listened_channels.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#redis ⇒ Object
readonly
Returns the value of attribute redis.
Instance Method Summary collapse
- #build_channel_for(type, prefix) ⇒ Object
- #find_type_from(channel) ⇒ Object
-
#initialize(redis, db = 0, keyspace_config = 'KEA', logger = NullLogger.new) ⇒ Base
constructor
A new instance of Base.
- #listen ⇒ Object
- #observe_key(key, &block) ⇒ Object
- #on(type, prefix, &block) ⇒ Object
- #on_event(event_name, &block) ⇒ Object
Constructor Details
#initialize(redis, db = 0, keyspace_config = 'KEA', logger = NullLogger.new) ⇒ Base
Returns a new instance of Base.
21 22 23 24 25 26 27 28 |
# File 'lib/redis_notifier.rb', line 21 def initialize(redis, db=0, keyspace_config='KEA', logger=NullLogger.new) @logger = logger @db = db.to_s @redis = redis redis.config(:set, NOTIFY_KEYSPACE_EVENTS, keyspace_config) @callbacks = { key: {}, event: {} } @listened_channels = [] end |
Instance Attribute Details
#callbacks ⇒ Object (readonly)
Returns the value of attribute callbacks.
19 20 21 |
# File 'lib/redis_notifier.rb', line 19 def callbacks @callbacks end |
#db ⇒ Object (readonly)
Returns the value of attribute db.
19 20 21 |
# File 'lib/redis_notifier.rb', line 19 def db @db end |
#listened_channels ⇒ Object (readonly)
Returns the value of attribute listened_channels.
19 20 21 |
# File 'lib/redis_notifier.rb', line 19 def listened_channels @listened_channels end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
19 20 21 |
# File 'lib/redis_notifier.rb', line 19 def logger @logger end |
#redis ⇒ Object (readonly)
Returns the value of attribute redis.
19 20 21 |
# File 'lib/redis_notifier.rb', line 19 def redis @redis end |
Instance Method Details
#build_channel_for(type, prefix) ⇒ Object
46 47 48 49 50 51 52 53 54 |
# File 'lib/redis_notifier.rb', line 46 def build_channel_for(type, prefix) namespace = case type when :event 'keyevent' when :key 'keyspace' end "__#{namespace}@#{db}__:#{prefix}" end |
#find_type_from(channel) ⇒ Object
56 57 58 59 60 61 62 63 64 |
# File 'lib/redis_notifier.rb', line 56 def find_type_from(channel) if channel.include?('__keyevent@') :event elsif channel.include?('__keyspace@') :key else nil end end |
#listen ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/redis_notifier.rb', line 66 def listen logger.debug("listened channels: #{listened_channels}") redis.subscribe(listened_channels) do |on| on. do |ch, msg| logger.debug("message received: channel=#{ch}; msg=#{msg}") extracted_key = ch.split(':')[1..-1].join(':') logger.debug("extracted key: #{extracted_key}") if type = find_type_from(ch) callbacks[type][extracted_key].call(msg) else logger.warn("message received but not in keyspace or keyevent") end end end end |
#observe_key(key, &block) ⇒ Object
34 35 36 |
# File 'lib/redis_notifier.rb', line 34 def observe_key(key, &block) on :key, key, &block end |
#on(type, prefix, &block) ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/redis_notifier.rb', line 38 def on(type, prefix, &block) raise NoBlockGiven unless block_given? channel = build_channel_for(type, prefix) return if @listened_channels.include?(channel) @listened_channels << channel @callbacks[type][prefix.to_s] = block end |
#on_event(event_name, &block) ⇒ Object
30 31 32 |
# File 'lib/redis_notifier.rb', line 30 def on_event(event_name, &block) on :event, event_name, &block end |