标签: spring-cache

Spring @Cacheable如何配置复杂的key

我有一个返回列表的方法,我想根据传递的参数缓存它。参数是 4 和整数,我如何使用 SpEL 对其进行配置?我正在使用 spring 版本 4.0.6.RELEASE。

java spring spring-el spring-cache

5
推荐指数
1
解决办法
2万
查看次数

是否可以定义通用接口并使用Spring @CacheConfig?

受到这个答案的启发,我尝试执行以下操作

@NoRepositoryBean
public interface CacheableRepository<T, ID extends Serializable>
    extends JpaRepository<T, ID>, JpaSpecificationExecutor<T>,       PagingAndSortingRepository<T, ID> {

    @Cacheable()
    T findOne(ID id);

    @Cacheable()
    List<T> findAll();

   @Cacheable()
   Page<T> findAll(Pageable pageable);

   @CacheEvict(allEntries = true)
   <S extends T> S save(S entity);

   @CacheEvict(allEntries = true)
   void delete(ID id);

}
Run Code Online (Sandbox Code Playgroud)

然后使用这个接口来定义Used存储库

 @CacheConfig(cacheNames={"uniqueCache"})
 public interface SomeObjectRepo extends    CacheableRepository<SomeObject, Long>  {

     @Cacheable(key = "someobject+#p0")
     List<SomeObject> findByType(Integer type);

    @Cacheable(key = "someobject+#p0")
    List<SomeObject> findByCategory(String field);

    @Cacheable(key="someobject+#p0.concat(#p1)")
    List<SomeObject> findByCategoryAndWizardType_id(String field,Integer id);

 }
Run Code Online (Sandbox Code Playgroud)

作品:

上面的缓存适用于findByType, findByCategory,findByCategoryAndWizardType_id

不起作用:

cacheable …

java spring spring-data-jpa spring-data-redis spring-cache

5
推荐指数
1
解决办法
1108
查看次数

在 Spring Boot 和 Redis 中使用 @Cacheable 与 Spring MongoDB 集成进行缓存

我想知道是否可以在 spring data mongo 存储库上使用 @Cacheable 注释。例如这样:

public interface UserRepository extends MongoRepository<User, String> {

    @Cacheable("byId")
    public interface UserRepository extends MongoRepository<User, String> {
        User findById(String id);
    }
}
Run Code Online (Sandbox Code Playgroud)

我想在接口类本身上执行此操作,并尽可能避免使用包装类。另外,是否有关于如何使用java配置(而不是xml)为redis进行缓存配置的示例?

redis spring-cache spring-mongodb

5
推荐指数
1
解决办法
3980
查看次数

javanica @HystrixCommand 和 spring @Cacheable 执行顺序

在 Spring 应用程序(不是 spring-boot)中,我在同一个 bean 方法上使用 javanica @HystrixCommand 注释和 spring cache @Cacheable 注释,Spring 在 Hystrix 建议之前执行缓存建议。这就是我在等待的,但对我来说,没有任何配置的缓存建议和 hystrix 建议在 spring 中具有相同的顺序:LOWEST_PRECEDENCE。

我不知道是什么产生了这个订单:它是在某处定义的还是未定义的订单,我很幸运能有好的订单?必须做些什么来确保缓存建议在 Hystrix 建议之前执行(在缓存上设置顺序:在 LOWEST_PRECEDENCE 之前设置注释驱动?)

这是我的代码示例:

...
import org.springframework.cache.annotation.CacheConfig;

import org.springframework.cache.annotation.Cacheable;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
...


@Service
@Slf4j
@CacheConfig(cacheManager="myCacheManager")
public class MyRessourcesImpl implements MyRessources {
...
    @Override
    @HystrixCommand(commandProperties = {
            @HystrixProperty(name = "execution.isolation.strategy", value = "SEMAPHORE"),
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "10000") })
    @Cacheable("cacheName")  //from spring cache
    public Map<String, String> getTheMap() {
        ...
    }
...    
}
Run Code Online (Sandbox Code Playgroud)

使用此弹簧配置:

<bean id="myCache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" …
Run Code Online (Sandbox Code Playgroud)

spring spring-cache hystrix spring-cloud-netflix

5
推荐指数
1
解决办法
2147
查看次数

使用注解将所有返回的元素放入 Spring-Boot 缓存中

使用spring-boot及其缓存机制,是否可以自动将所有作为集合返回的实体一一存储到缓存中?

例如图片下面的 Repository 方法:

@Query("...")
List<Foo> findFooByBar(Bar bar);
Run Code Online (Sandbox Code Playgroud)

我想将它们一个接一个地插入到 Spring Cache 中,这意味着会有 N 个插入(列表中的每个元素一个)而不是一个(整个列表)。

例子:

@Query("...")
@CachePut(value = "foos", key = "result.each.id")
List<Foo> findFooByBar(Bar bar);
Run Code Online (Sandbox Code Playgroud)

java caching annotations spring-boot spring-cache

5
推荐指数
1
解决办法
4649
查看次数

使用Spring Cache实现多个缓存实现

