forked from XiaoMi/mone
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: docean support redis session (XiaoMi#891)
- Loading branch information
Showing
17 changed files
with
354 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>run.mone</groupId> | ||
<artifactId>docean-plugin</artifactId> | ||
<version>1.6.0-jdk21-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>docean-plugin-redisSession</artifactId> | ||
|
||
<properties> | ||
<maven.compiler.source>21</maven.compiler.source> | ||
<maven.compiler.target>21</maven.compiler.target> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>run.mone</groupId> | ||
<artifactId>docean</artifactId> | ||
<version>1.6.1-jdk21-SNAPSHOT</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>run.mone</groupId> | ||
<artifactId>docean-plugin-redis</artifactId> | ||
<version>1.6.0-jdk21-SNAPSHOT</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
46 changes: 46 additions & 0 deletions
46
...in-redisSession/src/main/java/run/mone/docean/plugin/redissession/RedisSessionPlugin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright 2020 Xiaomi | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package run.mone.docean.plugin.redissession; | ||
|
||
import com.xiaomi.youpin.docean.Ioc; | ||
import com.xiaomi.youpin.docean.anno.DOceanPlugin; | ||
import com.xiaomi.youpin.docean.plugin.IPlugin; | ||
import com.xiaomi.youpin.docean.plugin.redis.Redis; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.util.Set; | ||
|
||
/** | ||
* @author shanwb | ||
* @date 2024-09-03 | ||
*/ | ||
@DOceanPlugin(order = 101) | ||
@Slf4j | ||
public class RedisSessionPlugin implements IPlugin { | ||
|
||
|
||
@Override | ||
public void init(Set<? extends Class<?>> classSet, Ioc ioc) { | ||
Redis redis = ioc.getBean(Redis.class); | ||
if (null == redis) { | ||
log.error("redis can not be empty"); | ||
} | ||
RedisSessionStore redisSessionStore = new RedisSessionStore(redis); | ||
ioc.putBean("ClusterSessionStore", redisSessionStore); | ||
} | ||
|
||
} |
100 changes: 100 additions & 0 deletions
100
...gin-redisSession/src/main/java/run/mone/docean/plugin/redissession/RedisSessionStore.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package run.mone.docean.plugin.redissession; | ||
|
||
import com.google.gson.Gson; | ||
import com.xiaomi.youpin.docean.mvc.session.DefaultHttpSession; | ||
import com.xiaomi.youpin.docean.mvc.session.HttpSession; | ||
import com.xiaomi.youpin.docean.mvc.session.ISessionStore; | ||
import com.xiaomi.youpin.docean.plugin.redis.Redis; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* @author shanwb | ||
* @date 2024-09-03 | ||
*/ | ||
@Slf4j | ||
public class RedisSessionStore implements ISessionStore { | ||
|
||
private static final String SESSION_PREFIX = "DOCEAN_SESSION_"; | ||
|
||
private static final int SESSION_EXPIRE_SECONDS = 3600; // 默认60分钟过期 | ||
|
||
private final static Gson gson = new Gson(); | ||
|
||
private Redis redis; | ||
|
||
public RedisSessionStore(Redis redis) { | ||
this.redis = redis; | ||
} | ||
|
||
@Override | ||
public void put(String sessionId, HttpSession session) { | ||
try { | ||
String key = SESSION_PREFIX + sessionId; | ||
String value = gson.toJson(session); | ||
redis.set(key, value, SESSION_EXPIRE_SECONDS); | ||
} catch (Exception e) { | ||
log.error("Error putting session to redis", e); | ||
} | ||
} | ||
|
||
@Override | ||
public void remove(String sessionId) { | ||
try { | ||
String key = SESSION_PREFIX + sessionId; | ||
redis.del(key); | ||
} catch (Exception e) { | ||
log.error("Error removing session from redis", e); | ||
} | ||
} | ||
|
||
@Override | ||
public List<String> sessionIdList() { | ||
return new ArrayList<>(); | ||
} | ||
|
||
@Override | ||
public HttpSession get(String sessionId) { | ||
try { | ||
String key = SESSION_PREFIX + sessionId; | ||
String value = redis.get(key); | ||
if (value == null) { | ||
return null; | ||
} | ||
return gson.fromJson(value, DefaultHttpSession.class); | ||
} catch (Exception e) { | ||
log.error("Error getting session from redis", e); | ||
return null; | ||
} | ||
} | ||
|
||
@Override | ||
public HttpSession getAndRefresh(String sessionId) { | ||
try { | ||
String key = SESSION_PREFIX + sessionId; | ||
String value = redis.get(key); | ||
if (value == null) { | ||
return null; | ||
} | ||
DefaultHttpSession session = gson.fromJson(value, DefaultHttpSession.class); | ||
redis.expire(key, SESSION_EXPIRE_SECONDS); | ||
return session; | ||
} catch (Exception e) { | ||
log.error("Error getting session from redis", e); | ||
return null; | ||
} | ||
} | ||
|
||
@Override | ||
public boolean containsKey(String sessionId) { | ||
try { | ||
String key = SESSION_PREFIX + sessionId; | ||
return redis.exists(key); | ||
} catch (Exception e) { | ||
log.error("Error checking session existence in redis", e); | ||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.