背景
这是我的工作(简体) GenericDao
接口,这是任何实现DomainDao
:
GenericDao.java
@NoRepositoryBean
public interface GenericDao<E extends Persistable<K>, K extends Serializable> extends JpaRepository<E, K> {
public List<E> findAll();
public E persist(E entity);
}
GenericDaoImpl.java
public class GenericDaoImpl<E extends Persistable<K>, K extends Serializable> extends SimpleJpaRepository<E, K> implements GenericDao<E, K> {
private final JpaEntityInformation<E, ?> entityInformation;
private final EntityManager em;
private final Class<E> type;
public GenericDaoImpl(JpaEntityInformation<E, ?> entityInformation, EntityManager em) {
super(entityInformation, em);
this.entityInformation = entityInformation;
this.em = em;
this.type = entityInformation.getJavaType();
}
@Override
public List<E> findAll() {
return super.findAll();
}
@Override
@Transactional
public E persist(E entity) {
if (entityInformation.isNew(entity) || !EntityUtils.isPrimaryKeyGenerated(type) && !em.contains(entity)) {
em.persist(entity);
}
return entity;
}
}
例如,管理域Foo
和Bar
,你只需要创建两个接口如下:
FooDao.java
public interface FooDao extends GenericDao<Foo, Integer> {
}
BarDao.java
public interface BarDao extends GenericDao<Bar, Integer> {
}
该@Autowired
的注解Spring
会自动实例化一个GenericDaoImpl
与良好的实体和主键类型。
问题
现在我想加上我的DAO高速缓存过程,使用的Ehcache和的Ehcache Spring注解模型。
GenericDao.java
@NoRepositoryBean
public interface GenericDao<E extends Persistable<K>, K extends Serializable> extends JpaRepository<E, K> {
@Cacheable(cacheName = "dao")
public List<E> findAll();
@TriggersRemove(cacheName = "dao")
public E persist(E entity);
}
applicationContext.xml中
<ehcache:annotation-driven cache-manager="ehCacheManager" />
<bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" />
ehcache.xml中
<cache name="dao"
eternal="false"
maxElementsInMemory="10000"
overflowToDisk="false"
timeToIdleSeconds="86400"
timeToLiveSeconds="86400"
memoryStoreEvictionPolicy="LFU" />
与使用的问题GenericDao
,是高速缓存应该管理每个DomainDao
相互独立的。 例如,当前的配置,如果我叫fooDao.findAll()
然后barDao.persist(new Bar())
所产生的缓存fooDao.findAll()
将被重置,因为同样的缓存会被使用(即<cache name="dao" />
而它不应该。
路线
我试图实现自己的CacheKeyGenerator
,将考虑到呼叫的类型DomainDao
:
applicationContext.xml中
<ehcache:annotation-driven cache-manager="ehCacheManager" default-cache-key-generator="daoCacheKeyGenerator" />
<bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" />
<bean id="daoCacheKeyGenerator" class="myapp.dao.support.DaoCacheKeyGenerator" />
DaoCacheKeyGenerator.java
public class DaoCacheKeyGenerator implements CacheKeyGenerator<DaoCacheKey> {
@Override
public DaoCacheKey generateKey(MethodInvocation methodInvocation) {
Class<?> clazz = methodInvocation.getThis().getClass().getInterfaces()[0];
Method method = methodInvocation.getMethod();
String methodName = method.getName();
Class<?>[] parameterClasses = method.getParameterTypes();
return new DaoCacheKey(clazz, methodName, parameterClasses);
}
@Override
public DaoCacheKey generateKey(Object... data) {
return null;
}
}
DaoCacheKey.java
public class DaoCacheKey implements Serializable {
private static final long serialVersionUID = 338466521373614710L;
private Class<?> clazz;
private String methodName;
private Class<?>[] parameterClasses;
public DaoCacheKey(Class<?> clazz, String methodName, Class<?>[] parameterClasses) {
this.clazz = clazz;
this.methodName = methodName;
this.parameterClasses = parameterClasses;
}
@Override
public boolean equals(Object obj) { // <-- breakpoint
if (obj instanceof DaoCacheKey) {
DaoCacheKey other = (DaoCacheKey) obj;
if (clazz.equals(other.clazz)) {
// if @TriggersRemove, reset any cache generated by a find* method of the same DomainDao
boolean removeCache = !methodName.startsWith("find") && other.methodName.startsWith("find");
// if @Cacheable, check if the result has been previously cached
boolean getOrCreateCache = methodName.equals(other.methodName) && Arrays.deepEquals(parameterClasses, other.parameterClasses);
return removeCache || getOrCreateCache;
}
}
return false;
}
@Override
public int hashCode() { // <-- breakpoint
return super.hashCode();
}
}
上述问题DaoCacheKey
,就是equals
方法得到从来没有所谓的(程序一直没有休息至少),但hashCode
人做,所以不能得到应用的算法。
题
有没有人已经成功这样的缓存? 如果是的话怎么样? 请问我的尝试是相关的? 如果是,如何使equals
调用的方法,而不是hashCode
一个? 通过扩展现有CacheKeyGenerator
? 如果有,是哪一个?