欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

MyBatis缓存与Apache Ignite的陷阱

程序员文章站 2024-03-02 16:04:28
...

一周前,MyBatis和Apache ignite 宣布支持apache ignite作为MyBatis缓存(L2缓存)。

从技术上讲,MyBatis支持两个级别的缓存:

  1. 本地缓存,默认情况下始终启用
  2. L2缓存,可选

随着Apache Ignite项目的各种功能Swift发展,在本博文中,我们将详细研究MyBatis支持。

第二级缓存存储实体数据,但不存储实体或对象本身。 数据以“序列化”格式存储,看起来像哈希图,其中键是实体ID,而值是原始值列表。

这是一个示例,其缓存条目在Apache ignite中的样子:

MyBatis缓存与Apache Ignite的陷阱
哪里:

  • 快取金钥
    CacheKey [idHash=1499858, hash=2019660929, checksum=800710994, count=6, multiplier=37, hashcode=2019660929, updateList=[com.blu.ignite.mapper.UserMapper.getUserObject, 0, 2147483647, SELECT * FROM all_objects t where t.OBJECT_TYPE='TABLE' and t.object_name=?, USERS, SqlSessionFactoryBean]]
  • 值类别java.util.ArrayList
  • 缓存值
    [UserObject [idHash=243119413, hash=1658511469, owner=C##DONOTDELETE, object_type=TABLE, object_id=94087, created=Mon Feb 15 13:59:41 MSK 2016, object_name=USERS]]

对于示例,我从Oracle数据库中选择了“ all_objects”对象和以下查询

SELECT count(*) FROM all_objects;

SELECT * FROM all_objects t where t.OBJECT_TYPE='TABLE' and t.object_name='EMP';

SELECT * FROM all_objects t where t.OBJECT_TYPE='TABLE';

就我而言,给定的查询执行时间平均约为660毫秒。

SELECT count(*) FROM all_objects;

接下来的下一个查询执行时间超过700ms:

SELECT t.object_type, count(*) FROM all_objects t group by t.OBJECT_TYPE;

让我们添加apache ignite作为第二级缓存并检查结果。 如果您想知道如何使用spring和myBatis安装和配置apache ignite,请参阅我以前的博客文章 此外,您可以在github存储库中找到所有源代码。

首先,让我们在项目中添加myBatis maven依赖项。

<dependency>
    <groupId>org.mybatis.caches</groupId>
    <artifactId>mybatis-ignite</artifactId>
    <version>1.0.0-beta1</version>
</dependency>

然后,只需在映射器XML中指定它,如下所示

<mapper namespace="com.blu.ignite.mapper.UserMapper">

    <cache type="org.mybatis.caches.ignite.IgniteCacheAdapter" />

    <select id="getUserObject" parameterType="String" resultType="com.blu.ignite.dto.UserObject" useCache="true">
        SELECT * FROM all_objects t where t.OBJECT_TYPE='TABLE' and t.object_name=#{objectName}
    </select>
    <select id="getAllObjectsTypeByGroup" parameterType="String" resultType="com.blu.ignite.dto.UobjectGroupBy" useCache="true">
        SELECT t.object_type, count(*) as cnt FROM all_objects t group by t.OBJECT_TYPE
    </select>

    <select id="allObjectCount" parameterType="String" resultType="String" useCache="true">
        SELECT count(*) FROM all_objects
    </select>
</mapper>

我也有以下的Java映射器:

public interface UserMapper {
    User getUser( String id);
    List getUniqueJob();
    UserObject getUserObject(String objectName);
    String allObjectCount();
    List getAllObjectsTypeByGroup();
}

和Web服务,如下所示:

@WebService(name = "BusinessRulesServices",
        serviceName="BusinessRulesServices",
        targetNamespace = "http://com.blu.rules/services")
public class WebServices {
    private UserServices userServices;

    @WebMethod(operationName = "getUserName")
    public String getUserName(String userId){
        User user = userServices.getUser(userId);
        return user.getuName();
    }
    @WebMethod(operationName = "getUserObject")
    public UserObject getUserObject(String objectName){
        return userServices.getUserObject(objectName);
    }
    @WebMethod(operationName = "getUniqueJobs")
    public List getUniqueJobs(){
        return userServices.getUniqueJobs();
    }
    @WebMethod(exclude = true)
    public void setDao(UserServices userServices){
        this.userServices = userServices;
    }
    @WebMethod(operationName = "allObjectCount")
    public String allObjectCount(){
        return userServices.allObjectCount();
    }
    @WebMethod(operationName = "getAllObjectsTypeCntByGroup")
    public List getAllObjectsTypeCntByGroup(){
        return userServices.getAllObjectCntbyGroup();
    }

}

如果我将在soupUI中调用Web方法'getAllObjectsTypeCntByGroup',则第一次它将获得非常高的响应时间,大约为1700 ms,因为结果不在缓存中。 从第二次开始,响应时间将为〜4到〜5 ms。

首次调用网络方法如下所示:

MyBatis缓存与Apache Ignite的陷阱
第二次或更晚调用Web方法的响应时间

MyBatis缓存与Apache Ignite的陷阱
在apache中,ignite缓存条目将如下所示:

MyBatis缓存与Apache Ignite的陷阱

MyBatis缓存与Apache Ignite的陷阱

  • 快取金钥
    CacheKey [idHash=46158416, hash=1558187086, checksum=2921583030, count=5, multiplier=37, hashcode=1558187086, updateList=[com.blu.ignite.mapper.UserMapper.getAllObjectsTypeByGroup, 0, 2147483647, SELECT t.object_type, count(*) as cnt FROM all_objects t group by t.OBJECT_TYPE, SqlSessionFactoryBean]]
  • 值类别java.util.ArrayList
  • 缓存值
    [UobjectGroupBy [idHash=2103707742, hash=1378996400, cnt=1, object_type=EDITION], UobjectGroupBy [idHash=333378159, hash=872886462, cnt=444, object_type=INDEX PARTITION], UobjectGroupBy [idHash=756814918, hash=1462794064, cnt=32, object_type=TABLE SUBPARTITION], UobjectGroupBy [idHash=931078572, hash=953621437, cnt=2, object_type=CONSUMER GROUP], UobjectGroupBy [idHash=1778706917, hash=1681913927, cnt=256, object_type=SEQUENCE], UobjectGroupBy [idHash=246231872, hash=1764800190, cnt=519, object_type=TABLE PARTITION], UobjectGroupBy [idHash=1138665719, hash=1030673983, cnt=4, object_type=SCHEDULE], UobjectGroupBy [idHash=232948577, hash=1038362844, cnt=1, object_type=RULE], UobjectGroupBy [idHash=1080301817, hash=646054631, cnt=310, object_type=JAVA DATA], UobjectGroupBy [idHash=657724550, hash=1248576975, cnt=201, object_type=PROCEDURE], UobjectGroupBy [idHash=295410055, hash=33504659, cnt=54, object_type=OPERATOR], UobjectGroupBy [idHash=150727006, hash=499210168, cnt=2, object_type=DESTINATION], UobjectGroupBy [idHash=1865360077, hash=727903197, cnt=9, object_type=WINDOW], UobjectGroupBy [idHash=582342926, hash=1060308675, cnt=4, object_type=SCHEDULER GROUP], UobjectGroupBy [idHash=1968399647, hash=1205380883, cnt=1306, object_type=PACKAGE], UobjectGroupBy [idHash=1495061270, hash=1345537223, cnt=1245, object_type=PACKAGE BODY], UobjectGroupBy [idHash=1328790450, hash=1823695135, cnt=228, object_type=LIBRARY], UobjectGroupBy [idHash=1128429299, hash=1267824468, cnt=10, object_type=PROGRAM], UobjectGroupBy [idHash=760711193, hash=1240703242, cnt=17, object_type=RULE SET], UobjectGroupBy [idHash=317487814, hash=61657487, cnt=10, object_type=CONTEXT], UobjectGroupBy [idHash=1079028994, hash=1960895356, cnt=229, object_type=TYPE BODY], UobjectGroupBy [idHash=276147733, hash=873140579, cnt=44, object_type=XML SCHEMA], UobjectGroupBy [idHash=24378178, hash=1621363993, cnt=1014, object_type=JAVA RESOURCE], UobjectGroupBy [idHash=1891142624, hash=90282027, cnt=10, object_type=DIRECTORY], UobjectGroupBy [idHash=902107208, hash=1995006200, cnt=593, object_type=TRIGGER], UobjectGroupBy [idHash=142411235, hash=444983119, cnt=14, object_type=JOB CLASS], UobjectGroupBy [idHash=373966405, hash=1518992835, cnt=3494, object_type=INDEX], UobjectGroupBy [idHash=580466919, hash=1394644601, cnt=2422, object_type=TABLE], UobjectGroupBy [idHash=1061370796, hash=1861472837, cnt=37082, object_type=SYNONYM], UobjectGroupBy [idHash=1609659322, hash=1543110475, cnt=6487, object_type=VIEW], UobjectGroupBy [idHash=458063471, hash=1317758482, cnt=346, object_type=FUNCTION], UobjectGroupBy [idHash=1886921697, hash=424653540, cnt=7, object_type=INDEXTYPE], UobjectGroupBy [idHash=1455482905, hash=1776171634, cnt=30816, object_type=JAVA CLASS], UobjectGroupBy [idHash=49819096, hash=2110362533, cnt=2, object_type=JAVA SOURCE], UobjectGroupBy [idHash=1916179950, hash=1760023032, cnt=10, object_type=CLUSTER], UobjectGroupBy [idHash=1138808674, hash=215713426, cnt=2536, object_type=TYPE], UobjectGroupBy [idHash=305229607, hash=340664529, cnt=23, object_type=JOB], UobjectGroupBy [idHash=1365509716, hash=623631686, cnt=12, object_type=EVALUATION CONTEXT]]

结论

使用L2缓存可以减少昂贵的数据库操作,在MyBatis中正确使用L2缓存可以将应用程序性能提高10到20倍。 内存数据网格中的Apache Ignite非常适合用于此目的,当然您也可以使用Hazelcash,EhCache或任何其他缓存工具。

翻译自: https://www.javacodegeeks.com/2016/03/pitfalls-mybatis-caches-apache-ignite.html