001package io.ebeaninternal.server.core; 002 003import io.ebean.annotation.Cache; 004 005/** 006 * Options for controlling cache behaviour for a given type. 007 */ 008public class CacheOptions { 009 010 /** 011 * Instance when no caching is used. 012 */ 013 public static final CacheOptions NO_CACHING = new CacheOptions(); 014 015 private static final String R0 = "r0"; 016 017 private static final CacheOptions INVALIDATE_QUERY_CACHE_R0 = new CacheOptions(true); 018 019 private final boolean invalidateQueryCache; 020 private final boolean enableBeanCache; 021 private final boolean enableQueryCache; 022 private final boolean readOnly; 023 private final String[] naturalKey; 024 private final String region; 025 026 public static CacheOptions invalidateQueryCache(String region) { 027 if (R0.equals(region)) { 028 return INVALIDATE_QUERY_CACHE_R0; 029 } else { 030 return new CacheOptions(true, region); 031 } 032 } 033 034 /** 035 * Construct for no caching. 036 */ 037 private CacheOptions() { 038 this.invalidateQueryCache = false; 039 this.enableBeanCache = false; 040 this.enableQueryCache = false; 041 this.readOnly = false; 042 this.naturalKey = null; 043 this.region = null; 044 } 045 046 /** 047 * Construct for invalidateQueryCache. 048 */ 049 private CacheOptions(boolean invalidateQueryCache) { 050 this(invalidateQueryCache, R0); 051 } 052 053 private CacheOptions(boolean invalidateQueryCache, String region) { 054 this.invalidateQueryCache = invalidateQueryCache; 055 this.enableBeanCache = false; 056 this.enableQueryCache = false; 057 this.readOnly = false; 058 this.naturalKey = null; 059 this.region = region; 060 } 061 062 /** 063 * Construct with cache annotation. 064 */ 065 public CacheOptions(Cache cache, String[] naturalKey) { 066 this.invalidateQueryCache = false; 067 this.enableBeanCache = cache.enableBeanCache(); 068 this.enableQueryCache = cache.enableQueryCache(); 069 this.readOnly = cache.readOnly(); 070 this.naturalKey = naturalKey; 071 this.region = cache.region(); 072 } 073 074 /** 075 * Return the cache region name. 076 */ 077 public String getRegion() { 078 return region; 079 } 080 081 /** 082 * Return true if this is InvalidateQueryCache. A Bean that itself isn't L2 083 * cached but invalidates query cache entries that join to it. 084 */ 085 public boolean isInvalidateQueryCache() { 086 return invalidateQueryCache; 087 } 088 089 /** 090 * Return true if bean caching is enabled. 091 */ 092 public boolean isEnableBeanCache() { 093 return enableBeanCache; 094 } 095 096 /** 097 * Return true if query caching is enabled. 098 */ 099 public boolean isEnableQueryCache() { 100 return enableQueryCache; 101 } 102 103 /** 104 * Return true if bean cache hits default to read only. 105 */ 106 public boolean isReadOnly() { 107 return readOnly; 108 } 109 110 /** 111 * Return the natural key property name. 112 */ 113 public String[] getNaturalKey() { 114 return naturalKey; 115 } 116}