Skip to content
This repository was archived by the owner on Jan 23, 2025. It is now read-only.

fix SortedCacheAddress to make work with JbossCacheClient #378

Merged
merged 2 commits into from
Mar 23, 2018
Merged
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 @@ -107,7 +107,7 @@ public boolean evaluate(Object o) {
private List<? extends Map<String,String>> getData() throws Exception {
CacheClient cc = null;
List<? extends Map<String,String>> data = null;
SortedCacheAddress cacheAddress = new SortedCacheAddress();
SortedCacheAddress cacheAddress = new SortedCacheAddress("group_member", MaxAge.FIVE_MINUTES);
cacheAddress.addAll(groupIds);
try{
cc = CacheClientFactory.create();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3895,7 +3895,8 @@ public static Set<ProjectGroup> getGroupsFromApi(TCSubject tcSubject, String end
public static Set<ProjectGroup> getGroups(TCSubject tcSubject, String endpoint) throws Exception {
CacheClient cc = null;
Set<ProjectGroup> projectGroups = null;
SortedCacheAddress cacheAddress = new SortedCacheAddress(tcSubject.getUserId());
SortedCacheAddress cacheAddress = new SortedCacheAddress("user_group", MaxAge.FIVE_MINUTES);
cacheAddress.add(tcSubject.getUserId());
try {
cc = CacheClientFactory.create();
projectGroups = (Set<ProjectGroup>) cc.get(cacheAddress);
Expand All @@ -3905,7 +3906,7 @@ public static Set<ProjectGroup> getGroups(TCSubject tcSubject, String endpoint)
if (projectGroups == null) {
projectGroups = DirectUtils.getGroupsFromApi(tcSubject, endpoint);
try {
cc.set(cacheAddress, projectGroups, MaxAge.HOUR);
cc.set(cacheAddress, projectGroups, MaxAge.FIVE_MINUTES);
} catch (Exception e) {
logger.error("Failed to put user group into cache ", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
*/
package com.topcoder.direct.services.view.util;

import com.topcoder.web.common.cache.address.CacheAddress;
import com.topcoder.web.common.cache.MaxAge;
import com.topcoder.web.common.cache.address.jboss.JbossCacheAddress;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
Expand All @@ -16,16 +16,18 @@
*
* @version 1.0
*/
public class SortedCacheAddress implements CacheAddress {
public class SortedCacheAddress implements JbossCacheAddress {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@deedee this is incorrect, we should not tied to specific, like JBossCacheAddress, which seems specific to jboss cache, if we use Redis as the cache, will that cause problem?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JbossCacheAddress extend CacheAddress. Implementing JbossCacheAddress should work on both client. I think this approach make task more simple instead creating different classes for each clients and use AddressFactory to get it.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@deedee ok

/**
* Prefix key
*/
private Long prefix;
private String prefix;

private MaxAge maxAge;

/**
* Default prefix
*/
private static final Long DEFAULT_PREFIX = 0L;
private static final String DEFAULT_PREFIX = "direct";

/**
* List of items for key
Expand All @@ -36,17 +38,22 @@ public SortedCacheAddress() {
this(DEFAULT_PREFIX);
}

public SortedCacheAddress(Long prefix) {
this.prefix = prefix;
public SortedCacheAddress(String prefix) {
this(prefix, MaxAge.HOUR);
}

public SortedCacheAddress(String prefix, MaxAge maxAge) {
this.prefix = prefix;
this.maxAge = maxAge;
}
/**
* Add item key
*
* @param item item to add
*/
public void add(Long item) {
items.add(item);
Collections.sort(items);
}

/**
Expand All @@ -56,6 +63,7 @@ public void add(Long item) {
*/
public void addAll(List<Long> added) {
items.addAll(added);
Collections.sort(items);
}

/**
Expand All @@ -74,14 +82,7 @@ public void remove (Long item) {
*/
@Override
public String getKey() {
StringBuffer keyBuffer = new StringBuffer(String.valueOf(prefix));
//sort it, so we'll get same key for same content
Collections.sort(items, new Comparator<Long>() {
@Override
public int compare(Long o1, Long o2) {
return o1.compareTo(o1);
}
});
StringBuffer keyBuffer = new StringBuffer(prefix);

for (Long item : items) {
keyBuffer.append("-");
Expand All @@ -90,4 +91,8 @@ public int compare(Long o1, Long o2) {
return keyBuffer.toString();
}

@Override
public String getFqn() {
return new StringBuffer("/").append(maxAge.name()).append("/").append(getKey()).toString();
}
}