【发布时间】:2017-07-11 12:54:07
【问题描述】:
我无法通过 ehcache 获取数据列表。
servlet 上下文:
<cache:annotation-driven />
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"
p:cacheManager-ref="ehcache"/>
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
p:configLocation="/WEB-INF/spring/appServlet/ehcache.xml" p:shared="true" />
ehcache.xml
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd"
updateCheck="true" monitoring="autodetect" dynamicConfig="true">
<cache name="category"
maxEntriesLocalHeap="5000"
maxEntriesLocalDisk="1000"
eternal="false"
diskSpoolBufferSizeMB="20"
timeToIdleSeconds="200"
timeToLiveSeconds="500"
memoryStoreEvictionPolicy="LFU"
transactionalMode="off">
<persistence strategy="localTempSwap"/>
</cache>
我有一个类别的实体:
@Entity
@Table(name = "categories")
public class Category implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long categoryId;
@Column(nullable = false)
private String name;
private long level;
private long parentId;
@ManyToMany(fetch = FetchType.EAGER, mappedBy = "categories")
private Set<Post> posts;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "userId")
private User user;
public Category() {
}
public Category(User user, long level, String name, long parentId) {
this.user = user;
this.level = level;
this.name = name;
this.parentId = parentId;
}
// ...
}
然后,我尝试缓存这些方法:
@Service
@Repository
@Transactional
public class CategoryServiceImpl implements CategoryService {
@Resource
private CategoryRepository categoryRepository;
@Override
@CachePut(value = "category", key = "#category.categoryId")
public Category add(Category category) {
return categoryRepository.saveAndFlush(category);
}
@Override
@Cacheable("category")
public Category findById(long id) {
return categoryRepository.findOne(id);
}
@Override
@Cacheable("category")
public Category findByParentIdThroughCategoryId(long prntId, long userId) {
return categoryRepository.findByParentIdThroughCategoryId(prntId, userId);
}
@Override
@Cacheable("category")
public List<Category> findByParentId(long prntId, long userId) {
return categoryRepository.findByParentId(prntId, userId);
}
@Override
@Cacheable("category")
public List<Category> findAll(long userId) {
return categoryRepository.findAll(userId);
}
@Override
@CacheEvict("category")
public void remove(long id) {
categoryRepository.delete(id);
}
}
添加一个分类,然后尝试带它查看所有有用户findByParentId/findall的分类,返回一个空列表
[]
这有关系,例如我缓存类别,然后获取类别列表,我尝试获取 ID 为 11 的类别 -- findById,结果:
ru.mrchebik.model.Category@f0b8277
【问题讨论】:
-
请先确认
categoryRepository.findAll(userId)和categoryRepository.findByParentId(prntId, userId)在直接调用时会返回一些内容。另一个不相关的评论是你的CategoryServiceImpl类不应该用@Repository注释,只有你的CategoryRepository应该。 -
这些方法是返回值。如果不成功,我不会做缓存。嗯...我总是这样做@Repository 并且所有工作。我已将
@Repository移至CategoryRepository,谢谢。 -
@artemisian 我也尝试这样做:
@Cacheable(cacheNames = "category", key = "#userId")但它没有任何改变 -
您是否有相同值的 userId 和 categoryId?请将 findAll 的 Cacheable 注解替换为
@Cacheable(cacheNames = "category", key = "'findAll-'.concat(#userId"))",然后重试。 -
抱歉有错别字,应该是
@Cacheable(cacheNames = "category", key = "'findAll-'.concat(#userId)"
标签: java spring caching ehcache