File tree Expand file tree Collapse file tree 8 files changed +29
-42
lines changed
Expand file tree Collapse file tree 8 files changed +29
-42
lines changed Original file line number Diff line number Diff line change @@ -30,7 +30,9 @@ module Session
3030 end
3131 end
3232
33- # Local cache with TTL and LRU eviction for session data
33+ # Local cache with TTL and LRU eviction
34+ # Generic parameter T can be any type - when used for sessions, instantiate as LocalCache(SessionId(T))
35+ # where T is your session data type that includes SessionData
3436 class LocalCache (T )
3537 struct CacheEntry (T )
3638 property value : T
@@ -199,6 +201,10 @@ module Session
199201 end
200202
201203 # Main cluster coordinator for managing pub/sub and local caching
204+ #
205+ # Generic Constraint:
206+ # T must include SessionData - represents the session data type
207+ # Internally manages LocalCache(SessionId(T)) for caching complete session objects
202208 class ClusterCoordinator (T )
203209 getter local_cache : LocalCache (SessionId (T ))
204210 getter node_id : String
Original file line number Diff line number Diff line change 11module Session
2+ # Provider module for session lifecycle management
3+ # Included by Store(T) to provide session operations
4+ # The including class must define generic type parameter T where T : SessionData
25 module Provider
36 abstract def storage : String
7+ # Current session - type is determined by the generic parameter T of the including Store(T)
48 abstract def current_session
59
610 macro included
Original file line number Diff line number Diff line change 11module Session
2+ # Container for session data with automatic expiration tracking
3+ #
4+ # Generic Constraint:
5+ # T must include the SessionData module
6+ # T must provide a parameterless constructor (T.new is called during initialization)
27 class SessionId (T )
38 include JSON ::Serializable
49
Original file line number Diff line number Diff line change 11module Session
2+ # Abstract base class for session storage backends
3+ #
4+ # Generic Constraint:
5+ # T must include the SessionData module for proper serialization and validation
6+ # T must provide a parameterless constructor (T.new)
27 abstract class Store (T )
38 include Provider
49
@@ -15,6 +20,10 @@ module Session
1520 end
1621
1722 # Module for stores that support querying sessions
23+ #
24+ # Generic Constraint:
25+ # T must include SessionData module
26+ # This module should only be included by Store(T) subclasses
1827 module QueryableStore (T )
1928 # Iterate over all sessions matching a predicate
2029 abstract def each_session (& block : SessionId (T ) - > Nil ) : Nil
Original file line number Diff line number Diff line change @@ -26,10 +26,9 @@ module Session
2626 getter redis_store : RedisStore (T )
2727 getter coordinator : ClusterCoordinator (T )
2828 getter circuit_breaker : CircuitBreaker ?
29- @ current_session : SessionId (T )?
29+ property current_session : SessionId (T ) = SessionId ( T ).new
3030
3131 def initialize (@client : Redis = Redis .new, config : ClusterConfig = ClusterConfig .new)
32- @current_session = SessionId (T ).new
3332 @redis_store = RedisStore (T ).new(@client )
3433 @coordinator = ClusterCoordinator (T ).new(@client , config)
3534 @circuit_breaker = @redis_store .circuit_breaker
@@ -39,14 +38,6 @@ module Session
3938 end
4039 end
4140
42- def current_session : SessionId (T )
43- @current_session .as(SessionId (T ))
44- end
45-
46- def current_session = (value : SessionId (T )) : SessionId (T )
47- @current_session = value
48- end
49-
5041 def storage : String
5142 " #{ self .class.name} (backed by #{ @redis_store .storage} )"
5243 end
Original file line number Diff line number Diff line change @@ -3,20 +3,10 @@ require "../store"
33module Session
44 class CookieStore (T ) < Store (T )
55 include Enumerable (HTTP ::Cookie )
6- @current_session : SessionId (T )?
7-
6+ property current_session : SessionId (T ) = SessionId (T ).new
87 property cookies
98
109 def initialize (@cookies : HTTP ::Cookies = HTTP ::Cookies .new)
11- @current_session = SessionId (T ).new
12- end
13-
14- def current_session : SessionId (T )
15- @current_session .as(SessionId (T ))
16- end
17-
18- def current_session = (value : SessionId (T )) : SessionId (T )
19- @current_session = value
2010 end
2111
2212 def each (& block : HTTP ::Cookie - > _)
Original file line number Diff line number Diff line change @@ -3,18 +3,9 @@ module Session
33 include Enumerable (SessionId (T ))
44 include QueryableStore (T )
55 getter sessions : Hash (String , SessionId (T ))
6- @ current_session : SessionId (T )?
6+ property current_session : SessionId (T ) = SessionId ( T ).new
77
88 def initialize (@sessions : Hash (String , SessionId (T )) = Hash (String , SessionId (T )).new)
9- @current_session = SessionId (T ).new
10- end
11-
12- def current_session : SessionId (T )
13- @current_session .as(SessionId (T ))
14- end
15-
16- def current_session = (value : SessionId (T )) : SessionId (T )
17- @current_session = value
189 end
1910
2011 def each (& block : SessionId (T ) - > _)
Original file line number Diff line number Diff line change @@ -4,23 +4,14 @@ module Session
44 class RedisStore (T ) < Store (T )
55 include QueryableStore (T )
66 getter circuit_breaker : CircuitBreaker ?
7- @ current_session : SessionId (T )?
7+ property current_session : SessionId (T ) = SessionId ( T ).new
88
99 def initialize (@client : Redis = Redis .new)
10- @current_session = SessionId (T ).new
1110 if Session .config.circuit_breaker_enabled
1211 @circuit_breaker = CircuitBreaker .new(Session .config.circuit_breaker_config)
1312 end
1413 end
1514
16- def current_session : SessionId (T )
17- @current_session .as(SessionId (T ))
18- end
19-
20- def current_session = (value : SessionId (T )) : SessionId (T )
21- @current_session = value
22- end
23-
2415 def storage : String
2516 self .class.name
2617 end
You can’t perform that action at this time.
0 commit comments