Class: Ribbon::Intercom::Service::Channel::Stores::RedisStore
- Defined in:
- lib/ribbon/intercom/service/channel/stores/redis_store.rb
Defined Under Namespace
Classes: Mutex
Instance Method Summary collapse
- #delete(channel) ⇒ Object
-
#initialize(params = {}) ⇒ RedisStore
constructor
A new instance of RedisStore.
- #lookup_channel(token) ⇒ Object
- #persist(channel) ⇒ Object
- #token_exists?(token) ⇒ Boolean
- #with_lock(channel, ttl = 1000) ⇒ Object
Methods inherited from Store
Constructor Details
#initialize(params = {}) ⇒ RedisStore
Returns a new instance of RedisStore.
9 10 11 12 13 14 15 16 17 |
# File 'lib/ribbon/intercom/service/channel/stores/redis_store.rb', line 9 def initialize(params={}) if params[:url] @_redis = Redis.new(url: params[:url]) elsif params[:redis] @_redis = params[:redis] else raise Errors::InvalidStoreParamsError end end |
Instance Method Details
#delete(channel) ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/ribbon/intercom/service/channel/stores/redis_store.rb', line 58 def delete(channel) raise Errors::InvalidChannelError, channel.inspect unless channel.is_a?(Channel) @_redis.del(_key_name(channel)) @_redis.del(_key_name(channel, 'permissions')) nil end |
#lookup_channel(token) ⇒ Object
23 24 25 26 27 |
# File 'lib/ribbon/intercom/service/channel/stores/redis_store.rb', line 23 def lookup_channel(token) if (data=_load_data(token)) Channel.new(self, data) end end |
#persist(channel) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/ribbon/intercom/service/channel/stores/redis_store.rb', line 29 def persist(channel) raise Errors::InvalidChannelError, channel.inspect unless channel.is_a?(Channel) channel.tap { |channel| data_hash = {} [:name, :token, :secret_hash_crt, :secret_hash_prv].each { |key| value = channel.send(key) data_hash[key] = value if value } # Save channel data as hash @_redis.mapped_hmset(_key_name(channel), data_hash) # Associate permissions set to its channel channel..each { |p| @_redis.sadd(_key_name(channel, 'permissions'), p) } # Save signing keys key_hash = Hash[ channel.signing_keys.map { |key_id, data| [key_id, Base64.strict_encode64(Marshal.dump(data))] } ] @_redis.mapped_hmset(_key_name(channel, 'signing_keys'), key_hash) unless key_hash.empty? } end |
#token_exists?(token) ⇒ Boolean
19 20 21 |
# File 'lib/ribbon/intercom/service/channel/stores/redis_store.rb', line 19 def token_exists?(token) @_redis.exists("channel:#{token}") end |
#with_lock(channel, ttl = 1000) ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/ribbon/intercom/service/channel/stores/redis_store.rb', line 66 def with_lock(channel, ttl=1000) Mutex.new(@_redis, _key_name(channel, 'lock')).synchronize(ttl) { |validity| start_time = Mutex.time_in_ms channel.refresh(_load_data(channel.token)) yield validity - (Mutex.time_in_ms - start_time) } end |