Skip to content

Commit cb57ba7

Browse files
committed
Refactor session handling to use properties for current_session across all store classes for consistency and clarity
1 parent 2ca8a65 commit cb57ba7

File tree

8 files changed

+29
-42
lines changed

8 files changed

+29
-42
lines changed

src/cluster.cr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff 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

src/provider.cr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
module 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

src/session_id.cr

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
module 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

src/store.cr

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
module 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

src/stores/clustered_redis_store.cr

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff 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

src/stores/cookie_store.cr

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,10 @@ require "../store"
33
module 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 -> _)

src/stores/memory_store.cr

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff 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) -> _)

src/stores/redis_store.cr

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)