use of net.sf.ehcache.Element in project ORCID-Source by ORCID.
the class WorkEntityCacheManagerImpl method retrieveWorkLastModifiedList.
@Override
public List<WorkLastModifiedEntity> retrieveWorkLastModifiedList(String orcid, long profileLastModified) {
Object key = new ProfileCacheKey(orcid, profileLastModified, releaseName);
List<WorkLastModifiedEntity> workLastModifiedList = toWorkLastModifiedList(getFromWorkLastModifiedCache(key));
if (workLastModifiedList == null) {
try {
synchronized (lockers.obtainLock(orcid)) {
workLastModifiedList = toWorkLastModifiedList(getFromWorkLastModifiedCache(key));
if (workLastModifiedList == null) {
workLastModifiedList = workDao.getWorkLastModifiedList(orcid);
workLastModifiedCache.put(new Element(key, workLastModifiedList));
}
}
} finally {
lockers.releaseLock(orcid);
}
}
return workLastModifiedList;
}
use of net.sf.ehcache.Element in project ORCID-Source by ORCID.
the class WorkEntityCacheManagerImpl method retrievePublicWorkLastModifiedList.
@Override
public List<WorkLastModifiedEntity> retrievePublicWorkLastModifiedList(String orcid, long profileLastModified) {
Object key = new ProfileCacheKey(orcid, profileLastModified, releaseName);
List<WorkLastModifiedEntity> workLastModifiedList = toWorkLastModifiedList(getFromPublicWorkLastModifiedCache(key));
if (workLastModifiedList == null) {
try {
synchronized (publicWorkLastModifiedListLockers.obtainLock(orcid)) {
workLastModifiedList = toWorkLastModifiedList(getFromPublicWorkLastModifiedCache(key));
if (workLastModifiedList == null) {
workLastModifiedList = workDao.getPublicWorkLastModifiedList(orcid);
publicWorkLastModifiedCache.put(new Element(key, workLastModifiedList));
}
}
} finally {
publicWorkLastModifiedListLockers.releaseLock(orcid);
}
}
return workLastModifiedList;
}
use of net.sf.ehcache.Element in project ORCID-Source by ORCID.
the class SourceNameCacheManagerImpl method retrieve.
@Override
public String retrieve(String sourceId) throws IllegalArgumentException {
String cacheKey = getCacheKey(sourceId);
String sourceName = getSourceNameFromCache(sourceNameCache.get(cacheKey));
if (sourceName == null) {
try {
synchronized (lockers.obtainLock(sourceId)) {
sourceName = getSourceNameFromCache(sourceNameCache.get(cacheKey));
if (sourceName == null) {
LOGGER.debug("Fetching source name for: " + sourceId);
sourceName = getProfileSourceNameFromRequest(sourceId);
if (sourceName == null) {
sourceName = getClientSourceName(sourceId);
if (sourceName != null) {
sourceNameCache.put(new Element(cacheKey, sourceName));
} else {
sourceName = getProfileSourceNameFromDb(sourceId);
}
}
}
}
} finally {
lockers.releaseLock(sourceId);
}
}
// a null value instead
if (StringUtils.EMPTY.equals(sourceName)) {
return null;
}
return sourceName;
}
use of net.sf.ehcache.Element in project ORCID-Source by ORCID.
the class WorkEntityCacheManagerImpl method retrieveWorkList.
/**
* Fetches a list of minimised works - does this by checking cache and then
* fetching all misses in one go from the DB.
*
* @param workIdsWithLastModified
* @return
*/
@Override
public <T extends WorkBaseEntity> List<T> retrieveWorkList(Map<Long, Date> workIdsWithLastModified, Cache workCache, LockerObjectsManager lockerObjectsManager, Function<List<Long>, List<T>> workRetriever) {
WorkBaseEntity[] returnArray = new WorkBaseEntity[workIdsWithLastModified.size()];
List<Long> fetchList = new ArrayList<Long>();
Map<Long, Integer> fetchListIndexOrder = new LinkedHashMap<Long, Integer>();
int index = 0;
for (Long workId : workIdsWithLastModified.keySet()) {
// get works from the cache if we can
Object key = new WorkCacheKey(workId, releaseName);
WorkBaseEntity cachedWork = toWorkBaseEntity(workCache.get(key));
if (cachedWork == null || cachedWork.getLastModified().getTime() < workIdsWithLastModified.get(workId).getTime()) {
fetchListIndexOrder.put(workId, index);
fetchList.add(workId);
} else {
returnArray[index] = cachedWork;
}
index++;
}
// now fetch all the others that are *not* in the cache
if (fetchList.size() > 0) {
List<? extends WorkBaseEntity> refreshedWorks = workRetriever.apply(fetchList);
for (WorkBaseEntity mWorkRefreshedFromDB : refreshedWorks) {
Object key = new WorkCacheKey(mWorkRefreshedFromDB.getId(), releaseName);
try {
synchronized (lockerObjectsManager.obtainLock(Long.toString(mWorkRefreshedFromDB.getId()))) {
// check cache again here to prevent race condition
// since something could have updated while we were
// fetching from DB
// (or can we skip because new last modified is always
// going to be after profile last modified as provided)
WorkBaseEntity cachedWork = toWorkBaseEntity(workCache.get(key));
int returnListIndex = fetchListIndexOrder.get(mWorkRefreshedFromDB.getId());
if (cachedWork == null || cachedWork.getLastModified().getTime() < workIdsWithLastModified.get(mWorkRefreshedFromDB.getId()).getTime()) {
workCache.put(new Element(key, mWorkRefreshedFromDB));
returnArray[returnListIndex] = mWorkRefreshedFromDB;
} else {
returnArray[returnListIndex] = cachedWork;
}
}
} finally {
lockerObjectsManager.releaseLock(Long.toString(mWorkRefreshedFromDB.getId()));
}
}
}
@SuppressWarnings("unchecked") List<T> results = (List<T>) Arrays.asList(returnArray);
return results;
}
use of net.sf.ehcache.Element in project ORCID-Source by ORCID.
the class OrcidProfileCacheManagerImpl method retrievePublicBio.
@Override
public OrcidProfile retrievePublicBio(String orcid) {
Object key = new OrcidCacheKey(orcid, releaseName);
Date dbDate = retrieveLastModifiedDate(orcid);
OrcidProfile op = toOrcidProfile(publicBioCache.get(key));
if (needsFresh(dbDate, op))
try {
synchronized (pubBioLocks.obtainLock(orcid)) {
op = toOrcidProfile(publicBioCache.get(orcid));
if (needsFresh(dbDate, op)) {
op = orcidProfileManager.retrievePublicOrcidProfile(orcid, LoadOptions.BIO_ONLY);
publicBioCache.put(new Element(key, op));
}
}
} finally {
pubBioLocks.releaseLock(orcid);
}
return op;
}
Aggregations