2017-04-25 235 views
1

我使用弹簧引导1.5.2.RELEASE和弹簧数据redis和弹簧数据jpa。弹簧引导数据redis集成弹簧数据jpa

  1. 我想,如果数据不能从Redis的foud,然后从MySQL从Redis的第一

  2. 查询数据。

是这样的:

get方法

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数据redis存储库可以做同样的事情,以及如何?

在此先感谢。

+0

您是否尝试过使用'@ Cacheable'和'@ CacheDelete'注释到您的服务? Spring Data Redis带有一个可以利用你的方法的Cache实现。 – mp911de

+0

你有一些简单的演示吗?谢谢。 – diligent

回答

1

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