[
https://issues.apache.org/jira/browse/SHIRO-317?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13606667#comment-13606667
]
Benjamin Marwell edited comment on SHIRO-317 at 2/1/21, 10:33 AM:
------------------------------------------------------------------
{code}
package com.hdpoker.account.security.shiro.cache;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheException;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ThreadContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.Serializable;
import java.util.Collection;
import java.util.Set;
/**
* User: Mark Spritzler
*
* First Serializable is the Key in the cache
* Second Serializable is the Session object.
*
* In regards to ThreadContext. We are putting in the Session object into
ThreadLocal. So the key is a constant
* for the key in ThreadLocal for the user's Session object.
*
*/
public class ThreadLocalDelegatingCache implements Cache<Serializable,
Serializable> {
Cache<Serializable, Serializable> wrappedCache;
public static final String SESSION_KEY = "sessionCached";
public void setWrappedCache(Cache wrappedCache) {
this.wrappedCache = wrappedCache;
}
@Override
public void clear() throws CacheException {
Serializable session = (Serializable)ThreadContext.get(SESSION_KEY);
if (session != null) {
ThreadContext.remove(SESSION_KEY);
}
wrappedCache.clear();
}
// Parameter is the SessionId to find in the cache object.
@Override
public Serializable get(Serializable o) throws CacheException {
Serializable session = (Serializable)ThreadContext.get(SESSION_KEY);
if (session != null) {
return session;
} else {
session = wrappedCache.get(o);
ThreadContext.put(SESSION_KEY, session);
return session;
}
}
// Parameters are key and value to put into the Session
@Override
public Serializable put(Serializable o, Serializable o2) throws
CacheException {
Serializable returnValue = wrappedCache.put(o, o2);
ThreadContext.put(SESSION_KEY, o2);
return returnValue;
}
@Override
public Serializable remove(Serializable o) throws CacheException {
Serializable returnValue;
if (o instanceof PrincipalCollection) {
String key = (String)((PrincipalCollection)o).getPrimaryPrincipal();
returnValue = wrappedCache.remove(key);
} else {
returnValue = wrappedCache.remove(o);
}
ThreadContext.remove(SESSION_KEY);
return returnValue;
}
@Override
public int size() {
return wrappedCache.size();
}
@Override
public Set keys() {
return wrappedCache.keys();
}
@Override
public Collection values() {
return wrappedCache.values();
}
}
{code}
was (Author: bytor99999):
package com.hdpoker.account.security.shiro.cache;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheException;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ThreadContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.Serializable;
import java.util.Collection;
import java.util.Set;
/**
* User: Mark Spritzler
*
* First Serializable is the Key in the cache
* Second Serializable is the Session object.
*
* In regards to ThreadContext. We are putting in the Session object into
ThreadLocal. So the key is a constant
* for the key in ThreadLocal for the user's Session object.
*
*/
public class ThreadLocalDelegatingCache implements Cache<Serializable,
Serializable> {
Cache<Serializable, Serializable> wrappedCache;
public static final String SESSION_KEY = "sessionCached";
public void setWrappedCache(Cache wrappedCache) {
this.wrappedCache = wrappedCache;
}
@Override
public void clear() throws CacheException {
Serializable session = (Serializable)ThreadContext.get(SESSION_KEY);
if (session != null) {
ThreadContext.remove(SESSION_KEY);
}
wrappedCache.clear();
}
// Parameter is the SessionId to find in the cache object.
@Override
public Serializable get(Serializable o) throws CacheException {
Serializable session = (Serializable)ThreadContext.get(SESSION_KEY);
if (session != null) {
return session;
} else {
session = wrappedCache.get(o);
ThreadContext.put(SESSION_KEY, session);
return session;
}
}
// Parameters are key and value to put into the Session
@Override
public Serializable put(Serializable o, Serializable o2) throws
CacheException {
Serializable returnValue = wrappedCache.put(o, o2);
ThreadContext.put(SESSION_KEY, o2);
return returnValue;
}
@Override
public Serializable remove(Serializable o) throws CacheException {
Serializable returnValue;
if (o instanceof PrincipalCollection) {
String key = (String)((PrincipalCollection)o).getPrimaryPrincipal();
returnValue = wrappedCache.remove(key);
} else {
returnValue = wrappedCache.remove(o);
}
ThreadContext.remove(SESSION_KEY);
return returnValue;
}
@Override
public int size() {
return wrappedCache.size();
}
@Override
public Set keys() {
return wrappedCache.keys();
}
@Override
public Collection values() {
return wrappedCache.values();
}
}
> Read session from cache once per request
> ----------------------------------------
>
> Key: SHIRO-317
> URL: https://issues.apache.org/jira/browse/SHIRO-317
> Project: Shiro
> Issue Type: New Feature
> Affects Versions: 1.1.0, 1.2.0, 1.2.1
> Reporter: Luke Biddell
> Assignee: Les Hazlewood
> Priority: Minor
>
> As per our discussion on the mailing thread, I've wired up my sessions to be
> stored in memcached (membase in the longer term). On a per request basis I'm
> seeing approximately 5 hits on my cache to retrieve the session. I would
> expect to see only one hit per threaded request, with the session stored as a
> thread local.
> For distributed caches this saves on network calls and for local caches it
> will save on potential lock contention.
--
This message was sent by Atlassian Jira
(v8.3.4#803005)