Java Examples for com.opensymphony.oscache.base.NeedsRefreshException

The following java examples will help you to understand the usage of com.opensymphony.oscache.base.NeedsRefreshException. These source code samples are taken from different open source projects.

Example 1
Project: spring-modules-master  File: OsCacheFacadeTests.java View source code
public void testOnPutInCacheWithGroups() throws Exception {
    setUpCacheAdministrator();
    Object objectToStore = "An Object";
    String group = groups[0];
    cachingModel.setGroups(new String[] { group });
    // execute the method to test.
    osCacheFacade.onPutInCache(CACHE_KEY, cachingModel, objectToStore);
    Object cachedObject = cacheAdministrator.getFromCache(CACHE_KEY);
    assertSame(objectToStore, cachedObject);
    // if we flush the group used, we should not be able to get the cached
    // object.
    cacheAdministrator.flushGroup(group);
    try {
        cacheAdministrator.getFromCache(CACHE_KEY);
        fail();
    } catch (NeedsRefreshException exception) {
    }
}
Example 2
Project: shtaobatis-master  File: OSCacheController.java View source code
public Object removeObject(CacheModel cacheModel, Object key) {
    Object result;
    String keyString = key.toString();
    try {
        int refreshPeriod = (int) (cacheModel.getFlushIntervalSeconds());
        Object value = CACHE.getFromCache(keyString, refreshPeriod);
        if (value != null) {
            CACHE.flushEntry(keyString);
        }
        result = value;
    } catch (NeedsRefreshException e) {
        try {
            CACHE.flushEntry(keyString);
        } finally {
            CACHE.cancelUpdate(keyString);
            result = null;
        }
    }
    return result;
}
Example 3
Project: gnutch-master  File: CacheManager.java View source code
/**
   * Get Search object from cache
   * @param id key
   * @return
   * @throws NeedsRefreshException
   */
public Search getSearch(String id, ServiceLocator locator) throws NeedsRefreshException {
    Search search = null;
    ByteBufferWrapper w = (ByteBufferWrapper) cache.getFromCache(id);
    if (w != null) {
        try {
            long time = System.currentTimeMillis();
            ByteArrayInputStream is = new ByteArrayInputStream(w.getContents());
            GZIPInputStream gs = new GZIPInputStream(is);
            DataInputStream dis = new DataInputStream(gs);
            search = new Search(locator);
            search.readFields(dis);
            long delta = System.currentTimeMillis() - time;
            if (LOG.isDebugEnabled()) {
                LOG.debug("Decompressing cache entry took: " + delta + "ms.");
            }
            search.init();
        } catch (IOException e) {
            LOG.info("Could not get cached object: " + e);
        }
    }
    return search;
}
Example 4
Project: shopizer-v1.1.5-master  File: OsCacheCacheModuleImpl.java View source code
public Object getFromCache(String key, MerchantStore store) throws CacheModuleException {
    // @todo, refreshPeriod from configuration file
    // property to cache or not //2000 seconds
    Object o = null;
    key = key + store.getMerchantId();
    if (store != null && store.isUseCache()) {
        try {
            o = admin.getFromCache(key, 2000);
        } catch (NeedsRefreshException nre) {
            admin.cancelUpdate(key);
        } catch (Exception e) {
            admin.cancelUpdate(key);
            throw new CacheModuleException(e);
        }
    }
    return o;
}
Example 5
Project: Sesat-master  File: NewsAggregatorSearchCommand.java View source code
/**
         * Loads an XML document from a URL i.e. file:// or http:// or cache based on a cache timeout
         * Gets from URL and stores the result in the cache
         * if a NeedsRefreshException is thrown by the cache
         * Adapted from TvWaitSearchCommand
         *
         * @param xmlUrl    the url to the xml
         * @return the parsed XML as an org.w3c.dom.Document object
         * @throws org.xml.sax.SAXException if the underlying sax parser throws an exception
         * @throws java.io.IOException      if the file could not be read for some reason
         */
private final Document getDocumentFromCache(final String urlString) throws IOException, SAXException {
    Document doc;
    try {
        doc = (Document) CACHE.getFromCache(urlString, REFRESH_PERIOD);
        LOG.debug("Got doc from cache for " + urlString);
    } catch (NeedsRefreshException e) {
        boolean updatedCache = false;
        try {
            doc = getDocumentFromUrl(urlString);
            CACHE.putInCache(urlString, doc);
            LOG.debug("Got doc from url and added to cache for " + urlString);
            updatedCache = true;
        } finally {
            if (!updatedCache) {
                CACHE.cancelUpdate(urlString);
            }
        }
    }
    return doc;
}
Example 6
Project: cayenne-master  File: OSQueryCache.java View source code
@SuppressWarnings("rawtypes")
public List get(QueryMetadata metadata) {
    String key = metadata.getCacheKey();
    if (key == null) {
        return null;
    }
    RefreshSpecification refresh = getRefreshSpecification(metadata);
    try {
        return (List) osCache.getFromCache(key, refresh.refreshPeriod, refresh.cronExpression);
    } catch (NeedsRefreshException e) {
        osCache.cancelUpdate(key);
        return null;
    }
}
Example 7
Project: gocd-master  File: StageSqlMapDao.java View source code
public Long getDurationOfLastSuccessfulOnAgent(String pipelineName, String stageName, JobInstance job) {
    String key = job.getBuildDurationKey(pipelineName, stageName);
    Long duration;
    try {
        duration = (Long) cache.getFromCache(key, CacheEntry.INDEFINITE_EXPIRY);
    } catch (NeedsRefreshException nre) {
        boolean updated = false;
        try {
            duration = recalculateBuildDuration(pipelineName, stageName, job);
            cache.putInCache(key, duration);
            updated = true;
        } finally {
            if (!updated) {
                cache.cancelUpdate(key);
                LOGGER.warn("refresh cancelled for " + key);
            }
        }
    }
    return duration;
}
Example 8
Project: infoglue-master  File: CacheController.java View source code
public static Object getCachedObjectFromAdvancedCache(String cacheName, String key, boolean useFileCacheFallback, String fileCacheCharEncoding, boolean cacheFileResultInMemory) {
    if (cacheName == null || key == null || key.length() == 0)
        return null;
    if (getDefeatCaches().getDefeatCache("" + cacheName + "_" + key))
        return null;
    Object value = null;
    boolean stopUseFileCacheFallback = false;
    //synchronized(caches) 
    //{
    GeneralCacheAdministrator cacheAdministrator = (GeneralCacheAdministrator) caches.get(cacheName);
    if (cacheAdministrator != null) {
        //TODO
        if (CmsPropertyHandler.getUseSynchronizationOnCaches()) {
            synchronized (cacheAdministrator//Back
            ) {
                try {
                    if (CmsPropertyHandler.getUseHashCodeInCaches())
                        value = (cacheAdministrator == null) ? null : cacheAdministrator.getFromCache("" + key.hashCode(), CacheEntry.INDEFINITE_EXPIRY);
                    else
                        value = (cacheAdministrator == null) ? null : cacheAdministrator.getFromCache(key, CacheEntry.INDEFINITE_EXPIRY);
                //RequestAnalyser.getRequestAnalyser().registerComponentStatistics("Getting from cache on " + cacheName, t.getElapsedTime());
                } catch (NeedsRefreshException nre) {
                    if (useFileCacheFallback && nre.getCacheContent() != null) {
                        stopUseFileCacheFallback = true;
                    }
                    try {
                        if (CmsPropertyHandler.getUseHashCodeInCaches())
                            cacheAdministrator.cancelUpdate("" + key.hashCode());
                        else
                            cacheAdministrator.cancelUpdate(key);
                    } catch (Exception e) {
                        logger.error("Error:" + e.getMessage());
                    }
                }
            }
        } else {
            try {
                if (CmsPropertyHandler.getUseHashCodeInCaches())
                    value = (cacheAdministrator == null) ? null : cacheAdministrator.getFromCache("" + key.hashCode(), CacheEntry.INDEFINITE_EXPIRY);
                else
                    value = (cacheAdministrator == null) ? null : cacheAdministrator.getFromCache(key, CacheEntry.INDEFINITE_EXPIRY);
            } catch (NeedsRefreshException nre) {
                if (useFileCacheFallback && nre.getCacheContent() != null) {
                    stopUseFileCacheFallback = true;
                }
                try {
                    if (CmsPropertyHandler.getUseHashCodeInCaches())
                        cacheAdministrator.cancelUpdate("" + key.hashCode());
                    else
                        cacheAdministrator.cancelUpdate(key);
                } catch (Exception e) {
                    logger.error("Error:" + e.getMessage());
                }
            }
        }
    }
    if (value == null && useFileCacheFallback && !stopUseFileCacheFallback) {
        Timer t = new Timer();
        if (logger.isInfoEnabled())
            logger.info("Getting cache content from file..");
        value = getCachedContentFromFile(cacheName, key, fileCacheCharEncoding);
        if (value != null && cacheFileResultInMemory) {
            if (logger.isInfoEnabled())
                logger.info("Got cached content from file as it did not exist in memory...:" + value.toString().length());
            if (CmsPropertyHandler.getUseHashCodeInCaches())
                cacheObjectInAdvancedCache(cacheName, "" + key.hashCode(), value);
            else
                cacheObjectInAdvancedCache(cacheName, key, value);
        }
        RequestAnalyser.getRequestAnalyser().registerComponentStatistics("File cache", t.getElapsedTime());
    }
    return value;
}
Example 9
Project: ibatis-2-master  File: OSCacheController.java View source code
public Object removeObject(CacheModel cacheModel, Object key) {
    Object result;
    String keyString = key.toString();
    try {
        int refreshPeriod = (int) (cacheModel.getFlushIntervalSeconds());
        Object value = CACHE.getFromCache(keyString, refreshPeriod);
        if (value != null) {
            CACHE.flushEntry(keyString);
        }
        result = value;
    } catch (NeedsRefreshException e) {
        try {
            CACHE.flushEntry(keyString);
        } finally {
            CACHE.cancelUpdate(keyString);
            result = null;
        }
    }
    return result;
}
Example 10
Project: activejdbc-master  File: OSCacheManager.java View source code
@Override
public Object getCache(String group, String key) {
    try {
        return administrator.getFromCache(key);
    } catch (NeedsRefreshException nre) {
        try {
            administrator.cancelUpdate(key);
        } catch (Exception e) {
            return null;
        }
    }
    return null;
}
Example 11
Project: aspectj-in-action-code-master  File: CacheAspectTest.java View source code
@Test
public void cacheMiss() throws Exception {
    when(mockCache.getFromCache("test:testArg")).thenThrow(new NeedsRefreshException("miss"));
    testService.get("testArg");
    verify(mockHelper).get();
}
Example 12
Project: richfaces-master  File: OSCacheCache.java View source code
public Object get(Object key) {
    String stringKey = (String) key;
    try {
        return cache.getFromCache(stringKey);
    } catch (NeedsRefreshException e) {
        cache.removeEntry(stringKey);
        return null;
    }
}
Example 13
Project: cosmos-message-master  File: OsCacheImpl.java View source code
public Object get(String key) {
    try {
        return this.admin.getFromCache(key, (int) this.defaultTimeout);
    } catch (NeedsRefreshException e) {
        logger.error("get cache object for key '{}' append exception:'{}'!", key, e);
        return null;
    }
}
Example 14
Project: ProjectModel-master  File: OSCache.java View source code
public Object get(Object key) throws CacheException {
    try {
        return cache.getFromCache(toString(key), refreshPeriod, cron);
    } catch (NeedsRefreshException e) {
        cache.cancelUpdate(toString(key));
        return null;
    }
}
Example 15
Project: castor-master  File: TestOsCache.java View source code
public void testStaticProperties() {
    assertEquals("oscache", OsCache.TYPE);
    assertEquals("com.opensymphony.oscache.general.GeneralCacheAdministrator", OsCache.IMPLEMENTATION);
    assertEquals("com.opensymphony.oscache.base.NeedsRefreshException", OsCache.NEEDS_REFRESH_EXCEPTION);
}