Gdao v1.2.2:Go 语言高效 ORM 框架,缓存优化

gdao是一个全面的go持久层解决方案。主要目的在于 减少编程量,提高生产力,提高性能,支持多数据源整合操作,支持数据读写分离,制定持久层编程规范。 灵活运用 gdao,可以在持久层设计上,减少30%甚至50%以上的编程量,同时形成持久层的统一编程规范,减少持久层错误,同时易于维护和扩展。   gdao对于go语言,相当于 hibernate+ myBatis对于java语言
gdao完整地在go语言中实现 myBatis的核心功能,实现SQL与程序分离,实现强大的动态SQL功能

GitHub :  Gdao Repository

示例程序:  Gdaodemo

使用文档:  Gdaodoc

同类型框架性能压测数据《Gdao—orm框架性能压测 gdao+gorm+sqlx+原生sql执行》


Gdao 1.2.2 更新内容

  • 新增写过期缓存

通过 BindExpireWriteClass 或  BindExpireWriteClassWithCacheHandle 设置写过期缓存,当设置的类发生写操作时,将自动清除缓存数据 。

使用示例:

func TestExpireWrite(t *testing.T) {
	gdaoCache.BindExpireWriteClass[dao.Hstest]() //set cache for Hstest
	hs := dao.NewHstest()
	hs.Where((hs.ID.Between(0, 2)).Or(hs.ID.Between(10, 15)))
	hs.Limit(3)
	hs.Selects() //第一次查询,缓冲池没有数据,则结果集放入缓冲池
	println()
	hs = dao.NewHstest()
	hs.Where((hs.ID.Between(0, 2)).Or(hs.ID.Between(10, 15)))
	hs.Limit(3)
	hss, _ := hs.Selects() //第二次查询,缓冲池有数据,返回缓存数据
	println()
	hss[0].Insert() //新增一条数据;发生增删改操作,清除dao.Hstest类的所有缓存
	println()
	hs = dao.NewHstest()
	hs.Where((hs.ID.Between(0, 2)).Or(hs.ID.Between(10, 15)))
	hs.Limit(3)
	hs.Selects() //第三次查询,缓冲池已经清空数据,结果集重新放入缓冲池
}

结果打印与解析

=== RUN   TestExpireWrite

//第一次查询,缓冲池没有数据,则结果集放入缓冲池 [SET CACHE]
[DEBUG][SELETE LIST][ select id,age,rowname,value,updatetime,body,floa,level from hstest where id between ? and ? or (id between ? and ?) LIMIT ? OFFSET 0 ][0 2 10 15 3]
[DEBUG][SET CACHE][ select id,age,rowname,value,updatetime,body,floa,level from hstest where id between ? and ? or (id between ? and ?) LIMIT ? OFFSET 0 ][0 2 10 15 3]

//第二次查询,缓冲池有数据,返回缓存数据 [GET CACHE]
[DEBUG][SELETE LIST][ select id,age,rowname,value,updatetime,body,floa,level from hstest where id between ? and ? or (id between ? and ?) LIMIT ? OFFSET 0 ][0 2 10 15 3]
[DEBUG][GET CACHE][ select id,age,rowname,value,updatetime,body,floa,level from hstest where id between ? and ? or (id between ? and ?) LIMIT ? OFFSET 0 ][0 2 10 15 3]

//新增一条数据;发生增删改操作,清除dao.Hstest类的所有缓存
[DEBUG][INSERT][insert  into hstest(updatetime,body,floa,level,id,age,rowname,value )values(?,?,?,?,?,?,?,?)][2024-06-21 00:00:00 +0800 CST [49 49 49 49 49 49] 1 22222222 1 33 111 bbbb]

//第三次查询,缓冲池已经清空数据,结果集重新放入缓冲池 [SET CACHE]
[DEBUG][SELETE LIST][ select id,age,rowname,value,updatetime,body,floa,level from hstest where id between ? and ? or (id between ? and ?) LIMIT ? OFFSET 0 ][0 2 10 15 3]
[DEBUG][SET CACHE][ select id,age,rowname,value,updatetime,body,floa,level from hstest where id between ? and ? or (id between ? and ?) LIMIT ? OFFSET 0 ][0 2 10 15 3]

实际项目使用示例:如 https://tlnet.top 项目,该项目使用gdao读写数据,并使用写过期缓存数据,方便数据缓存与增删改过期

设置相应的类的写过期缓存,当这些类发生增删改时,它们的缓存数据被清除

读取数据

不使用缓存,使用UseCache(false)

 

通过Gdao缓存, https://tlnet.top 的响应非常迅速,具备极高性能表现。


简单使用介绍

1. 安装

go get github.com/donnie4w/gdao

2. 配置数据源

gdao.Init(mysqldb,gdao.MYSQL) //gdao初始化数据源  mysqldb 为sql.DB对象 , gdao.MYSQL 为数据库类型

3. 标准化结构体操作

