-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathkemal-session-redis.cr
236 lines (198 loc) · 6.23 KB
/
kemal-session-redis.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
require "uri"
require "json"
require "redis"
require "pool/connection"
require "kemal-session"
class Session
class RedisEngine < Engine
module StorableObjectConverter
def self.from_json(pull : JSON::PullParser) : Hash(String, StorableObject)
hash = Hash(String, StorableObject).new
pull.read_object do |key|
if pull.kind == :null
pull.read_next
else
hash[key] = StorableObject.unserialize(pull.read_string)
end
end
hash
end
def self.to_json(value : Hash(String, StorableObject), io : IO)
if value.empty?
io << "{}"
return
end
io.json_object do |json_obj|
value.each do |object_name, storable_obj|
json_obj.field object_name, storable_obj.serialize
end
end
end
end
class StorageInstance
macro define_storage(vars)
JSON.mapping({
{% for name, type in vars %}
{% if name != "object" %}
{{name.id}}s: Hash(String, {{type}}),
{% end %}
{% end %}
objects: {type: Hash(String, StorableObject), converter: StorableObjectConverter},
})
{% for name, type in vars %}
@{{name.id}}s = Hash(String, {{type}}).new
getter {{name.id}}s
def {{name.id}}(k : String) : {{type}}
return @{{name.id}}s[k]
end
def {{name.id}}?(k : String) : {{type}}?
return @{{name.id}}s[k]?
end
def {{name.id}}(k : String, v : {{type}})
@{{name.id}}s[k] = v
end
{% end %}
def initialize
{% for name, type in vars %}
@{{name.id}}s = Hash(String, {{type}}).new
{% end %}
end
end
define_storage({int: Int32, string: String, float: Float64, bool: Bool, object: StorableObject})
end
@redis : ConnectionPool(Redis)
@cache : StorageInstance
@cached_session_id : String
def initialize(host = "localhost", port = 6379, password = nil, database = 0, capacity = 20, timeout = 2.0, unixsocket = nil, pool = nil, key_prefix = "kemal:session:")
@redis = uninitialized ConnectionPool(Redis)
if pool.nil?
@redis = ConnectionPool.new(capacity: capacity, timeout: timeout) do
Redis.new(
host: host,
port: port,
database: database,
unixsocket: unixsocket,
password: password
)
end
else
@redis = pool.as(ConnectionPool(Redis))
end
@cache = StorageInstance.new
@key_prefix = key_prefix
@cached_session_id = ""
end
def run_gc
# Do Nothing. All the sessions should be set with the
# expiration option on the keys. So long as the redis instance
# hasn't been set up with maxmemory policy of noeviction
# then this should be fine. `noeviction` will cause the redis
# instance to fill up and keys will not expire from the instance
end
def prefix_session(session_id : String)
"#{@key_prefix}#{session_id}"
end
def parse_session_id(key : String)
key.sub(@key_prefix, "")
end
def load_into_cache(session_id)
@cached_session_id = session_id
conn = @redis.checkout
value = conn.get(prefix_session(session_id))
if !value.nil?
@cache = StorageInstance.from_json(value)
else
@cache = StorageInstance.new
conn.set(
prefix_session(session_id),
@cache.to_json,
ex: Session.config.timeout.total_seconds.to_i
)
end
@redis.checkin(conn)
return @cache
end
def save_cache
conn = @redis.checkout
conn.set(
prefix_session(@cached_session_id),
@cache.to_json,
ex: Session.config.timeout.total_seconds.to_i
)
@redis.checkin(conn)
end
def is_in_cache?(session_id)
return session_id == @cached_session_id
end
def create_session(session_id : String)
load_into_cache(session_id)
end
def get_session(session_id : String)
conn = @redis.checkout
value = conn.get(prefix_session(session_id))
@redis.checkin(conn)
return Session.new(session_id) if value
nil
end
def destroy_session(session_id : String)
conn = @redis.checkout
conn.del(prefix_session(session_id))
@redis.checkin(conn)
end
def destroy_all_sessions
conn = @redis.checkout
cursor = 0
loop do
cursor, keys = conn.scan(cursor, "#{@key_prefix}*")
keys = keys.as(Array(Redis::RedisValue)).map(&.to_s)
keys.each do |key|
conn.del(key)
end
break if cursor == "0"
end
@redis.checkin(conn)
end
def all_sessions
arr = [] of Session
each_session do |session|
arr << session
end
return arr
end
def each_session
conn = @redis.checkout
cursor = 0
loop do
cursor, keys = conn.scan(cursor, "#{@key_prefix}*")
keys = keys.as(Array(Redis::RedisValue)).map(&.to_s)
keys.each do |key|
yield Session.new(parse_session_id(key.as(String)))
end
break if cursor == "0"
end
@redis.checkin(conn)
end
macro define_delegators(vars)
{% for name, type in vars %}
def {{name.id}}(session_id : String, k : String) : {{type}}
load_into_cache(session_id) unless is_in_cache?(session_id)
return @cache.{{name.id}}(k)
end
def {{name.id}}?(session_id : String, k : String) : {{type}}?
load_into_cache(session_id) unless is_in_cache?(session_id)
return @cache.{{name.id}}?(k)
end
def {{name.id}}(session_id : String, k : String, v : {{type}})
load_into_cache(session_id) unless is_in_cache?(session_id)
@cache.{{name.id}}(k, v)
save_cache
end
def {{name.id}}s(session_id : String) : Hash(String, {{type}})
load_into_cache(session_id) unless is_in_cache?(session_id)
return @cache.{{name.id}}s
end
{% end %}
end
define_delegators({int: Int32, string: String, float: Float64, bool: Bool, object: StorableObject})
end
end