Class: ViewComponentReflex::StateAdapter::Redis

Inherits:
Base
  • Object
show all
Defined in:
lib/view_component_reflex/state_adapter/redis.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(redis_opts:, ttl: 3600) ⇒ Redis

Returns a new instance of Redis.



16
17
18
19
# File 'lib/view_component_reflex/state_adapter/redis.rb', line 16

def initialize(redis_opts:, ttl: 3600)
  @client = ::Redis.new(redis_opts)
  @ttl = ttl
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



14
15
16
# File 'lib/view_component_reflex/state_adapter/redis.rb', line 14

def client
  @client
end

#ttlObject (readonly)

Returns the value of attribute ttl.



14
15
16
# File 'lib/view_component_reflex/state_adapter/redis.rb', line 14

def ttl
  @ttl
end

Instance Method Details

#set_state(request, _, key, new_state) ⇒ Object



32
33
34
35
# File 'lib/view_component_reflex/state_adapter/redis.rb', line 32

def set_state(request, _, key, new_state)
  cache_key = get_key(request, key)
  save_to_redis(cache_key, new_state)
end

#state(request, key) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/view_component_reflex/state_adapter/redis.rb', line 21

def state(request, key)
  cache_key = get_key(request, key)
  value = client.hgetall(cache_key)

  return {} if value.nil?

  value.map do |k, v|
    [k, Marshal.load(v)]
  end.to_h
end

#store_state(request, key, new_state = {}) ⇒ Object



37
38
39
40
# File 'lib/view_component_reflex/state_adapter/redis.rb', line 37

def store_state(request, key, new_state = {})
  cache_key = get_key(request, key)
  optimized_store_to_redis(cache_key, new_state, request)
end

#wrap_write_asyncObject



42
43
44
45
46
# File 'lib/view_component_reflex/state_adapter/redis.rb', line 42

def wrap_write_async
  client.pipelined do
    yield
  end
end