当前位置: 首页 > 工具软件 > Redis ORM > 使用案例 >

php mysql redis orm_Redis ORM

秦博延
2023-12-01

需要引用的库、初始化方式等

import (

"github.com/mkideal/log”

"github.com/go-redis/redis”

"github.com/weikaishio/redis_orm”

"github.com/weikaishio/redis_orm/test/models”

)

func main() {

options := redis.Options{

Addr: “127.0.0.1:6379”,

Password: “”,

DB: 1,

}

redisClient := redis.NewClient(&options)

//注:已省略redisClient可用性检测

engine := redis_orm.NewEngine(redisClient)

engine.IsShowLog(true)

driver := "mysql"

host := "127.0.0.1:3306"

database := "bg_db"

username := "root"

password := ""

dataSourceName := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4&&allowOldPasswords=1&parseTime=true", username, password, host, database)

dbEngine, err := xorm.NewEngine(driver, dataSourceName)

if err != nil {

log.Error("NewEngine:%s,err:%v", dataSourceName, err)

return

}

//给redisORM对象设置同步到dbEngine数据库对象,每90秒同步一次

engine.SetSync2DB(dbEngine, 90)

//退出之前会执行同步到DB

defer engine.Quit()

faq := &models.Faq{

Type: 1,

Title: "Title",

Unique: 111,

Content: "Content",

}

//插入数据

engine.Insert(faq)

//查询指定Id的数据

model := &models.Faq{

Id: 1,

}

has, err := engine.Get(model)

if err != nil {

log.Error("Get(%d) err:%v", model.Id, err)

return

}

//查询指定条件的数据

searchCon := NewSearchConditionV2(faq.Unique, faq.Unique, 111)

var ary []model.Faq

count, err := engine.Find(0, 100, searchCon, &ary)

if err != nil {

log.Error("Find(%v) err:%v", searchCon, err)

return

}

//其他请见engine_curd.go、engine_curd_by_map.go里面的方法....更新、删除等功能, 也可以看目录下面的测试代码

}

 类似资料:

相关阅读

相关文章

相关问答