当前位置: 首页 > 知识库问答 >
问题:

Spring Boot@CacheEvket保存(可迭代的实体);

燕博文
2023-03-14

我想在Spring Boot 4应用程序中添加缓存。作为一个键,我想使用我的实体的id(type Long),value是我的实体。我的存储库扩展了Crudepository。我缓存findOne方法的数据:

public interface SampleEntityService extends CrudRepository<SampleEntity, Long> {
@Cacheable(key = "#a0", value = "findOne")
SampleEntity findOne(Long id);

所以我想为实体的方法保存迭代无效缓存(要一致):

@CacheEvict(value = FIND_ONE, key = "#entities.id")
<S extends SampleEntity> Iterable<S> save(Iterable<S> entities);

但当我把这种方法称为:

sampleEntityService.save(ImmutableList.of(entity1));

我有个例外:

org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 10): Property or field 'id' cannot be found on object of type 'com.google.common.collect.SingletonImmutableList' - maybe not public?
at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:224)
at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:94)
at org.springframework.expression.spel.ast.PropertyOrFieldReference.access$000(PropertyOrFieldReference.java:46)
at org.springframework.expression.spel.ast.PropertyOrFieldReference$AccessorLValue.getValue(PropertyOrFieldReference.java:375)
at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:88)
at org.springframework.expression.spel.ast.SpelNodeImpl.getValue(SpelNodeImpl.java:120)
at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:242)
at org.springframework.cache.interceptor.ExpressionEvaluator.key(ExpressionEvaluator.java:115)
at org.springframework.cache.interceptor.CacheAspectSupport$CacheOperationContext.generateKey(CacheAspectSupport.java:621)
at org.springframework.cache.interceptor.CacheAspectSupport.performCacheEvict(CacheAspectSupport.java:406)
at org.springframework.cache.interceptor.CacheAspectSupport.processCacheEvicts(CacheAspectSupport.java:392)
at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:362)
at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:299)
at org.springframework.cache.interceptor.CacheInterceptor.invoke(CacheInterceptor.java:61)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
at com.sun.proxy.$Proxy75.save(Unknown Source)
at my.springboot.dao.sampleentity.SampleEntityCacheTest.testSampleEntityCaching_saveMany(SampleEntityCacheTest.java:119)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:73)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:73)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:224)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:83)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:68)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:163)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

-我如何修复这个EL表达式,使缓存中的所有项目无效,其中id在集合中?-如果这是不可能的,如何使这个方法不支持(不写具体的实现-我喜欢接口魔术)?

共有1个答案

景德海
2023-03-14

你不能像你提到的那样使用@cacheexecute(使用Iterable)。

当使用@CacheEvket的key属性时,SpEL应该指向要从指定缓存中删除的实体的单个id。

虽然在文档中没有直接提到(http://docs.spring.io/spring/docs/current/spring-framework-reference/html/cache.html#cache-annotations-evict)

 类似资料:
  • 我发现了这个关于使用限定符进行可迭代到不可迭代映射的示例: 但是,如果我想从emails集合的第一个元素中提取一个特定的字段,例如,就像我处理code一样? 例如,我希望编写如下的映射:

  • 当一个对象实现了属性时,我们认为它是可迭代的。 一些内置的类型如Array,Map,Set,String,Int32Array,Uint32Array等都已经实现了各自的Symbol.iterator。 对象上的Symbol.iterator函数负责返回供迭代的值。 for..of 语句 for..of会遍历可迭代的对象,调用对象上的Symbol.iterator属性。 下面是在数组上使用for.

  • 但是这种方法在最小的输入数据上特别慢,迭代版本需要<30秒,循环版本需要>3分钟。 我想flink无法创建执行循环的最佳计划。 有什么我应该尝试的变通方法吗?是否可以对flink进行一些修改,以便在hadoop等上保存部分结果?

  • 在Java8中,我们有类stream ,奇怪的是,它有一个方法 所以您希望它实现interface Iterable ,这恰恰需要这个方法,但事实并非如此。 当我想使用foreach循环对流进行迭代时,我必须执行如下操作 我是不是漏了什么?

  • 本文向大家介绍springboot hazelcast缓存中间件的实例代码,包括了springboot hazelcast缓存中间件的实例代码的使用技巧和注意事项,需要的朋友参考一下 缓存来了 在dotnet平台有自己的缓存框架,在java springboot里当然了集成了很多,而且缓存的中间件也可以进行多种选择,向 redis , hazelcast 都是分布式的缓存中间件,今天主要说一下后者

  • 我最近将swagger UI从更改为。 Swagger在上工作得非常好。 但是在上,我得到了以下错误。