【问题标题】:spring boot data redis integrated with spring data jpaspring boot data redis 与 spring data jpa 集成
【发布时间】:2017-09-22 17:40:15
【问题描述】:

我正在使用 spring boot 1.5.2.RELEASE 和 spring data redis 和 spring data jpa。

  1. 我想先从redis查询数据

  2. 如果数据无法从 redis 中获取,则从 mysql 中获取。

类似这样的:

获取方法

Object cacheValue = cache.get("key");
if(null != cacheValue){
    return cacheValue;
} else {
    Object dbValue = getFromInDb("key");
    cache.set("key", value);
    return dbValue;
}

删除方法

Object cacheValue = cache.get("key");
if(null != cacheValue){
    cache.delete("key");
    db.deleteByKey("key")
} else {
    db.deleteByKey("key")
}

我现在使用spring aop我可以完成工作。我想知道我使用 spring data redis repository 是否可以做同样的事情以及如何做?

提前致谢。

【问题讨论】:

  • 您是否尝试过在您的服务中使用@Cacheable@CacheDelete 注释? Spring Data Redis 附带了一个 Cache 实现,它可能会利用您的方法。
  • 你有一些简单的演示吗?谢谢。

标签: spring-boot spring-data-jpa spring-aop spring-data-redis


【解决方案1】:

Spring Data Redis 应该可以满足你的要求。我的建议是使用 @Cacheable 和方法上的其他注释让内存缓存中的自动配置工作。通过在依赖项中包含 Redis 来插入 Redis。您可能会遇到序列化问题,但这是一个不同的问题。
Spring Boot Caching Guide.使用内存缓存真的就是这么简单。您可以在方法中添加一些打印语句来验证它们何时运行以及何时缓存结果。
More useful spring boot cache info

【讨论】:

    猜你喜欢
    • 2017-05-09
    • 2021-09-13
    • 2019-04-29
    • 2020-08-18
    • 1970-01-01
    • 2014-07-02
    • 2017-11-29
    • 2020-07-05
    • 1970-01-01
    相关资源
    最近更新 更多