1 个不稳定版本
0.1.0 | 2021年10月29日 |
---|
#2003 在 数据库接口
12KB
202 行(不包括注释)
.___.__ .__
_______ ____ __| _/|__| ______ ____ _____| |__
\_ __ \_/ __ \ / __ | | |/ ___// __ \ / ___/ | \
| | \/\ ___// /_/ | | |\___ \\ ___/ \___ \| Y \
|__| \___ >____ | |__/____ >\___ >____ >___| /
\/ \/ \/ \/ \/ \/
Redis based session manager
会话以Redis散列的形式存储。会话密钥是128位随机数(16字节),并使用base64编码。会话可以配置为过期。
基本
// Establish redis connection
let mut redisesh = Redisesh::new("redis://127.0.0.1/").unwrap();
// Data to be store in session hash map value
let session_data = Some(String::from("{username: John}"));
// Create a new session by inserting the session data in redis.
// Also generates and returns a random Token which is the key of the
// session in the redis hashmap.
let base64_token = redisesh.insert(session_data).unwrap();
过期
let mut redisesh = Redisesh::new("redis://127.0.0.1/").unwrap();
let session_data = Some(String::from("{username: John}"));
// Session Expiration date (seconds)
let expiration = std::time::Duration::from_secs(10);
// Set session expiration
redisesh.configure(Config {
expiration: Some(expiration),
});
// This session expires after 10 seconds
let base64_token = redisesh.insert(session_data).unwrap();
// Sleep for 2 seconds to make sure the session has expired
std::thread::sleep(std::time::Duration::from_secs(2));
// Check if session exists
let exists = redisesh.is_active(&base64_token).unwrap();
// Session does not exist
assert_eq!(exists, false);
⧉
依赖
~22MB
~179K SLoC