Пример #1
0
func SetEngine() (*xorm.Engine, error) {

	if engine, err := ConDb(); err != nil {

		return nil, err
	} else {

		engine.ShowInfo = false
		engine.ShowSQL = true   //true则会在控制台打印出生成的SQL语句;
		engine.ShowDebug = true //true则会在控制台打印调试信息;
		engine.ShowWarn = true  //true则会在控制台打印警告信息;
		engine.Logger.SetLevel(core.LOG_OFF)

		cacher := xorm.NewLRUCacher(xorm.NewMemoryStore(), 10000)
		engine.SetDefaultCacher(cacher)
		//engine.TZLocation = time.Local

		if f, err := os.Create("./logs/xorm.log"); err != nil {
			panic(fmt.Sprintf("Create Xorm Logs Error :%v", err))
		} else {
			engine.Logger = xorm.NewSimpleLogger(f)
		}

		return engine, err
	}
}
Пример #2
0
func SetCacher() {
	cacher := xorm.NewLRUCacher(xorm.NewMemoryStore(), 5000)
	orm.SetDefaultCacher(cacher)

	// cacher := xorm.NewLRUCacher(xorm.NewMemoryStore(), 1000)
	// engine.MapCacher(&user, cacher)
}
Пример #3
0
func init() {
	// 创建 ORM 引擎与数据库
	var err error
	x, err = xorm.NewEngine("sqlite3", "./bank.db")
	if err != nil {
		log.Fatalf("Fail to create engine: %v\n", err)
	}

	// 同步结构体与数据表
	if err = x.Sync(new(Account)); err != nil {
		log.Fatalf("Fail to sync database: %v\n", err)
	}

	// 记录日志
	f, err := os.Create("sql.log")
	if err != nil {
		log.Fatalf("Fail to create log file: %v\n", err)
		return
	}
	x.Logger = xorm.NewSimpleLogger(f)
	x.ShowSQL = true

	// 设置默认 LRU 缓存
	cacher := xorm.NewLRUCacher(xorm.NewMemoryStore(), 1000)
	x.SetDefaultCacher(cacher)
}
func InitDb(datasourcename string) {
	var err error
	db, err = xorm.NewEngine("sqlite3", datasourcename)
	cacher = xorm.NewLRUCacher(xorm.NewMemoryStore(), 1000)
	if err != nil {
		log.Fatalf("Fail to create engine: %v\n", err)
	}
}
Пример #5
0
// 打开缓存, 默认只缓存User, Role, Task相关表
func EnableCache() error {
	cacher := xorm.NewLRUCacher(xorm.NewMemoryStore(), 1000)
	x.MapCacher(&User{}, cacher)
	x.MapCacher(&Role{}, cacher)
	x.MapCacher(&RoleUser{}, cacher)
	x.MapCacher(&Permission{}, cacher)
	x.MapCacher(&PermissionRole{}, cacher)

	return nil
}
Пример #6
0
func setDbEngine() (*xorm.Engine, error) {
	engine, err := xConnDb()
	if engine == nil || err != nil {
		return nil, err
	}

	engine.SetMapper(&core.SameMapper{})
	engine.ShowSQL = true

	cacher := xorm.NewLRUCacher(xorm.NewMemoryStore(), 1000)
	engine.SetDefaultCacher(cacher)

	return engine, nil
}
Пример #7
0
func (m *Logger) CheckEnv() bool {
	if m.x == nil {
		fmt.Printf("[%s] Database initial error, disable this plugin.\n", m.GetName())
		return false
	}
	m.x.ShowDebug = false
	m.x.ShowErr = false
	m.x.ShowSQL = false
	m.x.SetMaxConns(10)

	cacher := xorm.NewLRUCacher(xorm.NewMemoryStore(), 10000)
	m.x.SetDefaultCacher(cacher)
	m.x.Sync2(new(ChatLogger))

	return true
}
Пример #8
0
func init() {
	var err error
	switch {
	case dbtype == "mysql":
		x, err = xorm.NewEngine("mysql", "root:root@/comcms?charset=utf8")
	case dbtype == "sqlite":
		x, err = xorm.NewEngine("sqlite3", "./data/sqlite.db")
	}
	//x, err = xorm.NewEngine("mysql", "root:root@/comcms?charset=utf8")

	if err != nil {
		log.Fatalf("fail to create engine: %v", err)
	}
	err = x.Sync2(new(Ads), new(Link), new(Guestbook), new(Config), new(Article), new(Admin), new(AdminRole), new(Category))
	x.ShowSQL = true
	x.ShowDebug = true
	x.ShowErr = true
	//设置缓存
	cacher := xorm.NewLRUCacher(xorm.NewMemoryStore(), 10000)
	x.SetDefaultCacher(cacher)

}
Пример #9
0
func InitDB() {
	dns := revel.Config.StringDefault("db.dns", "root:@tcp(127.0.0.1:3306)/lms?charset=utf8")

	var err error
	Engine, err = xorm.NewEngine("mysql", dns)
	if err != nil {
		revel.ERROR.Fatalln("InitDB - NewEngine error ", err)
	}
	err = Engine.Ping()
	if err != nil {
		revel.ERROR.Fatalln("InitDB - Ping error ", err)
	}
	Engine.ShowSQL = true
	Engine.ShowErr = true
	Engine.ShowDebug = true
	Engine.ShowWarn = true
	Engine.ShowInfo = true

	cacher := xorm.NewLRUCacher(xorm.NewMemoryStore(), 5000)
	Engine.SetDefaultCacher(cacher)

	go pingDB()
}
Пример #10
0
func (s *Server) enableDbCache() {
	if *s.enDbCache {
		cacher := xorm.NewLRUCacher(xorm.NewMemoryStore(), *s.cacheAmout)
		DB.SetDefaultCacher(cacher)
	}
}
Пример #11
0
func main() {
	f := "cache.db"
	os.Remove(f)

	Orm, err := xorm.NewEngine("sqlite3", f)
	if err != nil {
		fmt.Println(err)
		return
	}
	Orm.ShowSQL = true
	cacher := xorm.NewLRUCacher(xorm.NewMemoryStore(), 1000)
	Orm.SetDefaultCacher(cacher)

	err = Orm.CreateTables(&User{})
	if err != nil {
		fmt.Println(err)
		return
	}

	_, err = Orm.Insert(&User{Name: "xlw"})
	if err != nil {
		fmt.Println(err)
		return
	}

	users := make([]User, 0)
	err = Orm.Find(&users)
	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println("users:", users)

	users2 := make([]User, 0)

	err = Orm.Find(&users2)
	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println("users2:", users2)

	users3 := make([]User, 0)

	err = Orm.Find(&users3)
	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println("users3:", users3)

	user4 := new(User)
	has, err := Orm.Id(1).Get(user4)
	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println("user4:", has, user4)

	user4.Name = "xiaolunwen"
	_, err = Orm.Id(1).Update(user4)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println("user4:", user4)

	user5 := new(User)
	has, err = Orm.Id(1).Get(user5)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println("user5:", has, user5)

	_, err = Orm.Id(1).Delete(new(User))
	if err != nil {
		fmt.Println(err)
		return
	}

	for {
		user6 := new(User)
		has, err = Orm.Id(1).Get(user6)
		if err != nil {
			fmt.Println(err)
			return
		}
		fmt.Println("user6:", has, user6)
	}
}