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



86
87
88
89
90
# File 'lib/common/models/redis_store.rb', line 86

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

.delete(redis_key = nil) ⇒ Object



96
97
98
# File 'lib/common/models/redis_store.rb', line 96

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

.exists?(redis_key = nil) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/common/models/redis_store.rb', line 82

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
# File 'lib/common/models/redis_store.rb', line 50

def self.find(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.empty? # 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



73
74
75
# File 'lib/common/models/redis_store.rb', line 73

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

.keysObject



92
93
94
# File 'lib/common/models/redis_store.rb', line 92

def self.keys
  redis_namespace.keys
end

.pop(redis_key = nil) ⇒ Object



77
78
79
80
# File 'lib/common/models/redis_store.rb', line 77

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



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

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

#destroyed?Boolean

Returns:

  • (Boolean)


148
149
150
# File 'lib/common/models/redis_store.rb', line 148

def destroyed?
  @destroyed == true
end

#expire(ttl) ⇒ Object



140
141
142
# File 'lib/common/models/redis_store.rb', line 140

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

#initialize_dup(other) ⇒ Object



131
132
133
134
# File 'lib/common/models/redis_store.rb', line 131

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

#persisted?Boolean

Returns:

  • (Boolean)


144
145
146
# File 'lib/common/models/redis_store.rb', line 144

def persisted?
  @persisted
end

#saveObject



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

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



113
114
115
# File 'lib/common/models/redis_store.rb', line 113

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

#ttlObject



136
137
138
# File 'lib/common/models/redis_store.rb', line 136

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

#update(attributes_hash) ⇒ Object



117
118
119
120
# File 'lib/common/models/redis_store.rb', line 117

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

#update!(attributes_hash) ⇒ Object



108
109
110
111
# File 'lib/common/models/redis_store.rb', line 108

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