Class: Chatbox::MemcachedStore
- Inherits:
-
Object
- Object
- Chatbox::MemcachedStore
- Defined in:
- lib/chatbox/memcached_store.rb
Defined Under Namespace
Classes: Record
Instance Method Summary collapse
- #add_message(attrs) ⇒ Object
- #find_message(id) ⇒ Object
- #find_messages_by_from_id(id) ⇒ Object
- #find_messages_by_to_id(id) ⇒ Object
-
#initialize(client, id_generator: -> { SecureRandom.uuid }) ⇒ MemcachedStore
constructor
A new instance of MemcachedStore.
- #mark_message_read!(id) ⇒ Object
- #mark_message_unread!(id) ⇒ Object
Constructor Details
#initialize(client, id_generator: -> { SecureRandom.uuid }) ⇒ MemcachedStore
Returns a new instance of MemcachedStore.
6 7 8 9 |
# File 'lib/chatbox/memcached_store.rb', line 6 def initialize(client, id_generator: -> { SecureRandom.uuid }) @client = client @id_generator = id_generator end |
Instance Method Details
#add_message(attrs) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/chatbox/memcached_store.rb', line 11 def (attrs) id = id_generator.() write "messages/#{id}", { from_id: attrs[:from_id], to_id: attrs[:to_id], body: attrs[:body], read: false, } from_list = read("from/#{attrs[:from_id]}") || [] from_list << {from_id: attrs[:from_id], message_id: id} write "from/#{attrs[:from_id]}", from_list to_list = read("to/#{attrs[:to_id]}") || [] to_list << {to_id: attrs[:to_id], message_id: id} write "to/#{attrs[:to_id]}", to_list end |
#find_message(id) ⇒ Object
46 47 48 49 50 |
# File 'lib/chatbox/memcached_store.rb', line 46 def (id) if attrs = read("messages/#{id}") Record.new id, attrs end end |
#find_messages_by_from_id(id) ⇒ Object
62 63 64 65 66 67 68 69 70 |
# File 'lib/chatbox/memcached_store.rb', line 62 def (id) if attrs_list = read("from/#{id}") attrs_list.map do |attrs| attrs[:message_id] end else [] end end |
#find_messages_by_to_id(id) ⇒ Object
52 53 54 55 56 57 58 59 60 |
# File 'lib/chatbox/memcached_store.rb', line 52 def (id) if attrs_list = read("to/#{id}") attrs_list.map do |attrs| attrs[:message_id] end else [] end end |
#mark_message_read!(id) ⇒ Object
32 33 34 35 36 |
# File 'lib/chatbox/memcached_store.rb', line 32 def (id) attrs = read "messages/#{id}" attrs[:read] = true write "messages/#{id}", attrs end |
#mark_message_unread!(id) ⇒ Object
38 39 40 41 42 |
# File 'lib/chatbox/memcached_store.rb', line 38 def (id) attrs = read "messages/#{id}" attrs[:read] = false write "messages/#{id}", attrs end |