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

Methods inherited from Base

extend_component, extend_reflex, set_state, state, store_state, wrap_write_async

Constructor Details

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

Returns a new instance of Redis.



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

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

Instance Attribute Details

#clientObject

Returns the value of attribute client.



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

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

#extend_component(_) ⇒ Object



52
53
54
# File 'lib/view_component_reflex/state_adapter/redis.rb', line 52

def extend_component(_)
  #stub
end

#extend_reflex(_) ⇒ Object



56
57
58
# File 'lib/view_component_reflex/state_adapter/redis.rb', line 56

def extend_reflex(_)
  #stub
end

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



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

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



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

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



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

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



43
44
45
46
47
48
49
50
# File 'lib/view_component_reflex/state_adapter/redis.rb', line 43

def wrap_write_async
  client.pipelined  do |pipeline|
    original_client = client
    @client = pipeline
    yield
    @client = original_client
  end
end