001package com.avaje.ebean.cache; 002 003import com.avaje.ebean.annotation.CacheTuning; 004 005/** 006 * Options for controlling a cache. 007 */ 008public class ServerCacheOptions { 009 010 private int maxSize; 011 private int maxIdleSecs; 012 private int maxSecsToLive; 013 private int trimFrequency; 014 015 /** 016 * Construct with no set options. 017 */ 018 public ServerCacheOptions() { 019 020 } 021 022 /** 023 * Create from the cacheTuning deployment annotation. 024 */ 025 public ServerCacheOptions(CacheTuning cacheTuning) { 026 this.maxSize = cacheTuning.maxSize(); 027 this.maxIdleSecs = cacheTuning.maxIdleSecs(); 028 this.maxSecsToLive = cacheTuning.maxSecsToLive(); 029 this.trimFrequency = cacheTuning.trimFrequency(); 030 } 031 032 /** 033 * Create merging default options with the deployment specified ones. 034 */ 035 public ServerCacheOptions(ServerCacheOptions defaults) { 036 this.maxSize = defaults.getMaxSize(); 037 this.maxIdleSecs = defaults.getMaxIdleSecs(); 038 this.maxSecsToLive = defaults.getMaxIdleSecs(); 039 this.trimFrequency = defaults.getTrimFrequency(); 040 } 041 042 /** 043 * Apply any settings from the default settings that have not already been 044 * specifically set. 045 */ 046 public void applyDefaults(ServerCacheOptions defaults) { 047 if (maxSize == 0) { 048 maxSize = defaults.getMaxSize(); 049 } 050 if (maxIdleSecs == 0) { 051 maxIdleSecs = defaults.getMaxIdleSecs(); 052 } 053 if (maxSecsToLive == 0) { 054 maxSecsToLive = defaults.getMaxSecsToLive(); 055 } 056 if (trimFrequency == 0) { 057 trimFrequency = defaults.getTrimFrequency(); 058 } 059 } 060 061 /** 062 * Return a copy of this object. 063 */ 064 public ServerCacheOptions copy() { 065 066 ServerCacheOptions copy = new ServerCacheOptions(); 067 copy.maxSize = maxSize; 068 copy.maxIdleSecs = maxIdleSecs; 069 copy.maxSecsToLive = maxSecsToLive; 070 copy.trimFrequency = trimFrequency; 071 return copy; 072 } 073 074 /** 075 * Return the maximum cache size. 076 */ 077 public int getMaxSize() { 078 return maxSize; 079 } 080 081 /** 082 * Set the maximum cache size. 083 */ 084 public void setMaxSize(int maxSize) { 085 this.maxSize = maxSize; 086 } 087 088 /** 089 * Return the maximum idle time. 090 */ 091 public int getMaxIdleSecs() { 092 return maxIdleSecs; 093 } 094 095 /** 096 * Set the maximum idle time. 097 */ 098 public void setMaxIdleSecs(int maxIdleSecs) { 099 this.maxIdleSecs = maxIdleSecs; 100 } 101 102 /** 103 * Return the maximum time to live. 104 */ 105 public int getMaxSecsToLive() { 106 return maxSecsToLive; 107 } 108 109 /** 110 * Set the maximum time to live. 111 */ 112 public void setMaxSecsToLive(int maxSecsToLive) { 113 this.maxSecsToLive = maxSecsToLive; 114 } 115 116 /** 117 * Return the trim frequency in seconds. 118 */ 119 public int getTrimFrequency() { 120 return trimFrequency; 121 } 122 123 /** 124 * Set the trim frequency in seconds. 125 */ 126 public void setTrimFrequency(int trimFrequency) { 127 this.trimFrequency = trimFrequency; 128 } 129}