// 读取
hs := dao.NewHstest()
hs.Where(hs.Id.EQ(10))
h, err := hs.Select(hs.Id, hs.Value, hs.Rowname)
//[DEBUG][SELETE ONE][ select id,value,rowname from hstest where id=?][10] 
// 更新
hs := dao.NewHstest()
hs.SetRowname("hello10")
hs.Where(hs.Id.EQ(10))
hs.Update()
//[DEBUG][UPDATE][update hstest set rowname=? where id=?][hello10 10]
// 删除
hs := dao.NewHstest()
hs.Where(hs.Id.EQ(10))
hs.Delete()
//[DEBUG][UPDATE][delete from hstest where id=?][10]
//新增
hs := dao.NewHstest()
hs.SetValue("hello123")
hs.SetLevel(12345)
hs.SetBody([]byte("hello"))
hs.SetRowname("hello1234")
hs.SetUpdatetime(time.Now())
hs.Insert()
//[DEBUG][INSERT][insert  into hstest(floa,age,value,level,body,rowname,updatetime )values(?,?,?,?,?)][hello123 12345 hello hello1234 2024-07-17 19:36:44]

4. 原生SQL操作

//查询,返回单条 gdao是原生SQL操作入口
gdao.ExecuteQueryBean("select id,value,rowname from hstest where id=?", 10)

//insert
gdao.ExecuteUpdate("insert into hstest2(rowname,value) values(?,?)", "helloWorld", "123456789");

//update
gdao.ExecuteUpdate("update hstest set value=? where id=1", "hello");

//delete
gdao.ExecuteUpdate("delete from hstest where id = ?", 1);

5. 配置缓存

//绑定Hstest  启用缓存, 缓存默认时效为300秒, gdaoCache为缓存操作入口
gdaoCache.BindClass[dao.Hstest]()

6. 读写分离

mysqldb := getDataSource("mysql.json") // 获取备库数据源:mysqldb
gdaoSlave.BindClass[dao.Hstest](mysqldb, gdao.MYSQL) //这里主数据库为sqlite,备数据库为mysql,Hstest读取数据源为mysql, gdaoSlave为读写分离操作入口

7. SQL映射

<!-- MyBatis 风格的 XML 配置文件 -->
<mapper namespace="user">
   <select id="selectHstest1" parameterType="int64" resultType="hstest1">
      SELECT * FROM hstest1  order by id desc limit #{limit}
   </select>
</mapper>
//读取解析xml配置
hs1, _ := gdaoMapper.Select[dao.Hstest1]("user.selectHstest1", 1)
fmt.Println(hs1)

8. 动态SQL映射

    <!-- where if -->
    <select id="demo1" resultType="Hstest">
        SELECT * FROM hstest
        <where>
            <if test="rowname== 'hello'">
                and rowname = #{rowname}
            </if>
            <if test="id >0">
                AND id = #{id}
            </if>
        </where>
    </select>
	hs := dao.NewHstest()
	hs.SetId(12)
	gdaoMapper.SelectBean("dynamic.demo1", hs)  //执行映射id  [SELECT * FROM hstest WHERE id = ?]ARGS[12]

9. SqlBuilder

func Test_Append(t *testing.T) {
	bean := NewSqlBuilder().Append("SELECT * FROM hstest where").
		Append("id=?", 11).
		Append("and rowname<>?", "hello").SelectOne()
	logger.Debug(bean)
}

10. SqlBuilder  

func Test_AppendScan(t *testing.T) {
	var hs dao.Hstest
	NewSqlBuilder().Append("SELECT * FROM hstest where").
		Append("id=?", 11).
		Append("and rowname<>?", "hello").SelectOne().Scan(&hs)
	logger.Debug(&hs)
}

11. SqlBuilder 动态SQL

func Test_sqlBuilder_if(t *testing.T) {
   context := map[string]any{"id": 12}
   
   builder := sqlBuilder.NewSqlBuilder()
   builder.Append("SELECT * FROM hstest where 1=1").
           AppendIf("id>0", context, "and id=?", context["id"])  //动态SQL,当id>0时,动态添加 and id=?
   
   bean := builder.SelectOne()  //查询SQL: SELECT * FROM hstest where 1=1 and id=?  [ARGS][12]
   logger.Debug(bean)
}
优秀的个人博客,低调大师

微信关注我们

原文链接:https://www.oschina.net/news/317285/gdao-1-2-2-released

转载内容版权归作者及来源网站所有!

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

相关文章

发表评论

资源下载

更多资源
优质分享Android(本站安卓app)

优质分享Android(本站安卓app)

近一个月的开发和优化,本站点的第一个app全新上线。该app采用极致压缩,本体才4.36MB。系统里面做了大量数据访问、缓存优化。方便用户在手机上查看文章。后续会推出HarmonyOS的适配版本。

Mario,低调大师唯一一个Java游戏作品

Mario,低调大师唯一一个Java游戏作品

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

Oracle Database,又名Oracle RDBMS

Oracle Database,又名Oracle RDBMS

Oracle Database,又名Oracle RDBMS,或简称Oracle。是甲骨文公司的一款关系数据库管理系统。它是在数据库领域一直处于领先地位的产品。可以说Oracle数据库系统是目前世界上流行的关系数据库管理系统,系统可移植性好、使用方便、功能强,适用于各类大、中、小、微机环境。它是一种高效率、可靠性好的、适应高吞吐量的数据库方案。

Apache Tomcat7、8、9(Java Web服务器)

Apache Tomcat7、8、9(Java Web服务器)

Tomcat是Apache 软件基金会(Apache Software Foundation)的Jakarta 项目中的一个核心项目,由Apache、Sun 和其他一些公司及个人共同开发而成。因为Tomcat 技术先进、性能稳定,而且免费,因而深受Java 爱好者的喜爱并得到了部分软件开发商的认可,成为目前比较流行的Web 应用服务器。