Class: FlowChat::Session::CacheSessionStore

Inherits:
Object
  • Object
show all
Includes:
Instrumentation
Defined in:
lib/flow_chat/session/cache_session_store.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Instrumentation

#instrument, instrument

Constructor Details

#initialize(context, cache = nil) ⇒ CacheSessionStore

Returns a new instance of CacheSessionStore.

Raises:

  • (ArgumentError)


9
10
11
12
13
14
15
16
17
# File 'lib/flow_chat/session/cache_session_store.rb', line 9

def initialize(context, cache = nil)
  @context = context
  @cache = cache || FlowChat::Config.cache

  raise ArgumentError, "Cache is required. Set FlowChat::Config.cache or pass a cache instance." unless @cache

  FlowChat.logger.debug { "CacheSessionStore: Initialized cache session store for session #{session_key}" }
  FlowChat.logger.debug { "CacheSessionStore: Cache backend: #{@cache.class.name}" }
end

Instance Attribute Details

#contextObject (readonly)

Make context available for instrumentation enrichment



7
8
9
# File 'lib/flow_chat/session/cache_session_store.rb', line 7

def context
  @context
end

Instance Method Details

#clearObject Also known as: destroy



91
92
93
94
95
96
97
98
99
100
# File 'lib/flow_chat/session/cache_session_store.rb', line 91

def clear
  return unless @context

  # Use instrumentation for session destruction
  instrument(Events::SESSION_DESTROYED, {
    session_id: @context["session.id"]
  })

  @cache.delete(session_key)
end

#delete(key) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/flow_chat/session/cache_session_store.rb', line 74

def delete(key)
  return unless @context

  FlowChat.logger.debug { "CacheSessionStore: Deleting key '#{key}' from session #{session_key}" }

  data = @cache.read(session_key)
  unless data
    FlowChat.logger.debug { "CacheSessionStore: No session data found for deletion" }
    return
  end

  data.delete(key.to_s)
  @cache.write(session_key, data, expires_in: session_ttl)

  FlowChat.logger.debug { "CacheSessionStore: Key '#{key}' deleted from session" }
end

#exists?Boolean

Returns:

  • (Boolean)


105
106
107
108
109
# File 'lib/flow_chat/session/cache_session_store.rb', line 105

def exists?
  exists = @cache.exist?(session_key)
  FlowChat.logger.debug { "CacheSessionStore: Session #{session_key} exists: #{exists}" }
  exists
end

#get(key) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/flow_chat/session/cache_session_store.rb', line 19

def get(key)
  return nil unless @context

  FlowChat.logger.debug { "CacheSessionStore: Getting key '#{key}' from session #{session_key}" }

  data = @cache.read(session_key)
  session_id = @context["session.id"]

  unless data
    # Use instrumentation for cache miss
    instrument(Events::SESSION_CACHE_MISS, {
      session_id: session_id,
      key: key.to_s
    })
    return nil
  end

  value = data[key.to_s]

  # Use instrumentation for cache hit and data get
  instrument(Events::SESSION_CACHE_HIT, {
    session_id: session_id,
    key: key.to_s
  })

  instrument(Events::SESSION_DATA_GET, {
    session_id: session_id,
    key: key.to_s,
    value: value
  })

  value
end

#set(key, value) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/flow_chat/session/cache_session_store.rb', line 53

def set(key, value)
  return unless @context

  FlowChat.logger.debug { "CacheSessionStore: Setting key '#{key}' = #{value.inspect} in session #{session_key}" }

  data = @cache.read(session_key) || {}
  data[key.to_s] = value

  ttl = session_ttl
  @cache.write(session_key, data, expires_in: ttl)

  # Use instrumentation for data set
  instrument(Events::SESSION_DATA_SET, {
    session_id: @context["session.id"],
    key: key.to_s
  })

  FlowChat.logger.debug { "CacheSessionStore: Session data saved with TTL #{ttl.inspect}" }
  value
end