我正在开发一个Spring Boot应用程序,我需要使用分布式(例如Hazelcast)和本地(例如Guava)缓存.有没有办法配置Spring Cache在使用时使用它们@Cacheable并根据缓存名称决定需要哪个实现?

我尝试为HZ和Guava创建一个配置来定义里面的缓存名称,但是Spring抱怨它无法找到应该由HZ处理的缓存名称.当我独家使用HZ或Guava时,它们起作用.

java spring caching spring-boot spring-cache

5
推荐指数
1
解决办法
9974
查看次数

自定义 CacheInterceptor 被默认 Spring 的 CacheInterceptor 覆盖

我已经实现了一个CacheInterceptor允许通过通配符逐出缓存的自定义:

public class CustomCacheInterceptor extends CacheInterceptor {

    private static final Logger LOGGER = LoggerFactory.getLogger(CustomCacheInterceptor.class);

    @Override
    protected void doEvict(Cache cache, Object key) {
        try {
            // evict cache
        } catch (RuntimeException ex) {
            getErrorHandler().handleCacheEvictError(ex, cache, key);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我正在努力让它工作:

@Configuration
public class CustomProxyCachingConfiguration extends ProxyCachingConfiguration {

    private static final Logger LOGGER = LoggerFactory.getLogger(CustomProxyCachingConfiguration.class);

    @Bean
    public CacheInterceptor cacheInterceptor() {
        LOGGER.info("Creating custom cache interceptor");

        CacheInterceptor interceptor = new CustomCacheInterceptor();
        interceptor.setCacheOperationSources(cacheOperationSource());
        if (this.cacheResolver != null) {
            interceptor.setCacheResolver(this.cacheResolver);
        } else if (this.cacheManager …
Run Code Online (Sandbox Code Playgroud)

java spring spring-boot spring-cache

5
推荐指数
1
解决办法
748
查看次数

如何在春季每 30 分钟更新一次缓存?

我有以下声明:

@Cacheable("books")
public Book findBook(ISBN isbn) {...}
Run Code Online (Sandbox Code Playgroud)

但我想每 30 分钟更新一次缓存。我知道我可以创建 @Scheduled 作业来调用带注释的方法@CacheEvict("books")

另外,我想在这种情况下,所有书籍都将被清除,但更可取的是仅更新陈旧数据(在 30 分钟前放入缓存中)

春天有什么可以促进实施的吗?

java spring caching spring-boot spring-cache

5
推荐指数
1
解决办法
2459
查看次数

如何在没有xml的情况下使用spring boot 2和ehcache 3?

现在我有以下配置:

@Configuration
@EnableCaching
public class EhcacheConfig {
    @Bean
    public CacheManager cacheManager() throws URISyntaxException {
        return new JCacheCacheManager(Caching.getCachingProvider().getCacheManager(
                getClass().getResource("/ehcache.xml").toURI(),
                getClass().getClassLoader()
        ));
    }
}
Run Code Online (Sandbox Code Playgroud)

它指的是以下 XML:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://www.ehcache.org/v3"
        xmlns:jsr107="http://www.ehcache.org/v3/jsr107"
        xsi:schemaLocation="
            http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.0.xsd
            http://www.ehcache.org/v3/jsr107 http://www.ehcache.org/schema/ehcache-107-ext-3.0.xsd">

    <cache alias="pow_cache">
        <key-type>org.springframework.cache.interceptor.SimpleKey</key-type>
        <value-type>java.lang.Double</value-type>
        <expiry>
            <ttl unit="seconds">15</ttl>
        </expiry>

        <listeners>
            <listener>
                <class>my.pack.CacheEventLogger</class>
                <event-firing-mode>ASYNCHRONOUS</event-firing-mode>
                <event-ordering-mode>UNORDERED</event-ordering-mode>
                <events-to-fire-on>CREATED</events-to-fire-on>
                <events-to-fire-on>EXPIRED</events-to-fire-on>
            </listener>
        </listeners>

        <resources>
            <heap unit="entries">2</heap>
            <offheap unit="MB">10</offheap>
        </resources>
    </cache>

</config>
Run Code Online (Sandbox Code Playgroud)

服务看起来像这样:

@Cacheable(value = "pow_cache", unless = "#pow==3||#result>100", condition = "#val<5")
public Double pow(int val, int pow) throws InterruptedException {
    System.out.println(String.format("REAL invocation myService.pow(%s, …
Run Code Online (Sandbox Code Playgroud)

java ehcache spring-boot spring-cache ehcache-3

5
推荐指数
2
解决办法
6056
查看次数

Spring 中的注解处理顺序(@Cacheable 和 @Timed)

我想@Cacheable@Timed(从千分尺)注释我的方法。但我希望@Timed仅在数据未计时的情况下应用。有没有办法做到这一点,以正确的顺序放置注释就足够了 - 那是什么顺序?

@Timed也在使用TimedAspect,不确定这是否相关。

现在我这样做:

@Cacheable
@Timed
public String getValue(long id) {
    ...
}
Run Code Online (Sandbox Code Playgroud)

我找不到任何@Cacheable可以讨论这个问题的文档。

java spring spring-cache micrometer

5
推荐指数
1
解决办法
204
查看次数