@@ -30,6 +30,10 @@ Sample app notes:
3030- ` sample/session.ts ` uses ` MemorySessionStore `
3131- ` sample/session_cookie.ts ` shows the cookie store pattern
3232- ` sample/session_kv.ts ` shows the Deno KV store pattern
33+ - ` sample/session_redis.ts ` shows the Redis store pattern
34+
35+ Redis sample notes:
36+ - Uses ` REDIS_HOST ` and ` REDIS_PORT ` (defaults: ` 127.0.0.1:6379 ` )
3337
3438### 1. Create a session middleware
3539
@@ -138,23 +142,19 @@ For distributed environments with Redis.
138142
139143``` ts ignore
140144import { type RedisClient , RedisSessionStore } from " ./mod.ts" ;
141- import { connect } from " npm:ioredis " ;
145+ import { connect } from " jsr:@db/redis " ;
142146
143- const redis = new Redis ({
144- host : " localhost " ,
147+ const redis = await connect ({
148+ hostname : " 127.0.0.1 " ,
145149 port: 6379 ,
146150});
147151
148152// Adapt to RedisClient interface
149153const client: RedisClient = {
150154 get : (key ) => redis .get (key ),
151- set : (key , value , options ) => {
152- if (options ?.ex ) {
153- return redis .set (key , value , " EX" , options .ex ).then (() => {});
154- }
155- return redis .set (key , value ).then (() => {});
156- },
157- del : (key ) => redis .del (key ).then (() => {}),
155+ set : (key , value , options ) =>
156+ redis .set (key , value , options ?.ex ? { ex: options .ex } : undefined ),
157+ del : (key ) => redis .del (key ),
158158};
159159
160160const store = new RedisSessionStore ({ client , keyPrefix: " session:" });
@@ -169,9 +169,7 @@ For applications using relational databases.
169169CREATE TABLE sessions (
170170 session_id VARCHAR (36 ) PRIMARY KEY ,
171171 data TEXT NOT NULL ,
172- expires_at DATETIME NULL ,
173- created_at DATETIME DEFAULT CURRENT_TIMESTAMP ,
174- updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
172+ expires_at DATETIME NULL
175173);
176174CREATE INDEX idx_sessions_expires_at ON sessions(expires_at);
177175```
0 commit comments