gpt4 book ai didi

java - Spring Cache——无法获取数据列表

转载 作者:行者123 更新时间:2023-12-02 03:00:55 24 4
gpt4 key购买 nike

我无法通过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

最佳答案

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

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

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

  • 您的@CachePut将存储单个类别,映射到 long与您的 @Cacheable 相匹配的 key 上findById是的。
  • 但是,这会与 @Cacheable 冲突上findAll它还使用 long作为键,但缓存 List<Category> 。因此 userId 之间的任何碰撞和categoryId可能会在客户端代码中导致意外的类转换异常 - 需要 List得到一个Category或相反。
  • 最后你有两个方法,需要两个 long作为参数,但在缓存配置相同的情况下执行不同的服务调用。该服务调用再次返回不相交的类型 - List<Category>Category 。这将导致与上一点类似的问题。

此外,我的印象是您希望单个条目缓存能够神奇地将条目附加到匹配列表中。这不会发生。

我强烈建议您检查您的缓存需求,并更好地了解 Spring 抽象提供了什么……以及它不提供什么。

关于java - Spring Cache——无法获取数据列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42375145/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com