Skip to content

Commit

Permalink
Updated DefaultPolicyInformationPointCacheProvider
Browse files Browse the repository at this point in the history
- Moved common functionality to BasePolicyInformationPointCacheProvider
- Reduced default cache sizes for content and attributes
  • Loading branch information
valdas-s committed Jul 31, 2014
1 parent 521308a commit 7f89e1c
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,60 +22,46 @@
* #L%
*/

import java.util.List;

import org.xacml4j.v30.BagOfAttributeExp;

import com.google.common.base.Preconditions;

public class BasePolicyInformationPointCacheProvider implements PolicyInformationPointCacheProvider
{
public abstract class BasePolicyInformationPointCacheProvider implements PolicyInformationPointCacheProvider {

@Override
public final Content getContent(ResolverContext context) {
ContentResolverDescriptor d = (ContentResolverDescriptor)context.getDescriptor();
return d.isCacheable() ? doGetContent(d, context.getKeys()) : null;
return context.getDescriptor().isCacheable() ? doGetContent(context) : null;
}

@Override
public final void putContent(ResolverContext context, Content content) {
ContentResolverDescriptor d = (ContentResolverDescriptor)context.getDescriptor();
Preconditions.checkArgument(context.getDescriptor() == content.getDescriptor());
if(d.isCacheable()){
doPutContent(d, context.getKeys(), content);
Preconditions.checkArgument(context.getDescriptor().getId().equals(content.getDescriptor().getId()),
"Content descriptor Id \"%s\" must match resolver context descriptor Id \"%s\"",
content.getDescriptor().getId(), context.getDescriptor().getId());
ContentResolverDescriptor d = (ContentResolverDescriptor) context.getDescriptor();
if (d.isCacheable()) {
doPutContent(context, content);
}
}

@Override
public final AttributeSet getAttributes(ResolverContext context) {
AttributeResolverDescriptor d = (AttributeResolverDescriptor)context.getDescriptor();
return d.isCacheable()?doGetAttributes(d, context.getKeys()):null;
return context.getDescriptor().isCacheable() ? doGetAttributes(context) : null;
}

@Override
public final void putAttributes(ResolverContext context, AttributeSet v) {
AttributeResolverDescriptor d = (AttributeResolverDescriptor)context.getDescriptor();
Preconditions.checkArgument(d.getId().equals(v.getDescriptor().getId()));
if(d.isCacheable()){
doPutAttributes(d, context.getKeys(), v);
Preconditions.checkArgument(context.getDescriptor().getId().equals(v.getDescriptor().getId()),
"Attribute set descriptor Id \"%s\" must match resolver context descriptor Id \"%s\"",
v.getDescriptor().getId(), context.getDescriptor().getId());
if (context.getDescriptor().isCacheable()) {
doPutAttributes(context, v);
}
}

protected Content doGetContent(ContentResolverDescriptor d, List<BagOfAttributeExp> keys){
return null;
}
protected abstract Content doGetContent(ResolverContext context);

protected AttributeSet doGetAttributes(AttributeResolverDescriptor d,
List<BagOfAttributeExp> keys) {
return null;
}
protected abstract AttributeSet doGetAttributes(ResolverContext context);

protected void doPutContent(ContentResolverDescriptor d, List<BagOfAttributeExp> keys,
Content content) {
}

protected void doPutAttributes(AttributeResolverDescriptor d, List<BagOfAttributeExp> keys,
AttributeSet v) {
protected abstract void doPutContent(ResolverContext context, Content content);

}
protected abstract void doPutAttributes(ResolverContext context, AttributeSet v);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,31 @@
* #L%
*/

import com.google.common.base.Preconditions;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;

public final class DefaultPolicyInformationPointCacheProvider
implements PolicyInformationPointCacheProvider
{
extends BasePolicyInformationPointCacheProvider {
private final Cache<ResolverCacheKey, AttributeSet> attributeCache;
private final Cache<ResolverCacheKey, Content> contentCache;

public DefaultPolicyInformationPointCacheProvider(){
this(Integer.MAX_VALUE/2, Integer.MAX_VALUE/2);
/**
* Default maximum number of items in the attribute cache
*/
private static final int DEFAULT_MAX_ATTRIBUTE_ITEMS = 2048;

/**
* Default maximum number of items in the content cache
*/
private static final int DEFAULT_MAX_CONTENT_ITEMS = 2048;

public DefaultPolicyInformationPointCacheProvider() {
this(DEFAULT_MAX_ATTRIBUTE_ITEMS, DEFAULT_MAX_CONTENT_ITEMS);
}

public DefaultPolicyInformationPointCacheProvider(
int maxAttrSize,
int maxContentSize){
int maxContentSize) {
this.attributeCache = CacheBuilder
.newBuilder()
.maximumSize(maxAttrSize)
Expand All @@ -50,76 +58,58 @@ public DefaultPolicyInformationPointCacheProvider(
}

@Override
public Content getContent(ResolverContext context) {
ContentResolverDescriptor d = (ContentResolverDescriptor)context.getDescriptor();
if(d.isCacheable()){
ResolverCacheKey key = ResolverCacheKey
.builder()
.id(d)
.keys(context.getKeys())
.build();
Content v = contentCache.getIfPresent(key);
if (v != null && isExpired(v, context)) {
attributeCache.invalidate(key);
}
return v;
protected Content doGetContent(ResolverContext context) {
ResolverCacheKey key = ResolverCacheKey
.builder()
.id(context.getDescriptor())
.keys(context.getKeys())
.build();
Content v = contentCache.getIfPresent(key);
if (v != null && isExpired(v, context)) {
attributeCache.invalidate(key);
}
return null;
return v;
}

@Override
public void putContent(ResolverContext context, Content content) {
ContentResolverDescriptor d = (ContentResolverDescriptor)context.getDescriptor();
Preconditions.checkArgument(context.getDescriptor() == content.getDescriptor());
if(d.isCacheable()){
contentCache.put(ResolverCacheKey
.builder()
.id(d)
.keys(context.getKeys())
.build(), content);
}
protected void doPutContent(ResolverContext context, Content content) {
contentCache.put(ResolverCacheKey
.builder()
.id(context.getDescriptor())
.keys(context.getKeys())
.build(), content);
}

@Override
public AttributeSet getAttributes(ResolverContext context) {
AttributeResolverDescriptor d = (AttributeResolverDescriptor)context.getDescriptor();
if(d.isCacheable()){
ResolverCacheKey key =
ResolverCacheKey
.builder()
.id(d)
.keys(context.getKeys())
.build();
AttributeSet v = attributeCache.getIfPresent(key);
if(v!= null && isExpired(v, context)){
attributeCache.invalidate(key);
}
return v;
protected AttributeSet doGetAttributes(ResolverContext context) {
ResolverCacheKey key = ResolverCacheKey
.builder()
.id(context.getDescriptor())
.keys(context.getKeys())
.build();
AttributeSet v = attributeCache.getIfPresent(key);
if (v != null && isExpired(v, context)) {
attributeCache.invalidate(key);
}
return null;
return v;
}

@Override
public final void putAttributes(ResolverContext context, AttributeSet v) {
AttributeResolverDescriptor d = (AttributeResolverDescriptor)context.getDescriptor();
Preconditions.checkArgument(d.getId().equals(v.getDescriptor().getId()));
if(d.isCacheable()){
ResolverCacheKey key =
ResolverCacheKey
.builder()
.id(d)
.keys(context.getKeys())
.build();
attributeCache.put(key, v);
}
protected void doPutAttributes(ResolverContext context, AttributeSet v) {
ResolverCacheKey key = ResolverCacheKey
.builder()
.id(context.getDescriptor())
.keys(context.getKeys())
.build();
attributeCache.put(key, v);
}

private boolean isExpired(AttributeSet v, ResolverContext context){
private boolean isExpired(AttributeSet v, ResolverContext context) {
return ((context.getTicker().read() - v.getCreatedTime()) /
1000000000L) >= v.getDescriptor().getPreferredCacheTTL();
}

private boolean isExpired(Content v, ResolverContext context){
private boolean isExpired(Content v, ResolverContext context) {
return ((context.getTicker().read() - v.getTimestamp()) /
1000000000L) >= v.getDescriptor().getPreferredCacheTTL();
}
Expand Down

0 comments on commit 7f89e1c

Please sign in to comment.