/** * Populate the cache */ public void populateCache() throws Exception { // create cache primaryCache = new LinkedList<E>(); secondaryCache = new LinkedList<SoftReference<E>>(); // preallocate objects into primary cache int primaryCachePreallocation = (preallocation > primaryCacheSize) ? primaryCacheSize : preallocation; for (int i = 0; i < primaryCachePreallocation; i++) { primaryCache.offer(create()); } // preallocate objects into secondary cache int secondaryCachePreallocation = preallocation - primaryCachePreallocation; for (int i = 0; i < secondaryCachePreallocation; i++) { secondaryCache.offer(new SoftReference<E>(create())); } }
/** * Return a cached or newly created object * * @return an object */ public E get() throws Exception { E ret = poll(); if (ret == null) { ret = create(); } return ret; }