Class: Loco::WsConnectionStorage

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/loco/ws_connection_storage.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeWsConnectionStorage

Returns a new instance of WsConnectionStorage.



15
16
17
# File 'lib/loco/ws_connection_storage.rb', line 15

def initialize
  @storage = Config.redis_instance
end

Instance Attribute Details

#storageObject (readonly)

Returns the value of attribute storage.



7
8
9
# File 'lib/loco/ws_connection_storage.rb', line 7

def storage
  @storage
end

Class Method Details

.currentObject



10
11
12
# File 'lib/loco/ws_connection_storage.rb', line 10

def current
  instance
end

Instance Method Details

#add(key, val) ⇒ Object



70
71
72
# File 'lib/loco/ws_connection_storage.rb', line 70

def add(key, val)
  storage.sadd(proper_key("s:#{key}"), val)
end

#del(key, hkey = nil) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/loco/ws_connection_storage.rb', line 43

def del(key, hkey = nil)
  if hkey.nil?
    storage.del(proper_key("k:#{key}"))
  else
    storage.hdel(proper_key("h:#{key}"), hkey)
  end
end

#exists?(key) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/loco/ws_connection_storage.rb', line 23

def exists?(key)
  storage.exists?(proper_key(key))
end

#get(key, hkey = nil) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/loco/ws_connection_storage.rb', line 27

def get(key, hkey = nil)
  if hkey.nil?
    storage.get(proper_key("k:#{key}"))
  else
    storage.hget(proper_key("h:#{key}"), hkey)
  end
end

#hlen(key) ⇒ Object



66
67
68
# File 'lib/loco/ws_connection_storage.rb', line 66

def hlen(key)
  storage.hlen(proper_key("h:#{key}"))
end

#member?(key, val) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/loco/ws_connection_storage.rb', line 78

def member?(key, val)
  storage.sismember(proper_key("s:#{key}"), val)
end

#members(key) ⇒ Object



74
75
76
# File 'lib/loco/ws_connection_storage.rb', line 74

def members(key)
  storage.smembers(proper_key("s:#{key}"))
end

#rem(key, val) ⇒ Object



82
83
84
# File 'lib/loco/ws_connection_storage.rb', line 82

def rem(key, val)
  storage.srem(proper_key("s:#{key}"), val)
end

#scan(match: nil, all: false, &block) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/loco/ws_connection_storage.rb', line 51

def scan(match: nil, all: false, &block)
  match = 'uuid:*' if all
  storage.scan_each(match: "#{proper_key('s:')}#{match}").each do |key|
    if all
      yield(key.split('uuid:').last)
    else
      storage.smembers(key).each(&block)
    end
  end
end

#scan_hash(key, &block) ⇒ Object



62
63
64
# File 'lib/loco/ws_connection_storage.rb', line 62

def scan_hash(key, &block)
  storage.hscan_each(proper_key("h:#{key}"), &block)
end

#set(key, val, opts = {}) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/loco/ws_connection_storage.rb', line 35

def set(key, val, opts = {})
  if val.is_a?(Hash)
    storage.hset(proper_key("h:#{key}"), val)
  else
    storage.set(proper_key("k:#{key}"), val, ex: opts[:ex])
  end
end

#type(key) ⇒ Object



19
20
21
# File 'lib/loco/ws_connection_storage.rb', line 19

def type(key)
  storage.type(proper_key(key))
end