Skip to content

修复redisSession过期问题 #87

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,21 @@ public void readObjectData(java.io.ObjectInputStream in) throws IOException, Cla
this.setCreationTime(in.readLong());
}

@Override
public void access() {
if (getMaxInactiveInterval() > 0) {
Jedis jedis = null;
try {
jedis = ((RedisSessionManager) manager).acquireConnection();
jedis.expire(getId().getBytes(), getMaxInactiveInterval());
} catch (IOException e) {
log.error(e.getMessage());
} finally {
if (jedis != null) {
returnConnection(jedis, error);
}
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ static SessionPersistPolicy fromName(String name) {

private final Log log = LogFactory.getLog(RedisSessionManager.class);

protected String host = "localhost";
protected int port = 6379;
protected int database = 0;
protected String host = Protocol.DEFAULT_HOST;
protected int port = Protocol.DEFAULT_PORT;
protected int database = Protocol.DEFAULT_DATABASE;
protected String password = null;
protected int timeout = Protocol.DEFAULT_TIMEOUT;
protected String sentinelMaster = null;
Expand All @@ -78,6 +78,8 @@ static SessionPersistPolicy fromName(String name) {

protected EnumSet<SessionPersistPolicy> sessionPersistPoliciesSet = EnumSet.of(SessionPersistPolicy.DEFAULT);

protected int maxActiveSessions = -1;
protected int rejectedSessions = 0;
/**
* The lifecycle event support for this component.
*/
Expand Down Expand Up @@ -189,14 +191,23 @@ public void setSentinelMaster(String master) {
this.sentinelMaster = master;
}

public int getMaxActiveSessions() {
return this.maxActiveSessions;
}

public void setMaxActiveSessions(int max) {
int oldMaxActiveSessions = this.maxActiveSessions;
this.maxActiveSessions = max;
this.support.firePropertyChange("maxActiveSessions", new Integer(oldMaxActiveSessions), new Integer(this.maxActiveSessions));
}

@Override
public int getRejectedSessions() {
// Essentially do nothing.
return 0;
return rejectedSessions;
}

public void setRejectedSessions(int i) {
// Do nothing.
public void setRejectedSessions(int rejectedSessions) {
this.rejectedSessions = rejectedSessions;
}

protected Jedis acquireConnection() {
Expand Down Expand Up @@ -333,6 +344,11 @@ protected synchronized void stopInternal() throws LifecycleException {

@Override
public Session createSession(String requestedSessionId) {
if (maxActiveSessions >= 0 && getSize() >= maxActiveSessions) {
++rejectedSessions;
throw new IllegalStateException(sm.getString("standardManager.createSession.ise"));
}

RedisSession session = null;
String sessionId = null;
String jvmRoute = getJvmRoute();
Expand Down Expand Up @@ -450,6 +466,23 @@ public Session findSession(String id) throws IOException {
return session;
}

@Override
public Session[] findSessions() {
try {
String[] keys = keys();
Session[] sessions = new Session[keys.length];
int i = 0;
for(String key : keys) {
byte[] data = loadSessionDataFromRedis(key);
DeserializedSessionContainer container = sessionFromSerializedData(key, data);
sessions[i++] = container.session;
}
return sessions;
} catch (IOException e) {
log.error("Error find sessions", e);
}
}

public void clear() {
Jedis jedis = null;
Boolean error = true;
Expand Down