Reputation: 2035
I'm incorporating cache in an existing spring project using Spring
annotations. I have created this class for storing cache configuration -
public class CacheParams {
public final String name;
public final int lifeTime;
public final TimeUnit lifeTimeUnit;
public final String key;
public CacheParams(args here) {/*implementation here*/}
}
and this is how I intend to use it -
class FooDaoCache extends FooDaoImpl {
private static final CacheParam USER_BY_ID_CACHE = new CacheParams(values here);
@Override
@Cacheable(cacheNames = USER_BY_ID_CACHE.name, key = USER_BY_ID_CACHE.key)
public User getUser(int userId) {
implementation here
}
}
But this does not work as USER_BY_ID_CACHE
will be created on compile time. If I simply create a string constant containing cache name I can successfully use it -
class FooDaoCache extends FooDaoImpl {
private static final String CACHE_NAME = "baz";
@Override
@Cacheable(cacheNames = CACHE_NAME)
public User getUser(int userId) {
//implementation here
}
}
Is there any way to deal with this or an alternate design? I need the class CacheParams
because I have to perform other operations using cache name, lifetime etc in other parts of code too.
Upvotes: 1
Views: 2390
Reputation: 348
CacheParam
needs to be an enum or the name
field needs to be a static final String
.
Annotation return types may only be primitive types, enums, Strings, classes and arrays of these.
Upvotes: 2