001package io.ebeaninternal.server.cache;
002
003import io.ebean.cache.QueryCacheEntry;
004import io.ebean.cache.QueryCacheEntryValidate;
005
006/**
007 * Server cache for query caching.
008 * <p>
009 * Entries in this cache contain QueryCacheEntry and we need to additionally
010 * validate the entries when hit for changes to dependent tables.
011 * </p>
012 */
013public class DefaultServerQueryCache extends DefaultServerCache {
014
015  private final QueryCacheEntryValidate queryCacheEntryValidate;
016
017  public DefaultServerQueryCache(DefaultServerCacheConfig config) {
018    super(config);
019    this.queryCacheEntryValidate = config.getQueryCacheEntryValidate();
020  }
021
022  @Override
023  protected Object unwrapEntry(CacheEntry entry) {
024    return ((QueryCacheEntry) entry.getValue()).getValue();
025  }
026
027  @Override
028  protected CacheEntry getCacheEntry(Object id) {
029    Object key = key(id);
030    CacheEntry entry = map.get(key);
031    if (entry == null) {
032      return null;
033    }
034    QueryCacheEntry value = (QueryCacheEntry) entry.getValue();
035    if (!queryCacheEntryValidate.isValid(value)) {
036      map.remove(key);
037      removeCount.increment();
038      return null;
039    }
040    return entry;
041  }
042}