public final void put(final DOReader reader, long cacheTime) { try { String pid = reader.GetObjectPID(); LOG.debug("adding {} to cache", pid); mapLock.lock(); cacheMap.put(pid, new TimestampedCacheEntry<DOReader>(cacheTime, reader)); mapLock.unlock(); } catch (ServerException e) { throw new RuntimeException( "Unable to retrieve PID from reader for caching"); } } /**
/** * get an {@link DOReader} from the cache * * @param pid * the pid of the {@link DOReader} * @return the corresponding {@link DOReader} or null if there is no * applicable cache content */ public final DOReader get(final String pid) { DOReader result = null; mapLock.lock(); if (cacheMap.containsKey(pid)) { TimestampedCacheEntry<DOReader> e = cacheMap.remove(pid); LOG.debug("cache hit for {}", pid); result = e.value(); // put the cache hit at the end of the FIFO list cacheMap.put(pid, e.refresh()); } else { LOG.debug("cache miss for {}", pid); } mapLock.unlock(); return result; }
/** * Try to cache parsed Templates, but check for changes on disk * @param src * @return Templates */ public static Templates getTemplates(File src) throws TransformerException { String key = src.getAbsolutePath(); TimestampedCacheEntry<Templates> entry = TEMPLATES_CACHE.get(key); // check to see if it is null or has changed if (entry == null || entry.timestamp() < src.lastModified()) { TransformerFactory factory = null; try { factory = borrowTransformerFactory(); Templates template = factory.newTemplates(new StreamSource(src)); entry = new TimestampedCacheEntry<Templates>(src.lastModified(), template); } catch (Exception e) { throw new TransformerException(e.getMessage(), e); } finally { if (factory != null) returnTransformerFactory(factory); } TEMPLATES_CACHE.put(key, entry); } return entry.value(); }
/** * remove expired entries from the cache */ public final void removeExpired() { mapLock.lock(); Iterator<Entry<String, TimestampedCacheEntry<DOReader>>> entries = cacheMap.entrySet().iterator(); if (entries.hasNext()) { long now = System.currentTimeMillis(); while (entries.hasNext()) { Entry<String, TimestampedCacheEntry<DOReader>> entry = entries.next(); TimestampedCacheEntry<DOReader> e = entry.getValue(); long age = e.ageAt(now); if (age > (maxSeconds * 1000)) { entries.remove(); String pid = entry.getKey(); LOG.debug("removing entry {} after {} milliseconds", pid, age); } } } mapLock.unlock(); }
/** * Try to cache parsed Templates, but check for changes on disk * @param src * @return Templates */ public static Templates getTemplates(File src) throws TransformerException { String key = src.getAbsolutePath(); TimestampedCacheEntry<Templates> entry = TEMPLATES_CACHE.get(key); // check to see if it is null or has changed if (entry == null || entry.timestamp() < src.lastModified()) { TransformerFactory factory = null; try { factory = borrowTransformerFactory(); Templates template = factory.newTemplates(new StreamSource(src)); entry = new TimestampedCacheEntry<Templates>(src.lastModified(), template); } catch (Exception e) { throw new TransformerException(e.getMessage(), e); } finally { if (factory != null) returnTransformerFactory(factory); } TEMPLATES_CACHE.put(key, entry); } return entry.value(); }
/** * remove expired entries from the cache */ public final void removeExpired() { mapLock.lock(); Iterator<Entry<String, TimestampedCacheEntry<DOReader>>> entries = cacheMap.entrySet().iterator(); if (entries.hasNext()) { long now = System.currentTimeMillis(); while (entries.hasNext()) { Entry<String, TimestampedCacheEntry<DOReader>> entry = entries.next(); TimestampedCacheEntry<DOReader> e = entry.getValue(); long age = e.ageAt(now); if (age > (maxSeconds * 1000)) { entries.remove(); String pid = entry.getKey(); LOG.debug("removing entry {} after {} milliseconds", pid, age); } } } mapLock.unlock(); }
/** * get an {@link DOReader} from the cache * * @param pid * the pid of the {@link DOReader} * @return the corresponding {@link DOReader} or null if there is no * applicable cache content */ public final DOReader get(final String pid) { DOReader result = null; mapLock.lock(); if (cacheMap.containsKey(pid)) { TimestampedCacheEntry<DOReader> e = cacheMap.remove(pid); LOG.debug("cache hit for {}", pid); result = e.value(); // put the cache hit at the end of the FIFO list cacheMap.put(pid, e.refresh()); } else { LOG.debug("cache miss for {}", pid); } mapLock.unlock(); return result; }
public final void put(final DOReader reader, long cacheTime) { try { String pid = reader.GetObjectPID(); LOG.debug("adding {} to cache", pid); mapLock.lock(); cacheMap.put(pid, new TimestampedCacheEntry<DOReader>(cacheTime, reader)); mapLock.unlock(); } catch (ServerException e) { throw new RuntimeException( "Unable to retrieve PID from reader for caching"); } } /**