Added cache that keeps hashtags for scope with a TTL of 5 minutes
git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/portlets/user/top-topics@128785 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
9b7db56867
commit
bf90c6723f
|
@ -21,6 +21,7 @@ import org.gcube.portal.databook.server.DBCassandraAstyanaxImpl;
|
|||
import org.gcube.portal.databook.server.DatabookStore;
|
||||
import org.gcube.portal.databook.shared.Feed;
|
||||
import org.gcube.portlets.user.topics.client.TopicService;
|
||||
import org.gcube.portlets.user.topics.server.cache.TopicsCacheImpl;
|
||||
import org.gcube.portlets.user.topics.shared.HashTagAndOccurrence;
|
||||
import org.gcube.portlets.user.topics.shared.HashtagsWrapper;
|
||||
import org.gcube.vomanagement.usermanagement.GroupManager;
|
||||
|
@ -87,11 +88,32 @@ public class TopicServiceImpl extends RemoteServiceServlet implements TopicServi
|
|||
*/
|
||||
@Override
|
||||
public HashtagsWrapper getHashtags() {
|
||||
ArrayList<String> hashtagsChart = new ArrayList<>();
|
||||
ArrayList<String> hashtagsChart;
|
||||
ASLSession session = getASLSession();
|
||||
|
||||
String userName = session.getUsername();
|
||||
boolean isInfrastructure = isInfrastructureScope();
|
||||
String currentScope = session.getScope();
|
||||
|
||||
long timestampStart = System.currentTimeMillis();
|
||||
|
||||
// get cache reference
|
||||
TopicsCacheImpl topicsCache = TopicsCacheImpl.getCacheInstance();
|
||||
|
||||
hashtagsChart = topicsCache.get(currentScope);
|
||||
|
||||
if(hashtagsChart != null){
|
||||
|
||||
_log.debug("TopTopics for scope " + currentScope + " retrieved from the cache");
|
||||
|
||||
}
|
||||
else{
|
||||
|
||||
_log.debug("TopTopics for scope " + currentScope + " are not in the cache, evaluating them...");
|
||||
|
||||
// create array
|
||||
hashtagsChart = new ArrayList<String>();
|
||||
|
||||
// get the reference time
|
||||
Calendar referenceTime = Calendar.getInstance();
|
||||
int currentMonth = referenceTime.get(Calendar.MONTH); // jan = 0, ..... dec = 11
|
||||
|
@ -101,9 +123,6 @@ public class TopicServiceImpl extends RemoteServiceServlet implements TopicServi
|
|||
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
|
||||
_log.debug("Reference time for trending topics is " + format.format(referenceTime.getTime()));
|
||||
|
||||
String userName = session.getUsername();
|
||||
boolean isInfrastructure = isInfrastructureScope();
|
||||
|
||||
try {
|
||||
//in case the portal is restarted and you have the social home open it will get test.user (no callback to set session info)
|
||||
//this check just return nothing if that happens
|
||||
|
@ -201,15 +220,18 @@ public class TopicServiceImpl extends RemoteServiceServlet implements TopicServi
|
|||
String hashtagLink = "<a class=\"topiclink\" href=" + href + ">"+hashtag+"</a>";
|
||||
hashtagsChart.add(hashtagLink);
|
||||
}
|
||||
|
||||
// update the cache
|
||||
topicsCache.insert(currentScope, hashtagsChart);
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
long timestampEnd = System.currentTimeMillis() - timestampStart;
|
||||
_log.debug("Overall time to retrieve hastags is " + timestampEnd);
|
||||
_log.debug("Overall time to retrieve hastags is " + timestampEnd + "ms");
|
||||
|
||||
return new HashtagsWrapper(isInfrastructure, hashtagsChart);
|
||||
}
|
||||
|
|
26
src/main/java/org/gcube/portlets/user/topics/server/cache/CacheInterface.java
vendored
Normal file
26
src/main/java/org/gcube/portlets/user/topics/server/cache/CacheInterface.java
vendored
Normal file
|
@ -0,0 +1,26 @@
|
|||
package org.gcube.portlets.user.topics.server.cache;
|
||||
|
||||
/**
|
||||
* Cache interface
|
||||
* @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it)
|
||||
* @param <K> the key type
|
||||
* @param <V> the value type
|
||||
*/
|
||||
public interface CacheInterface <K,V>{
|
||||
|
||||
/**
|
||||
* Retrieve a value V from the cache
|
||||
* @param key
|
||||
* @return
|
||||
*/
|
||||
public V get(K key);
|
||||
|
||||
/**
|
||||
* Insert an object V with key K into the cache
|
||||
* @param key
|
||||
* @param value
|
||||
* @return
|
||||
*/
|
||||
public boolean insert(K key, V value);
|
||||
|
||||
}
|
26
src/main/java/org/gcube/portlets/user/topics/server/cache/CacheUtilities.java
vendored
Normal file
26
src/main/java/org/gcube/portlets/user/topics/server/cache/CacheUtilities.java
vendored
Normal file
|
@ -0,0 +1,26 @@
|
|||
package org.gcube.portlets.user.topics.server.cache;
|
||||
|
||||
/**
|
||||
* Utility functions for caches
|
||||
* @author Costantino Perciante at ISTI-CNR
|
||||
* (costantino.perciante@isti.cnr.it)
|
||||
*
|
||||
*/
|
||||
public class CacheUtilities {
|
||||
/**
|
||||
* Check if the bean expired
|
||||
* @param beanTimestamp
|
||||
* @param ttl
|
||||
* @return <true> if expired, <false> otherwise
|
||||
*/
|
||||
public static boolean expired(long beanTimestamp, long ttl){
|
||||
|
||||
long currentTime = System.currentTimeMillis();
|
||||
|
||||
if((beanTimestamp + ttl) <= currentTime)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
|
||||
}
|
||||
}
|
40
src/main/java/org/gcube/portlets/user/topics/server/cache/CacheValueBean.java
vendored
Normal file
40
src/main/java/org/gcube/portlets/user/topics/server/cache/CacheValueBean.java
vendored
Normal file
|
@ -0,0 +1,40 @@
|
|||
package org.gcube.portlets.user.topics.server.cache;
|
||||
|
||||
/**
|
||||
* A bean object to be used as value within caches. It contains a TTL value too.
|
||||
* @author Costantino Perciante at ISTI-CNR
|
||||
* (costantino.perciante@isti.cnr.it)
|
||||
*
|
||||
* @param <V> the value type
|
||||
*/
|
||||
public class CacheValueBean <V>{
|
||||
|
||||
private V value;
|
||||
private long TTL;
|
||||
|
||||
/**
|
||||
* @param value
|
||||
* @param tTL
|
||||
*/
|
||||
public CacheValueBean(V value, long ttl) {
|
||||
super();
|
||||
this.value = value;
|
||||
this.TTL = ttl;
|
||||
}
|
||||
public V getValue() {
|
||||
return value;
|
||||
}
|
||||
public void setValue(V value) {
|
||||
this.value = value;
|
||||
}
|
||||
public long getTTL() {
|
||||
return TTL;
|
||||
}
|
||||
public void setTTL(long ttl) {
|
||||
this.TTL = ttl;
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return "CacheValueBean [value=" + value + ", TTL=" + TTL + "]";
|
||||
}
|
||||
}
|
77
src/main/java/org/gcube/portlets/user/topics/server/cache/TopicsCacheImpl.java
vendored
Normal file
77
src/main/java/org/gcube/portlets/user/topics/server/cache/TopicsCacheImpl.java
vendored
Normal file
|
@ -0,0 +1,77 @@
|
|||
package org.gcube.portlets.user.topics.server.cache;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.slf4j.LoggerFactory;
|
||||
/**
|
||||
* TopTopics cache: the key of each value is the scope
|
||||
* @author Costantino Perciante at ISTI-CNR
|
||||
* (costantino.perciante@isti.cnr.it)
|
||||
*/
|
||||
public class TopicsCacheImpl implements CacheInterface<String, ArrayList<String>> {
|
||||
|
||||
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(TopicsCacheImpl.class);
|
||||
|
||||
private static TopicsCacheImpl instance = new TopicsCacheImpl();
|
||||
|
||||
/**
|
||||
* The hashmap
|
||||
*/
|
||||
private Map<String, CacheValueBean<ArrayList<String>>> cacheMap;
|
||||
|
||||
/**
|
||||
* Cache entry expires after EXPIRED_AFTER ms
|
||||
*/
|
||||
private static final long EXPIRED_AFTER = 1000 * 60 * 2;
|
||||
|
||||
/**
|
||||
* private constructor
|
||||
*/
|
||||
private TopicsCacheImpl(){
|
||||
|
||||
this.cacheMap = new HashMap<String, CacheValueBean<ArrayList<String>>>();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the current cache instance object
|
||||
* @return
|
||||
*/
|
||||
public static TopicsCacheImpl getCacheInstance(){
|
||||
|
||||
return instance;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArrayList<String> get(String key) {
|
||||
if(cacheMap.containsKey(key)){
|
||||
|
||||
CacheValueBean<ArrayList<String>> bean = cacheMap.get(key);
|
||||
|
||||
if(CacheUtilities.expired(bean.getTTL(), EXPIRED_AFTER)){
|
||||
cacheMap.remove(key);
|
||||
logger.debug("TopTopics for scope " + key + "expired, return null");
|
||||
}
|
||||
else
|
||||
return bean.getValue();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean insert(String key, ArrayList<String> value) {
|
||||
CacheValueBean<ArrayList<String>> newBean = new CacheValueBean<ArrayList<String>>(value, System.currentTimeMillis());
|
||||
|
||||
if(cacheMap.containsKey(key))
|
||||
cacheMap.remove(key);
|
||||
|
||||
cacheMap.put(key, newBean);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue