Search in sources :

Example 31 with Element

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;
}
Also used : WorkLastModifiedEntity(org.orcid.persistence.jpa.entities.WorkLastModifiedEntity) Element(net.sf.ehcache.Element)

Example 32 with Element

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;
}
Also used : WorkLastModifiedEntity(org.orcid.persistence.jpa.entities.WorkLastModifiedEntity) Element(net.sf.ehcache.Element)

Example 33 with Element

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;
}
Also used : Element(net.sf.ehcache.Element)

Example 34 with Element

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;
}
Also used : Element(net.sf.ehcache.Element) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) List(java.util.List) WorkBaseEntity(org.orcid.persistence.jpa.entities.WorkBaseEntity)

Example 35 with Element

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;
}
Also used : OrcidProfile(org.orcid.jaxb.model.message.OrcidProfile) Element(net.sf.ehcache.Element) Date(java.util.Date)

Aggregations

Element (net.sf.ehcache.Element)114 Test (org.junit.Test)21 CacheKey (org.apereo.portal.utils.cache.CacheKey)8 ArrayList (java.util.ArrayList)7 Date (java.util.Date)7 Cache (net.sf.ehcache.Cache)7 HashSet (java.util.HashSet)6 CacheException (net.sf.ehcache.CacheException)6 MalformedURLException (java.net.MalformedURLException)5 ConfigurationException (javax.naming.ConfigurationException)5 Ehcache (net.sf.ehcache.Ehcache)5 CacheConfiguration (net.sf.ehcache.config.CacheConfiguration)5 EntityIdentifier (org.apereo.portal.EntityIdentifier)5 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 URISyntaxException (java.net.URISyntaxException)4 SQLException (java.sql.SQLException)4 EntityExistsException (javax.persistence.EntityExistsException)4 RouteBuilder (org.apache.camel.builder.RouteBuilder)4 BaseCacheTest (org.apache.camel.component.BaseCacheTest)4