Skip to content

Commit

Permalink
SAK-31645 replace duplicated cache evict method with a common one. Fi…
Browse files Browse the repository at this point in the history
…x a few other broken windows (sakaiproject#3148)
  • Loading branch information
steveswinsburg authored Aug 6, 2016
1 parent ddd2761 commit 126b1fa
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,14 @@ public interface CacheManager {
* @param cacheName
* @return
*/
public Cache createCache(String cacheName);
@SuppressWarnings("rawtypes")
Cache createCache(String cacheName);

/**
* Helper to evict an item from a given cache.
* @param cache the cache to evict from
* @param cacheKey the id for the data in the cache
*/
@SuppressWarnings("rawtypes")
void evictFromCache(Cache cache, String cacheKey);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
*/
package org.sakaiproject.profile2.cache;

import lombok.Setter;

import org.sakaiproject.memory.api.Cache;
import org.sakaiproject.memory.api.MemoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import lombok.Setter;

/**
* Implementation of CacheManager for Profile2.
Expand All @@ -28,13 +30,22 @@
*/
public class CacheManagerImpl implements CacheManager {

/**
* {@inheritDoc}
*/
private static final Logger log = LoggerFactory.getLogger(CacheManagerImpl.class);

@Override
@SuppressWarnings("rawtypes")
public Cache createCache(String cacheName) {
return memoryService.newCache(cacheName);
return memoryService.getCache(cacheName);
}


@Override
@SuppressWarnings({"unchecked", "rawtypes"})
public void evictFromCache(Cache cache, String cacheKey) {
cache.remove(cacheKey);
log.debug("Evicted data in cache: " + cache.getName() + ", key: " + cacheKey);
}

@Setter
private MemoryService memoryService;

}
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,8 @@ public boolean confirmFriendRequest(final String fromUser, final String toUser)
sakaiProxy.postEvent(ProfileConstants.EVENT_FRIEND_CONFIRM, "/profile/"+fromUser, true);

//invalidate the confirmed connection caches for each user as they are now stale
evictFromCache(fromUser);
evictFromCache(toUser);
this.cacheManager.evictFromCache(this.cache, fromUser);
this.cacheManager.evictFromCache(this.cache, toUser);

return true;
}
Expand Down Expand Up @@ -371,8 +371,8 @@ public boolean removeFriend(String userId, String friendId) {
log.info("User: " + userId + " removed friend: " + friendId);

//invalidate the confirmed connection caches for each user as they are now stale
evictFromCache(userId);
evictFromCache(friendId);
this.cacheManager.evictFromCache(this.cache, userId);
this.cacheManager.evictFromCache(this.cache, friendId);

return true;
}
Expand Down Expand Up @@ -538,7 +538,7 @@ private List<String> getConfirmedConnectionUserIdsForUser(final String userUuid)
if(userUuids == null) {
// This means that the cache has expired. evict the key from the cache
log.debug("Connections cache appears to have expired for " + userUuid);
evictFromCache(userUuid);
this.cacheManager.evictFromCache(this.cache, userUuid);
}
}
if(userUuids == null) {
Expand Down Expand Up @@ -605,16 +605,6 @@ private void sendConnectionEmailNotification(String toUuid, final String fromUui

}

/**
* Helper to evict an item from a cache.
* @param cacheKey the id for the data in the cache
*/
private void evictFromCache(String cacheKey) {
cache.remove(cacheKey);
log.debug("Evicted data in cache for key: " + cacheKey);
}


public void init() {
cache = cacheManager.createCache(CACHE_NAME);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,15 @@
import java.util.HashMap;
import java.util.Map;

import lombok.Setter;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sakaiproject.profile2.dao.ProfileDao;
import org.sakaiproject.profile2.model.ExternalIntegrationInfo;
import org.sakaiproject.profile2.util.ProfileConstants;
import org.sakaiproject.profile2.util.ProfileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import lombok.Setter;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
Expand All @@ -52,6 +51,7 @@ public class ProfileExternalIntegrationLogicImpl implements ProfileExternalInteg
/**
* {@inheritDoc}
*/
@Override
public ExternalIntegrationInfo getExternalIntegrationInfo(String userUuid) {
ExternalIntegrationInfo info = dao.getExternalIntegrationInfo(userUuid);
if(info != null) {
Expand All @@ -63,6 +63,7 @@ public ExternalIntegrationInfo getExternalIntegrationInfo(String userUuid) {
/**
* {@inheritDoc}
*/
@Override
public boolean updateExternalIntegrationInfo(ExternalIntegrationInfo info) {
if(dao.updateExternalIntegrationInfo(info)){
log.info("ExternalIntegrationInfo updated for user: " + info.getUserUuid());
Expand All @@ -74,6 +75,7 @@ public boolean updateExternalIntegrationInfo(ExternalIntegrationInfo info) {
/**
* {@inheritDoc}
*/
@Override
public Map<String,String> getTwitterOAuthConsumerDetails() {

Map<String,String> map = new HashMap<String,String>();
Expand All @@ -86,6 +88,7 @@ public Map<String,String> getTwitterOAuthConsumerDetails() {
/**
* {@inheritDoc}
*/
@Override
public String getTwitterName(ExternalIntegrationInfo info) {

if(info == null){
Expand Down Expand Up @@ -123,13 +126,15 @@ public String getTwitterName(ExternalIntegrationInfo info) {
/**
* {@inheritDoc}
*/
@Override
public boolean validateTwitterCredentials(ExternalIntegrationInfo info) {
return StringUtils.isNotBlank(getTwitterName(info));
}

/**
* {@inheritDoc}
*/
@Override
public void sendMessageToTwitter(final String userUuid, String message){
//setup class thread to call later
class TwitterUpdater implements Runnable{
Expand All @@ -151,6 +156,7 @@ public TwitterUpdater(String userUuid, String userToken, String userSecret, Stri


//do it!
@Override
public synchronized void run() {

//global config
Expand Down Expand Up @@ -202,6 +208,7 @@ public synchronized void run() {
/**
* {@inheritDoc}
*/
@Override
public String getGoogleAuthenticationUrl() {

String clientId = sakaiProxy.getServerConfigurationParameter("profile2.integration.google.client-id", null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
import java.math.BigDecimal;
import java.util.Date;

import lombok.Setter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sakaiproject.memory.api.Cache;
import org.sakaiproject.profile2.cache.CacheManager;
import org.sakaiproject.profile2.dao.ProfileDao;
import org.sakaiproject.profile2.hbm.model.ProfileKudos;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import lombok.Setter;

/**
* Implementation of ProfileKudosLogic API
Expand All @@ -44,6 +44,7 @@ public class ProfileKudosLogicImpl implements ProfileKudosLogic {
/**
* {@inheritDoc}
*/
@Override
public int getKudos(String userUuid){

ProfileKudos k = null;
Expand All @@ -54,7 +55,7 @@ public int getKudos(String userUuid){
if(k == null) {
// This means that the cache has expired. evict the key from the cache
log.debug("Kudos cache appears to have expired for " + userUuid);
evictFromCache(userUuid);
this.cacheManager.evictFromCache(this.cache, userUuid);
}
}
if(k == null) {
Expand All @@ -75,6 +76,7 @@ public int getKudos(String userUuid){
/**
* {@inheritDoc}
*/
@Override
public BigDecimal getRawKudos(String userUuid){
ProfileKudos k = dao.getKudos(userUuid);
if(k == null){
Expand All @@ -86,6 +88,7 @@ public BigDecimal getRawKudos(String userUuid){
/**
* {@inheritDoc}
*/
@Override
public boolean updateKudos(String userUuid, int score, BigDecimal percentage) {
ProfileKudos k = new ProfileKudos();
k.setUserUuid(userUuid);
Expand All @@ -100,15 +103,6 @@ public boolean updateKudos(String userUuid, int score, BigDecimal percentage) {
}
return false;
}

/**
* Helper to evict an item from a cache.
* @param cacheKey the id for the data in the cache
*/
private void evictFromCache(String cacheKey) {
cache.remove(cacheKey);
log.debug("Evicted data in cache for key: " + cacheKey);
}

public void init() {
cache = cacheManager.createCache(CACHE_NAME);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@
*/
package org.sakaiproject.profile2.logic;

import lombok.Setter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sakaiproject.memory.api.Cache;
import org.sakaiproject.profile2.cache.CacheManager;
import org.sakaiproject.profile2.dao.ProfileDao;
import org.sakaiproject.profile2.model.ProfilePreferences;
import org.sakaiproject.profile2.types.PreferenceType;
import org.sakaiproject.profile2.util.ProfileConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import lombok.Setter;

/**
* Implementation of ProfilePreferencesLogic API
Expand All @@ -42,13 +42,15 @@ public class ProfilePreferencesLogicImpl implements ProfilePreferencesLogic {
/**
* {@inheritDoc}
*/
@Override
public ProfilePreferences getPreferencesRecordForUser(final String userId) {
return getPreferencesRecordForUser(userId, true);
}

/**
* {@inheritDoc}
*/
@Override
public ProfilePreferences getPreferencesRecordForUser(final String userId, final boolean useCache) {

if(userId == null){
Expand All @@ -68,7 +70,7 @@ public ProfilePreferences getPreferencesRecordForUser(final String userId, final
}
// This means that the cache has expired. evict the key from the cache
log.debug("Preferences cache appears to have expired for " + userId);
evictFromCache(userId);
this.cacheManager.evictFromCache(this.cache, userId);
}
}

Expand Down Expand Up @@ -102,6 +104,7 @@ public ProfilePreferences getPreferencesRecordForUser(final String userId, final
/**
* {@inheritDoc}
*/
@Override
public boolean savePreferencesRecord(ProfilePreferences prefs) {

if(dao.savePreferencesRecord(prefs)){
Expand All @@ -120,6 +123,7 @@ public boolean savePreferencesRecord(ProfilePreferences prefs) {
/**
* {@inheritDoc}
*/
@Override
public boolean isPreferenceEnabled(final String userUuid, final PreferenceType type) {

//get preferences record for this user
Expand Down Expand Up @@ -172,16 +176,6 @@ private ProfilePreferences getDefaultPreferencesRecord(final String userId) {
return prefs;
}


/**
* Helper to evict an item from a cache.
* @param cacheKey the id for the data in the cache
*/
private void evictFromCache(String cacheKey) {
cache.remove(cacheKey);
log.debug("Evicted data in cache for key: " + cacheKey);
}

public void init() {
cache = cacheManager.createCache(CACHE_NAME);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public ProfilePrivacy getPrivacyRecordForUser(final String userId, final boolean
}
// This means that the cache has expired. evict the key from the cache
log.debug("Privacy cache appears to have expired for " + userId);
evictFromCache(userId);
this.cacheManager.evictFromCache(this.cache, userId);
}
}

Expand Down Expand Up @@ -522,14 +522,6 @@ private ProfilePrivacy getDefaultPrivacyRecord(String userId) {
return privacy;
}

/**
* Helper to evict an item from a cache.
* @param cacheKey the id for the data in the cache
*/
private void evictFromCache(String cacheKey) {
cache.remove(cacheKey);
log.debug("Evicted data in cache for key: " + cacheKey);
}

public void init() {
cache = cacheManager.createCache(CACHE_NAME);
Expand Down
Loading

0 comments on commit 126b1fa

Please sign in to comment.