Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring缓存——can';t获取数据列表_Java_Spring_Caching_Ehcache - Fatal编程技术网

Java Spring缓存——can';t获取数据列表

Java Spring缓存——can';t获取数据列表,java,spring,caching,ehcache,Java,Spring,Caching,Ehcache,我无法通过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.e

我无法通过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;
}
// ...
}
@实体
@表(name=“categories”)
公共类类别实现可序列化{
@身份证
@GeneratedValue(策略=GenerationType.IDENTITY)
私人长类别;
@列(nullable=false)
私有字符串名称;
私人长级别;
私人长父ID;
@ManyToMany(fetch=FetchType.EAGER,mappedBy=“categories”)
私人固定职位;
@manytone(fetch=FetchType.LAZY)
@JoinColumn(name=“userId”)
私人用户;
公共类别(){
}
公共类别(用户、长级别、字符串名称、长父ID){
this.user=用户;
这个水平=水平;
this.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);
}
}
@服务
@存储库
@交易的
公共类CategoryServiceImpl实现CategoryService{
@资源
私人分类报告分类报告;
@凌驾
@CachePut(value=“category”,key=“#category.categoryId”)
公共类别添加(类别){
返回类别repository.saveAndFlush(类别);
}
@凌驾
@可缓存(“类别”)
公共类别findById(长id){
返回categoryRepository.findOne(id);
}
@凌驾
@可缓存(“类别”)
公共类别findByParentIdThroughCategoryId(长prntId,长userId){
return categoryRepository.findByParentIdThroughCategoryId(prntId,userId);
}
@凌驾
@可缓存(“类别”)
公共列表findByParentId(长prntId,长userId){
return categoryRepository.findByParentId(prntId,userId);
}
@凌驾
@可缓存(“类别”)
公共列表findAll(长用户ID){
返回categoryRepository.findAll(userId);
}
@凌驾
@缓存逐出(“类别”)
公共无效删除(长id){
categoryRepository.delete(id);
}
}
当添加一个类别,然后尝试将其用于查看具有用户findByParentId/findall的所有类别时,它将返回一个空列表

[]

它有一些功能,例如缓存类别,然后获取类别列表,我尝试获取ID为11的类别--findById,并发现:

ru.mrchebik.model。Category@f0b8277


您的设置在Ehcache和Spring级别都存在许多问题

对于Ehcache,磁盘大小不应小于堆大小。有一段时间,ehcache中的分层模型意味着所有条目都必须在磁盘层中,因此使用此设置可以有效地人为地将堆大小限制为磁盘大小

在Spring方面,您使用单个缓存来保存不同的内容,但它们会发生冲突

  • 您的
    @CachePut
    将存储单个类别,映射到一个
    键,该键与您的
    @Cacheable
    findById
    上的功能相匹配
  • 但是,这将与
    findAll
    上的
    @Cacheable
    冲突,后者也使用
    作为键,但缓存
    列表
    。因此,
    userId
    categoryId
    之间的任何冲突都可能导致客户端代码中出现意外的类强制转换异常-期望
    列表
    获得
    类别
    或相反
  • 最后,您有两种方法,它们将两个
    long
    作为参数,但在缓存配置相同的情况下执行不同的服务调用。该服务调用再次返回不相交的类型-
    List
    Category
    。这将导致与前一点类似的问题
另外,我的印象是,您希望单条目缓存能够神奇地将条目附加到匹配列表中。这是不会发生的


我强烈建议您查看缓存需求,更好地理解Spring抽象提供了什么。。。您的设置在Ehcache和Spring级别都存在许多问题

对于Ehcache,磁盘大小不应小于堆大小。有一段时间,ehcache中的分层模型意味着所有条目都必须在磁盘层中,因此使用此设置可以有效地人为地将堆大小限制为磁盘大小

在Spring方面,您使用单个缓存来保存不同的内容,但它们会发生冲突

  • 您的
    @CachePut
    将存储单个类别,映射到一个
    键,该键与您的
    @Cacheable
    findById
    上的功能相匹配
  • 但是,这将与
    findAll
    上的
    @Cacheable
    冲突,后者也使用
    作为键,但缓存
    列表
    。因此,
    userId
    categoryId
    之间的任何冲突都可能导致客户端代码中出现意外的类强制转换异常-期望
    列表
    获得
    类别
    或相反
  • 最后,您有两种方法,它们将两个
    long
    作为参数,但在缓存配置相同的情况下执行不同的服务调用。该服务调用再次返回不相交的类型-
    List
    Category
    。这将导致与前一点类似的问题
另外,我的印象是,您希望单条目缓存能够神奇地将条目附加到匹配列表中。这是不会发生的


我强烈建议您查看缓存需求,更好地理解Spring抽象提供了什么。。。请首先确认
categoryRepository.findAll(userId)
categoryRepository.findByParentId(prntId,userId)
在直接调用时返回一些内容。另一个不相关的评论是,您的
CategoryServiceImpl
类不应使用
@Repository
进行注释,只有您的
CategoryRepository
应该进行注释。此方法返回值。如果不成功,我会