Class: Common::RedisStore

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Callbacks, ActiveModel::Naming
Includes:
ActiveModel::Serialization, ActiveModel::Validations
Defined in:
lib/common/models/redis_store.rb

Constant Summary collapse

REQ_CLASS_INSTANCE_VARS =
%i[redis_namespace redis_namespace_key].freeze

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}, persisted = false) ⇒ RedisStore

Returns a new instance of RedisStore.

Raises:

  • (NoMethodError)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/common/models/redis_store.rb', line 35

def initialize(attributes = {}, persisted = false)
  undefined = REQ_CLASS_INSTANCE_VARS.select { |class_var| send(class_var).nil? }
  raise NoMethodError, "Required class methods #{undefined.join(', ')} are not defined" if undefined.any?

  begin
    super(attributes)
  rescue NoMethodError
    Rails.logger.error("attributes failure: #{attributes}")
    raise
  end

  @persisted = persisted
  run_callbacks :initialize
end

Class Attribute Details

.redis_namespaceObject (readonly)

Returns the value of attribute redis_namespace.



17
18
19
# File 'lib/common/models/redis_store.rb', line 17

def redis_namespace
  @redis_namespace
end

.redis_namespace_keyObject (readonly)

Returns the value of attribute redis_namespace_key.



17
18
19
# File 'lib/common/models/redis_store.rb', line 17

def redis_namespace_key
  @redis_namespace_key
end

.redis_namespace_ttlObject (readonly)

Returns the value of attribute redis_namespace_ttl.



17
18
19
# File 'lib/common/models/redis_store.rb', line 17

def redis_namespace_ttl
  @redis_namespace_ttl
end

Class Method Details

.create(attributes) ⇒ Object



88
89
90
91
92
# File 'lib/common/models/redis_store.rb', line 88

def self.create(attributes)
  instance = new(attributes)
  instance.save
  instance
end

.delete(redis_key = nil) ⇒ Object



98
99
100
# File 'lib/common/models/redis_store.rb', line 98

def self.delete(redis_key = nil)
  redis_namespace.del(redis_key)
end

.exists?(redis_key = nil) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/common/models/redis_store.rb', line 84

def self.exists?(redis_key = nil)
  redis_namespace.exists?(redis_key)
end

.find(redis_key = nil) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/common/models/redis_store.rb', line 50

def self.find(redis_key = nil)
  return nil if redis_key.nil?

  response = redis_namespace.get(redis_key)
  return nil unless response

  attributes = Oj.load(response)
  return nil if attributes.blank?

  unless attributes.is_a?(Hash)
    Rails.logger.info("redis_namespace: #{redis_namespace.inspect} - response: #{response}
                        - oj parsed attributes: #{attributes} redis_key: #{redis_key}")

    nil if redis_key.blank? # Case where session[:token] is empty and response returns 1
  end

  object = new(attributes, true)
  if object.valid?
    object
  else
    redis_namespace.del(redis_key)
    nil
  end
end

.find_or_build(redis_key) ⇒ Object



75
76
77
# File 'lib/common/models/redis_store.rb', line 75

def self.find_or_build(redis_key)
  find(redis_key) || new({ @redis_namespace_key => redis_key })
end

.keysObject



94
95
96
# File 'lib/common/models/redis_store.rb', line 94

def self.keys
  redis_namespace.keys
end

.pop(redis_key = nil) ⇒ Object



79
80
81
82
# File 'lib/common/models/redis_store.rb', line 79

def self.pop(redis_key = nil)
  object = find(redis_key)
  delete(redis_key) && object if object
end

.redis_key(key) ⇒ Object



30
31
32
# File 'lib/common/models/redis_store.rb', line 30

def self.redis_key(key)
  @redis_namespace_key = key
end

.redis_store(namespace) ⇒ Object



20
21
22
# File 'lib/common/models/redis_store.rb', line 20

def self.redis_store(namespace)
  @redis_namespace = Redis::Namespace.new(namespace, redis: $redis)
end

.redis_ttl(ttl) ⇒ Object



25
26
27
# File 'lib/common/models/redis_store.rb', line 25

def self.redis_ttl(ttl)
  @redis_namespace_ttl = ttl
end

Instance Method Details

#destroyObject

The instance should be frozen once destroyed, since object can no longer be persisted. See also: ActiveRecord::Persistence#destroy



126
127
128
129
130
131
# File 'lib/common/models/redis_store.rb', line 126

def destroy
  count = redis_namespace.del(attributes[redis_namespace_key])
  @destroyed = true
  freeze
  count
end

#destroyed?Boolean

Returns:

  • (Boolean)


150
151
152
# File 'lib/common/models/redis_store.rb', line 150

def destroyed?
  @destroyed == true
end

#expire(ttl) ⇒ Object



142
143
144
# File 'lib/common/models/redis_store.rb', line 142

def expire(ttl)
  redis_namespace.expire(attributes[redis_namespace_key], ttl)
end

#initialize_dup(other) ⇒ Object



133
134
135
136
# File 'lib/common/models/redis_store.rb', line 133

def initialize_dup(other)
  initialize_copy(other)
  @destroyed = false
end

#persisted?Boolean

Returns:

  • (Boolean)


146
147
148
# File 'lib/common/models/redis_store.rb', line 146

def persisted?
  @persisted
end

#saveObject



102
103
104
105
106
107
108
# File 'lib/common/models/redis_store.rb', line 102

def save
  return false unless valid?

  redis_namespace.set(attributes[redis_namespace_key], Oj.dump(attributes))
  expire(redis_namespace_ttl) if defined? redis_namespace_ttl
  @persisted = true
end

#save!Object



115
116
117
# File 'lib/common/models/redis_store.rb', line 115

def save!
  raise Common::Exceptions::ValidationErrors, self unless save
end

#ttlObject



138
139
140
# File 'lib/common/models/redis_store.rb', line 138

def ttl
  redis_namespace.ttl(attributes[redis_namespace_key])
end

#update(attributes_hash) ⇒ Object



119
120
121
122
# File 'lib/common/models/redis_store.rb', line 119

def update(attributes_hash)
  self.attributes = attributes_hash
  save
end

#update!(attributes_hash) ⇒ Object



110
111
112
113
# File 'lib/common/models/redis_store.rb', line 110

def update!(attributes_hash)
  self.attributes = attributes_hash
  save!
end