Beispiel #1
0
 private void removeFromCache(Object key) {
   try {
     Cache cache =
         CacheManager.getInstance().getCacheFactory().createCache(Collections.emptyMap());
     cache.remove(key);
   } catch (CacheException e) {
     Logger.getLogger(location).log(Level.SEVERE, "removeFromCache(): Cache error: " + e);
     e.printStackTrace();
   }
 }
Beispiel #2
0
  /**
   * Adds new updates to storage.
   *
   * @param newUpdates
   */
  public static void addUpdates(List<TrainUpdate> newUpdates) {
    if (newUpdates.isEmpty()) {
      return;
    }

    // Add the new updates to the list of cached results.
    @SuppressWarnings("unchecked")
    List<TrainUpdate> cachedUpdates = (List<TrainUpdate>) cache.get(CACHED_UPDATES_KEY);

    if (cachedUpdates == null) {
      cachedUpdates = new ArrayList<TrainUpdate>();
    }

    log.info("Fetched cache with size of : " + cachedUpdates.size());
    cachedUpdates.addAll(newUpdates);
    // Update the storage stats
    StorageStats stats = getStorageStats();
    stats.setLatestUpdateId(newUpdates.get(0).getTwitterId());

    // Attempt to persist the updates to the data store.
    PersistenceManager pm = PMF.get().getPersistenceManager();

    try {
      pm.makePersistentAll(cachedUpdates);
      pm.makePersistent(stats);
      cachedUpdates.clear();
      log.info("Cache cleared");
    } catch (DatastoreTimeoutException ex) {
      log.info("Couldn't write to datastore.  caching instead");
    } finally {
      // No matter what happens, store the new cached results.
      cache.remove(CACHED_QUERIES_KEY);
      cache.put(CACHED_UPDATES_KEY, cachedUpdates);
      cache.put(STORAGE_STATS_KEY, stats);
      log.info("Updated cache to size of : " + cachedUpdates.size());
      pm.close();
    }
  }
 public void expireArticlesUrl() {
   cache.remove(RECENT_ARTICLE_KEY);
   cache.remove(POPULAR_ARTICLE_KEY);
   cache.remove(RELATIVE_URL_KEY);
 }