-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #143 from restx/review-restx-session
Allow to plug other cache mechanisms for RestxSession #141 [breaking] [breaking] RestxSession.Definition.Entry is now an interface. Use DefaultSessionDefinitionEntry to get similar behavior if you were instanciating RestxSession.Definition.Entry before. [breaking] RestxSession#cleanUpCaches has been removed It was misleading, was not invalidating the cache and very implementation dependent. 2 invalidate methods have been added to replace it.
- Loading branch information
Showing
6 changed files
with
258 additions
and
68 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
restx-core/src/main/java/restx/security/DefaultSessionDefinitionEntry.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,29 @@ | ||
package restx.security; | ||
|
||
import com.google.common.base.Function; | ||
import com.google.common.base.Optional; | ||
import restx.security.RestxSession.Definition.Entry; | ||
|
||
/** | ||
* Date: 20/1/15 | ||
* Time: 20:56 | ||
*/ | ||
public class DefaultSessionDefinitionEntry<T> implements Entry<T> { | ||
private final String sessionDefKey; | ||
private final Function<String, Optional<? extends T>> function; | ||
|
||
public DefaultSessionDefinitionEntry(Class<T> clazz, String sessionDefKey, Function<String, Optional<? extends T>> function) { | ||
this.sessionDefKey = sessionDefKey; | ||
this.function = function; | ||
} | ||
|
||
@Override | ||
public String getKey() { | ||
return sessionDefKey; | ||
} | ||
|
||
@Override | ||
public Optional<? extends T> getValueForId(String valueId) { | ||
return function.apply(valueId); | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
restx-core/src/main/java/restx/security/GuavaEntryCacheManager.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,87 @@ | ||
package restx.security; | ||
|
||
import com.google.common.base.Optional; | ||
import com.google.common.cache.CacheBuilder; | ||
import com.google.common.cache.CacheLoader; | ||
import com.google.common.cache.LoadingCache; | ||
import restx.security.RestxSession.Definition.CachedEntry; | ||
import restx.security.RestxSession.Definition.Entry; | ||
import restx.security.RestxSession.Definition.EntryCacheManager; | ||
|
||
import java.util.concurrent.ExecutionException; | ||
|
||
/** | ||
* A restx session entry cache manager based on guava cache. | ||
* | ||
* You can override the cache settings by overriding the getCacheBuilder() method. | ||
* | ||
* Note that Guava Cache is not distributed, so be very careful with cache invalidation | ||
* when using this cache. | ||
* | ||
* This is the default EntryCacheManager, see SecurityModule which provides one. | ||
*/ | ||
public class GuavaEntryCacheManager implements EntryCacheManager { | ||
@Override | ||
public <T> CachedEntry<T> getCachedEntry(Entry<T> entry) { | ||
return new GuavaCacheSessionDefinitionEntry<T>(entry.getKey(), getLoadingCacheFor(entry)); | ||
} | ||
|
||
protected <T> LoadingCache<String, T> getLoadingCacheFor(final Entry<T> entry) { | ||
return getCacheBuilder(entry).build(getCacheLoaderFor(entry)); | ||
} | ||
|
||
protected <T> CacheLoader<String, T> getCacheLoaderFor(final Entry<T> entry) { | ||
return new CacheLoader<String, T>() { | ||
@Override | ||
public T load(String key) throws Exception { | ||
return entry.getValueForId(key).orNull(); | ||
} | ||
}; | ||
} | ||
|
||
protected <T> CacheBuilder<Object, Object> getCacheBuilder(Entry<T> entry) { | ||
return CacheBuilder.newBuilder().maximumSize(1000); | ||
} | ||
|
||
/** | ||
* A session definition entry implementation using Guava Cache. | ||
*/ | ||
public static class GuavaCacheSessionDefinitionEntry<T> implements CachedEntry<T> { | ||
private final LoadingCache<String, T> loadingCache; | ||
private final String key; | ||
|
||
public GuavaCacheSessionDefinitionEntry(String key, LoadingCache<String, T> loadingCache) { | ||
this.key = key; | ||
this.loadingCache = loadingCache; | ||
} | ||
|
||
@Override | ||
public String getKey() { | ||
return key; | ||
} | ||
|
||
@Override | ||
public Optional<T> getValueForId(String valueId) { | ||
try { | ||
return Optional.fromNullable(loadingCache.get(valueId)); | ||
} catch (CacheLoader.InvalidCacheLoadException e) { | ||
// this exception is raised when cache loader returns null, which may happen if the object behind the key | ||
// is deleted. Therefore we just return an absent value | ||
return Optional.absent(); | ||
} catch (ExecutionException e) { | ||
throw new RuntimeException( | ||
"impossible to load object from cache using valueid " + valueId + " for " + key + ": " + e.getMessage(), e); | ||
} | ||
} | ||
|
||
@Override | ||
public void invalidateCacheFor(String valueId) { | ||
loadingCache.invalidate(valueId); | ||
} | ||
|
||
@Override | ||
public void invalidateCache() { | ||
loadingCache.invalidateAll(); | ||
} | ||
} | ||
} |
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.