简体   繁体   English

在 mybatis 中缓存查询——可能的方法

[英]Caching queries in mybatis - possible ways

I have many queries written in mybatis.我有很多用 mybatis 编写的查询。 What are possible ways to cache answers ?缓存答案的可能方法是什么? For example, SELECT * FROM someFun(#{someId}) or SELECT count(*) FROM someFun(#{someId})例如, SELECT * FROM someFun(#{someId})SELECT count(*) FROM someFun(#{someId})

In other words, I search for way which cache only these queries which I would like to cache (something like annotations, maybe?) Of course, caching should depend on parameters, eq the some queries in XML , but with different parameters should be newly recomputed, for example:换句话说,我搜索只缓存我想缓存的这些查询的方式(比如注释,也许?)当然,缓存应该取决于参数,例如XML的一些查询,但不同的参数应该是新的重新计算,例如:

SELECT count(*) FROM someFun(2) -- compute
SELECT count(*) FROM someFun(2) -- cache
SELECT count(*) FROM someFun(2) -- cache
SELECT count(*) FROM someFun(3) -- compute
SELECT count(*) FROM someFun(2) -- cache
SELECT count(*) FROM someFun(3) -- cache

....
10 minuts
....
SELECT count(*) FROM someFun(3) -- compute
SELECT count(*) FROM someFun(2) -- compute
SELECT count(*) FROM someFun(3) -- cache
SELECT count(*) FROM someFun(2) -- cache

Any ideas ?有什么想法吗?

You can choose which queries to cache as well as interval for when you want your cache to flush .您可以选择要缓存的查询以及您希望缓存刷新的时间间隔。

Both options are documented.这两个选项都记录在案。 Look for useCache on select and flushInterval on cache在选择上查找 useCache 并在缓存上查找 flushInterval

You can use MyBatis 3 cache service.您可以使用 MyBatis 3 缓存服务。 Just add the below tag in your mapper file.只需在您的映射器文件中添加以下标签。

<cache/>

You can customize the way your cache operates.您可以自定义缓存的运行方式。 In your case if you want your cache to flush after 10 minutes add the following code in your mapper file.在您的情况下,如果您希望缓存在 10 分钟后刷新,请在映射器文件中添加以下代码。

<cache flushInterval="60000"/>

There are other customization options available which you can check at MyBatis Cache还有其他可用的自定义选项,您可以在MyBatis Cache 中查看

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM