当前位置: 移动技术网 > IT编程>开发语言>Java > 使用Redis和Java进行数据库缓存

使用Redis和Java进行数据库缓存

2020年01月15日  | 移动技术网IT编程  | 我要评论

使用redis和java探索数据库缓存,并查看一些不同的缓存策略。最近在优锐课学习收获颇多,记录下来大家一起进步!

为什么数据库缓存如此重要?

你在数据库中拥有的信息越多,随着时间的推移它将变得越慢。 即使是经过精心设计以支持许多并发请求的数据库管理系统,最终也会达到其极限。

数据库缓存是处理这些性能问题的最常见策略之一。缓存涉及将数据库查询的结果保存在更快,更易于访问的位置。正确完成缓存后,缓存将减少查询响应时间,减少数据库负载并降低成本。

但是,缓存也需要谨慎处理,因为它们实际上是在单独的位置中复制了你的另一份信息。保持数据库和缓存同步并保持最新状态可能是比你预期的棘手的挑战。在下一节中,我们将讨论一些最常见的数据库缓存策略。

有哪些不同的缓存策略?

手动缓存(也称为备用缓存策略)涉及对数据库和缓存的直接管理。你的应用程序在启动数据库查询之前检查缓存,并且在对数据库进行任何更改后都会更新缓存。

如果正确实施,手动缓存虽然有效,但可能非常繁琐,尤其是在需要查询多个数据库的情况下。由于这些原因,开发人员发明了许多替代的缓存策略。

read-through缓存策略

在read-through中,应用程序首先查询缓存以查看其所需的信息是否在内部。如果不是,它将从数据库中检索信息并使用它来更新缓存。缓存提供程序或缓存库负责查询和更新缓存的详细逻辑。

当应用程序反复请求相同的数据时,read-through策略最适合于繁重的工作负载:例如,一个新闻网站一遍又一遍地加载相同的文章。

read-through策略的一个缺点是,对高速缓存的第一个查询将始终导致未命中,因为保证了所请求的信息不会在内部。为了解决此问题,开发人员通常会提前用用户可能会请求的信息“预热”缓存。

write-through 缓存策略

在write-through缓存中,首先对缓存进行更新,然后对数据库进行更新。从应用程序到缓存以及从缓存到数据库都有一条直线。 与read-through缓存结合使用时,write-through策略可确保你的数据保持一致,从而无需手动进行高速缓存失效。

write-behind缓存策略

在write-behind缓存(也称为write-back缓存)中,应用程序首先将数据写入高速缓存。 经过一段时间的延迟后,高速缓存还将这些信息写入数据库。write-behind缓存最适合于繁重的写工作负载,即使出现一些故障和停机也可以表现良好。

 

使用redisson的基于java的redis缓存

redis是nosql数据库中最受欢迎的选项之一,它使用键值系统存储数据。redisson是java编程语言中redis的客户端库,可使用所有熟悉的java集合轻松访问redis功能。

redisson允许你将数据放置在外部存储的“地图”中。你可以使用此功能为数据库,web服务或任何其他数据源实现缓存。

redis中的read-through缓存

下面是一个java示例,说明如何在redis和redisson中使用read-through缓存。

如果请求的条目在缓存中不存在,它将由maploader对象加载:

 1 maploader<string, string> maploader = new maploader<string, string>() {
 2     @override
 3     public iterable<string> loadallkeys() {
 4         list<string> list = new arraylist<string>();
 5         statement statement = conn.createstatement();
 6         try {
 7             resultset result = statement.executequery("select id from student");
 8             while (result.next()) {
 9                 list.add(result.getstring(1));
10             }
11         } finally {
12             statement.close();
13         }
14         return list;
15     }
16     @override
17     public string load(string key) {
18         preparedstatement preparedstatement = conn.preparestatement("select name from student where id = ?");
19         try {
20             preparedstatement.setstring(1, key);
21             resultset result = preparedstatement.executequery();
22             if (result.next()) {
23                 return result.getstring(1);
24             }
25             return null;
26         } finally {
27             preparedstatement.close();
28         }
29     }
30 };

 

configuration example:

1 mapoptions<k, v> options = mapoptions.<k, v>defaults()
2                               .loader(maploader);
3 rmap<k, v> map = redisson.getmap("test", options);
4 // or
5 rmapcache<k, v> map = redisson.getmapcache("test", options);
6 // or with boost up to 45x times 
7 rlocalcachedmap<k, v> map = redisson.getlocalcachedmap("test", options);
8 // or with boost up to 45x times 
9 rlocalcachedmapcache<k, v> map = redisson.getlocalcachedmapcache("test", options);

 

redis 中的write-through缓存

下面是一个java示例,说明如何在redis和redisson中的redis中使用write-through缓存。

直到mapwriter对象更新了缓存和数据库后,缓存更新方法才会返回:

 1 mapwriter<string, string> mapwriter = new mapwriter<string, string>() {
 2     @override
 3     public void write(map<string, string> map) {
 4         preparedstatement preparedstatement = conn.preparestatement("insert into student (id, name) values (?, ?)");
 5         try {
 6             for (entry<string, string> entry : map.entryset()) {
 7                 preparedstatement.setstring(1, entry.getkey());
 8                 preparedstatement.setstring(2, entry.getvalue());
 9                 preparedstatement.addbatch();
10             }
11             preparedstatement.executebatch();
12         } finally {
13             preparedstatement.close();
14         }
15     }
16     @override
17     public void delete(collection<string> keys) {
18         preparedstatement preparedstatement = conn.preparestatement("delete from student where id = ?");
19         try {
20             for (string key : keys) {
21                 preparedstatement.setstring(1, key);
22                 preparedstatement.addbatch();
23             }
24             preparedstatement.executebatch();
25         } finally {
26             preparedstatement.close();
27         }
28     }
29 };

 

configuration example:

 1 mapoptions<k, v> options = mapoptions.<k, v>defaults()
 2                               .writer(mapwriter)
 3                               .writemode(writemode.write_through);
 4 rmap<k, v> map = redisson.getmap("test", options);
 5 // or
 6 rmapcache<k, v> map = redisson.getmapcache("test", options);
 7 // or with boost up to 45x times 
 8 rlocalcachedmap<k, v> map = redisson.getlocalcachedmap("test", options);
 9 // or with boost up to 45x times 
10 rlocalcachedmapcache<k, v> map = redisson.getlocalcachedmapcache("test", options);

 

redis 中的write-behind缓存

mapwriter接口还用于将更新异步提交到map对象(缓存)和外部存储(数据库)。所有地图更新都分批累积,并以定义的延迟异步写入。

writebehinddelay —批处理写入或删除操作的延迟。默认值为1000毫秒。

writebehindbatchsize —批处理的大小。每批包含“映射条目”写入或删除命令。默认值为50。

下面,我们看到一个redisson中基于redis的write-behind缓存实现的java配置示例:

 1 mapoptions<k, v> options = mapoptions.<k, v>defaults()
 2                               .writer(mapwriter)
 3                               .writemode(writemode.write_behind)
 4                               .writebehinddelay(5000)
 5                               .writebehindbatchsize(100);
 6 rmap<k, v> map = redisson.getmap("test", options);
 7 // or
 8 rmapcache<k, v> map = redisson.getmapcache("test", options);
 9 // or with boost up to 45x times 
10 rlocalcachedmap<k, v> map = redisson.getlocalcachedmap("test", options);
11 // or with boost up to 45x times 
12 rlocalcachedmapcache<k, v> map = redisson.getlocalcachedmapcache("test", options);

所有讨论的策略都可用于redisson中的rmap,rmapcache,rlocalcachedmap和rlocalcachedmapcache对象。使用后两个对象可以使redis中的读取操作快45倍。

感谢阅读!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网