Tag: spring cache

如何让Spring缓存存储ResponseBody而不是中间对象

我使用spring cache和this方法,它将查询的值作为JSON返回: @RequestMapping(“/getById”) @ResponseBody @Cacheable public HugeValue getHugeValueFromSlowFoo( @RequestParam(value = “id”, defaultValue = “”) String id ) { return Foo.getById( id ); } 这工作正常,HugeValue对象存储在缓存中(在本例中为Hazelcast)。 我想进一步改进这个,因为从HugeValue创建JSON的时间非常长。 我可以告诉spring cache缓存我对象的JSON-ified版本吗? 我使用Jackson和Spring Boot 1.2以及Spring 4.1

Spring MVC的ehcache或spring cache中最好的Cache实践是什么?

计划在基于Spring网络的应用程序中实现静态数据的缓存机制,任何人都可以解释哪个是最好的以及它是如何工作的? 的Ehcache Spring Cache

使用Spring Cache Abstraction的异步缓存更新

使用Spring的缓存抽象,如何在仍然返回旧条目的同时异步刷新条目? 我正在尝试使用Spring的缓存抽象来创建一个缓存系统,在相对较短的“软”超时后,缓存条目可以进行刷新。 然后,在查询它们时,返回缓存的值,并启动异步更新操作以刷新条目。 我也会 Guava的缓存构建器允许我指定缓存中的条目应在一定时间后刷新。 然后可以使用异步实现覆盖缓存加载器的reload()方法,允许返回过时的缓存值,直到检索到新的缓存值。 但是,spring缓存似乎不使用底层Guava缓存的CacheLoader 是否可以使用Spring的缓存抽象来进行这种异步缓存刷新? 编辑澄清:使用Guava的CacheBuilder,我可以使用refreshAfterWrite()来获取我想要的行为。 例如来自Guava Caches解释 : LoadingCache graphs = CacheBuilder.newBuilder() .maximumSize(1000) .refreshAfterWrite(1, TimeUnit.MINUTES) .build( new CacheLoader() { public Graph load(Key key) { // no checked exception return getGraphFromDatabase(key); } public ListenableFuture reload(final Key key, Graph prevGraph) { if (neverNeedsRefresh(key)) { return Futures.immediateFuture(prevGraph); } else { // asynchronous! ListenableFutureTask task […]

Spring Boot中的ACL安全性

我在Spring Boot应用程序中通过Java配置设置ACL时遇到问题。 我创建了一个小项目来重现问题。 我尝试过几种不同的方法。 我遇到的第一个问题是EhCache,在我修复之后(我假设我做了)我再也无法登录了,看起来所有的数据都消失了。 有4个类具有不同的配置: ACLConfig1.class ACLConfig2.class ACLConfig3.class ACLConfig4.class 所有@PreAuthorize和@PostAuthorize注释都按预期工作,但hasPermission除外。 控制器拥有4个端点:一个用于用户,一个用于管理员,一个用于公共,最后一个给我带来麻烦@PostAuthorize(“hasPermission(returnObject,’administration’)”) 我很确定DB中的插入是正确的。 这个类是四个中的一个,也是我尝试过的最后一个类: @Configuration @EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true) public class ACLConfig4 { @Autowired DataSource dataSource; @Bean public EhCacheBasedAclCache aclCache() { return new EhCacheBasedAclCache(aclEhCacheFactoryBean().getObject(), permissionGrantingStrategy(), aclAuthorizationStrategy()); } @Bean public EhCacheFactoryBean aclEhCacheFactoryBean() { EhCacheFactoryBean ehCacheFactoryBean = new EhCacheFactoryBean(); ehCacheFactoryBean.setCacheManager(aclCacheManager().getObject()); ehCacheFactoryBean.setCacheName(“aclCache”); return ehCacheFactoryBean; } @Bean […]

使用Spring KeyGenerator生成唯一的缓存键不起作用

当我的缓存键在Spring中使用@Cacheable注释发生冲突时,我遇到了问题。 例如,使用以下两种方法: @Cacheable(“doOneThing”) public void doOneThing(String name) { // do something with name } @Cacheable(“doAnotherThing”) public void doAnotherThing(String name) { // do some other thing with name } 这是我的缓存配置,其中我添加了一个keyGenerator和一个cacheManager bean: @Configuration @EnableCaching public class CacheConfig { @Bean public JedisConnectionFactory redisConnectionFactory() { return new JedisConnectionFactory(); } @Bean public RedisTemplate redisTemplate(RedisConnectionFactory cf) { RedisTemplate redisTemplate = new […]

Spring Boot – 如何在开发期间禁用@Cachable?

我正在寻找两件事: 如何使用Spring启动“dev”配置文件禁用开发期间的所有缓存。 在application.properties中,没有seam作为一般设置来关闭它。 什么是最简单的方法? 如何禁用特定方法的缓存? 我试着像这样使用SpEl: @Cacheable(value = “complex-calc”, condition = “#{${spring.profiles.active} != ‘dev’}”) public String someBigCalculation(String input){ … } 但我可以让它发挥作用。 关于SO的问题有几个问题,但它们指的是XML配置或其他东西,但我使用的是Spring Boot 1.3.3,它使用了自动配置。 我不想让事情过于复杂。

如何在spring cache java中配置多个缓存管理器

我希望在我的Web应用程序中配置多个Spring缓存管理器,并且我可以在项目的不同位置使用不同的缓存管理器。 有没有办法做到这一点。

使用Ehcache弹出@Cacheable,spel为有效对象找到null

我有一个类似的问题 ,但有时它的工作原理。 描述的错误只会偶尔发生一次。 我使用的是spring 3.2.5和ehcache 2.6.5。 exception跟踪: org.springframework.expression.spel.SpelEvaluationException: EL1007E:(pos 0): Field or property ‘applicationID’ cannot be found on null at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:213) at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:85) at org.springframework.expression.spel.ast.PropertyOrFieldReference.access$000(PropertyOrFieldReference.java:43) at org.springframework.expression.spel.ast.PropertyOrFieldReference$AccessorLValue.getValue(PropertyOrFieldReference.java:346) at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:82) at org.springframework.expression.spel.ast.SpelNodeImpl.getValue(SpelNodeImpl.java:93) at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:89) at org.springframework.cache.interceptor.ExpressionEvaluator.key(ExpressionEvaluator.java:95) at org.springframework.cache.interceptor.CacheAspectSupport$CacheOperationContext.generateKey(CacheAspectSupport.java:452) at org.springframework.cache.interceptor.CacheAspectSupport.inspectCacheables(CacheAspectSupport.java:281) at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:199) at org.springframework.cache.interceptor.CacheInterceptor.invoke(CacheInterceptor.java:66) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204) at com.sun.proxy.$Proxy85.getMailOrigin(Unknown Source) at com.myApplication.MailFilterServiceImpl.isValid(ApplicationServiceImpl.java:134) 我的缓存代码如下: MailFilterServiceImpl @Cacheable(value=”mailClientsCache”, […]

如何在春季启动时加载@Cache?

我正在使用spring-cache来改进数据库查询,其工作正常如下: @Bean public CacheManager cacheManager() { return new ConcurrentMapCacheManager(“books”); } @Cacheable(“books”) public Book getByIsbn(String isbn) { return dao.findByIsbn(isbn); } 但现在我想在启动时预先填充完整的书籍缓存。 这意味着我想调用dao.findAll()并将所有值放入缓存中。 此例程不应仅定期安排。 但是,在使用@Cacheable时,如何显式填充缓存?

带有项目/实体集合的Spring Cache

我正在使用Spring Cache,我在其中传递了一组键,返回是一个实体列表。 我想让缓存框架理解返回列表中的每个元素都要使用相应的代码进行缓存。 目前,似乎关键是整个列表,如果我在后续调用中缺少一个键,它将尝试再次重新加载整个集合。 @Override @Cacheable(value = “countries”) public List getAll(Listcodes) { return countryDao.findAllInCodes(codes); } 另一种可能性是返回是一个映射,同样我希望缓存足够智能,只能查询以前从未查询过的项目,也用它的密钥缓存每个项目。 @Override @Cacheable(value = “countries”) public Map getAllByCode(Listcodes) { return countryDao.findAllInCodes(codes); } 假设国家类看起来像这样: class Country{ String code; String fullName; long id; … // getters setters constructurs etc.. } 这可以用Spring Cache吗?