Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could cache an Optional in which you would save Optional.absent() in the first request if the response was empty. You have to invalidate this cache's Optional entity as soon as the associated Id is present in the database, or sooner if you are experiencing memory problems with a ever growing cache.</p> <p>I like the guava's Cache and Optional classes as I think they address most of the possible issues and I'll use them in my example:</p> <pre><code>import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import com.google.common.base.Optional; import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; public class OptionalCacheUsage { private static final Cache&lt;Id, Optional&lt;Entity&gt;&gt; cache = CacheBuilder.newBuilder() .softValues() .expireAfterWrite(5, TimeUnit.MINUTES) .build(); private static final Database db = Databases.getDatabase(); Entity findEntity(final Id requestedId) throws ExecutionException, EntityNotFoundException { final Optional&lt;Entity&gt; optionalEntity = cache.get(requestedId, new Callable&lt;Optional&lt;Entity&gt;&gt;() { @Override public Optional&lt;Entity&gt; call() throws Exception { return Optional.fromNullable(db.getById(requestedId)); }}); if (optionalEntity.isPresent()) { return optionalEntity.get(); } else { throw new EntityNotFoundException(); } } Id saveEntity(final Entity newEntity) { final Id savedId = db.save(newEntity); cache.invalidate(savedId); return savedId; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload