添加日志、缓存操作
This commit is contained in:
parent
eeda01e0ab
commit
bfe7ed570f
21
beego_demo1/controllers/cache/memoryController.go
vendored
Normal file
21
beego_demo1/controllers/cache/memoryController.go
vendored
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
package cache
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/astaxie/beego"
|
||||||
|
"github.com/astaxie/beego/cache"
|
||||||
|
"github.com/astaxie/beego/logs"
|
||||||
|
)
|
||||||
|
|
||||||
|
type MemoryController struct {
|
||||||
|
beego.Controller
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *MemoryController) Get() {
|
||||||
|
mc, err := cache.NewCache("memory", `{"interval":"60"}`)
|
||||||
|
if err != nil {
|
||||||
|
logs.Error("创建缓存报错了")
|
||||||
|
}
|
||||||
|
mc.Put("name", "朱毅骏", 20)
|
||||||
|
logs.Info(mc.Get("name"))
|
||||||
|
t.TplName = "cache/cache.html"
|
||||||
|
}
|
34
beego_demo1/controllers/log/logController.go
Normal file
34
beego_demo1/controllers/log/logController.go
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
package log
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/astaxie/beego"
|
||||||
|
"github.com/astaxie/beego/logs"
|
||||||
|
)
|
||||||
|
|
||||||
|
type InternalLogController struct {
|
||||||
|
beego.Controller
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *InternalLogController) Get() {
|
||||||
|
//日志文件
|
||||||
|
logs.SetLogger("file", `{"filename":"logs/test_internal.log"}`)
|
||||||
|
//删除控制台日志
|
||||||
|
//logs.GetBeeLogger().DelLogger("console")
|
||||||
|
//设置打印日志级别
|
||||||
|
logs.SetLevel(logs.LevelDebug)
|
||||||
|
//日志级别从0开始
|
||||||
|
logs.Emergency("this is a emergency,level 0")
|
||||||
|
logs.Alert("this is a alert,,level 1")
|
||||||
|
logs.Critical("this is a Critical,level 2")
|
||||||
|
logs.Error("this is a Error,level 3")
|
||||||
|
logs.Warning("this is a Warning,level 4")
|
||||||
|
logs.Notice("this is a Notice,level 5")
|
||||||
|
logs.Info("this is a Info,level 6")
|
||||||
|
logs.Informational("this is a Informational,level 6")
|
||||||
|
logs.Debug("this is a Debug,level 7")
|
||||||
|
t.TplName = "log/log.html"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *InternalLogController) Post() {
|
||||||
|
t.TplName = "log/log.html"
|
||||||
|
}
|
@ -79,7 +79,8 @@ func (t *OrmController2update) Post() {
|
|||||||
id, _ := t.GetInt64("id")
|
id, _ := t.GetInt64("id")
|
||||||
title := t.GetString("title")
|
title := t.GetString("title")
|
||||||
author := t.GetString("author")
|
author := t.GetString("author")
|
||||||
article := models.Article{Id: int(id), Title: title, Author: author}
|
readCount, _ := t.GetInt64("readCount")
|
||||||
|
article := models.Article{Id: int(id), Title: title, Author: author, ReadCount: int(readCount)}
|
||||||
count, err := o.Update(&article)
|
count, err := o.Update(&article)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("更新失败")
|
log.Errorf("更新失败")
|
||||||
|
@ -11,16 +11,47 @@ type OrmController3 struct {
|
|||||||
beego.Controller
|
beego.Controller
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
复杂查询
|
||||||
|
*/
|
||||||
func (t *OrmController3) Get() {
|
func (t *OrmController3) Get() {
|
||||||
o := orm.NewOrm()
|
o := orm.NewOrm()
|
||||||
|
|
||||||
//
|
|
||||||
//o.QueryTable("article")
|
//o.QueryTable("article")
|
||||||
article := models.Article{}
|
article := models.Article{}
|
||||||
articles := []models.Article{}
|
var articles []models.Article
|
||||||
qs := o.QueryTable(article)
|
qs := o.QueryTable(article)
|
||||||
|
//大小写敏感
|
||||||
//qs.Filter("title__exact","西游记").One(&article)
|
//qs.Filter("title__exact","西游记").One(&article)
|
||||||
qs.All(&articles)
|
////大小写不敏感
|
||||||
log.Info(articles)
|
//qs.Filter("title__iexact","西游记").One(&article)
|
||||||
|
|
||||||
|
//模糊查询
|
||||||
|
//大小写敏感
|
||||||
|
//qs.Filter("title__contains","游").One(&article) //LIKE BINARY
|
||||||
|
//大小写不敏感
|
||||||
|
//qs.Filter("title__icontains","Z").One(&article) //LIKE
|
||||||
|
|
||||||
|
//查询所有
|
||||||
|
//qs.GroupBy("id").All(&articles)
|
||||||
|
//log.Info("list:",articles)
|
||||||
|
|
||||||
|
//gt 大于 gte 大于等于
|
||||||
|
//qs.Filter("read_count__gt",4).One(&article)
|
||||||
|
//qs.Filter("read_count__gte",4).One(&article)
|
||||||
|
//lt 小于 lte 小于等于
|
||||||
|
//qs.Filter("read_count__lt",100).One(&article)
|
||||||
|
//qs.Filter("read_count__lte",100).All(&articles)
|
||||||
|
//以什么开始
|
||||||
|
//qs.Filter("title__startswith","z").All(&articles)
|
||||||
|
//qs.Filter("title__istartswith","z").All(&articles)
|
||||||
|
//以什么结束
|
||||||
|
//qs.Filter("title__endswith","n").All(&articles)
|
||||||
|
//qs.Filter("title__iendswith","n").All(&articles)
|
||||||
|
|
||||||
|
//qs.Filter("read_count__in",3,0,20,100).All(&articles)
|
||||||
|
qs.Filter("read_count__isnull", false).All(&articles)
|
||||||
|
|
||||||
|
log.Info("one打印:", article)
|
||||||
|
log.Info("all打印:", articles)
|
||||||
t.TplName = "test_orm/orm3.html"
|
t.TplName = "test_orm/orm3.html"
|
||||||
}
|
}
|
||||||
|
28
beego_demo1/controllers/test_orm/ormController4.go
Normal file
28
beego_demo1/controllers/test_orm/ormController4.go
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
package test_orm
|
||||||
|
|
||||||
|
import (
|
||||||
|
"beego_demo1/models"
|
||||||
|
"github.com/astaxie/beego"
|
||||||
|
"github.com/astaxie/beego/orm"
|
||||||
|
"github.com/prometheus/common/log"
|
||||||
|
)
|
||||||
|
|
||||||
|
type OrmController4 struct {
|
||||||
|
beego.Controller
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *OrmController4) Get() {
|
||||||
|
o := orm.NewOrm()
|
||||||
|
articles := []models.Article{}
|
||||||
|
qs := o.QueryTable(new(models.Article))
|
||||||
|
//qs.Filter("title__exact","西游记").All(&articles,"title","author")
|
||||||
|
//qs.Filter("title__exact","西游记").Filter("author__exact","吴承恩").All(&articles,"Id","Title","Author","ReadCount")
|
||||||
|
//排除
|
||||||
|
//qs.Exclude("title__iexact","zhuyijun").All(&articles)
|
||||||
|
|
||||||
|
//qs.Limit(2,1).All(&articles)
|
||||||
|
qs.Limit(10).Offset(0).All(&articles)
|
||||||
|
log.Info(articles)
|
||||||
|
|
||||||
|
t.TplName = "test_orm/orm4.html"
|
||||||
|
}
|
@ -37,5 +37,7 @@ func main() {
|
|||||||
beego.BConfig.WebConfig.EnableXSRF = true
|
beego.BConfig.WebConfig.EnableXSRF = true
|
||||||
beego.BConfig.WebConfig.XSRFKey = "9029zyjlpydzxzyq9029"
|
beego.BConfig.WebConfig.XSRFKey = "9029zyjlpydzxzyq9029"
|
||||||
beego.BConfig.WebConfig.XSRFExpire = 3600
|
beego.BConfig.WebConfig.XSRFExpire = 3600
|
||||||
|
//开启sql打印
|
||||||
|
orm.Debug = true
|
||||||
beego.Run()
|
beego.Run()
|
||||||
}
|
}
|
||||||
|
@ -3,9 +3,10 @@ package models
|
|||||||
import "github.com/astaxie/beego/orm"
|
import "github.com/astaxie/beego/orm"
|
||||||
|
|
||||||
type Article struct {
|
type Article struct {
|
||||||
Id int `orm:"pk"`
|
Id int `orm:"pk"`
|
||||||
Title string `orm:"title"`
|
Title string `orm:"column(title)"`
|
||||||
Author string `orm:"author"`
|
Author string `orm:"column(author)"`
|
||||||
|
ReadCount int `orm:"column(read_count)"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *Article) TableName() string {
|
func (a *Article) TableName() string {
|
||||||
|
@ -2,6 +2,8 @@ package routers
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"beego_demo1/controllers"
|
"beego_demo1/controllers"
|
||||||
|
"beego_demo1/controllers/cache"
|
||||||
|
"beego_demo1/controllers/log"
|
||||||
"beego_demo1/controllers/test_orm"
|
"beego_demo1/controllers/test_orm"
|
||||||
"github.com/astaxie/beego"
|
"github.com/astaxie/beego"
|
||||||
)
|
)
|
||||||
@ -42,4 +44,8 @@ func init() {
|
|||||||
|
|
||||||
//exper表达式
|
//exper表达式
|
||||||
beego.Router("/orm3", &test_orm.OrmController3{})
|
beego.Router("/orm3", &test_orm.OrmController3{})
|
||||||
|
beego.Router("/orm4", &test_orm.OrmController4{})
|
||||||
|
//log
|
||||||
|
beego.Router("/log", &log.InternalLogController{})
|
||||||
|
beego.Router("/memory", &cache.MemoryController{})
|
||||||
}
|
}
|
||||||
|
@ -25,12 +25,13 @@ CREATE TABLE `article` (
|
|||||||
`id` int(11) NOT NULL AUTO_INCREMENT,
|
`id` int(11) NOT NULL AUTO_INCREMENT,
|
||||||
`title` varchar(64) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL,
|
`title` varchar(64) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL,
|
||||||
`author` varchar(20) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL,
|
`author` varchar(20) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL,
|
||||||
|
`read_count` int DEFAULT 0,
|
||||||
PRIMARY KEY (`id`) USING BTREE
|
PRIMARY KEY (`id`) USING BTREE
|
||||||
) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8 COLLATE = utf8_unicode_ci ROW_FORMAT = Dynamic;
|
) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8 COLLATE = utf8_unicode_ci ROW_FORMAT = Dynamic;
|
||||||
|
|
||||||
-- ----------------------------
|
-- ----------------------------
|
||||||
-- Records of article
|
-- Records of article
|
||||||
-- ----------------------------
|
-- ----------------------------
|
||||||
INSERT INTO `article` VALUES (1, '西游记', '吴承恩');
|
INSERT INTO `article` VALUES (1, '西游记', '吴承恩',0);
|
||||||
|
|
||||||
SET FOREIGN_KEY_CHECKS = 1;
|
SET FOREIGN_KEY_CHECKS = 1;
|
||||||
|
10
beego_demo1/views/cache/cache.html
vendored
Normal file
10
beego_demo1/views/cache/cache.html
vendored
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>Title</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>缓存</h1>
|
||||||
|
</body>
|
||||||
|
</html>
|
10
beego_demo1/views/log/log.html
Normal file
10
beego_demo1/views/log/log.html
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>log</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>日志模块</h1>
|
||||||
|
</body>
|
||||||
|
</html>
|
@ -11,6 +11,7 @@
|
|||||||
<input type="hidden" name="id" value="{{.article.Id}}"><br>
|
<input type="hidden" name="id" value="{{.article.Id}}"><br>
|
||||||
文章标题:<input type="text" name="title" value="{{.article.Title}}"><br>
|
文章标题:<input type="text" name="title" value="{{.article.Title}}"><br>
|
||||||
文章作者:<input type="text" name="author" value="{{.article.Author}}"><br>
|
文章作者:<input type="text" name="author" value="{{.article.Author}}"><br>
|
||||||
|
阅读量:<input type="text" name="readCount" value="{{.article.ReadCount}}"><br>
|
||||||
<input type="submit" value="提交">
|
<input type="submit" value="提交">
|
||||||
</form>
|
</form>
|
||||||
</body>
|
</body>
|
||||||
|
@ -12,11 +12,13 @@
|
|||||||
<thead>
|
<thead>
|
||||||
<th>标题</th>
|
<th>标题</th>
|
||||||
<th>作者</th>
|
<th>作者</th>
|
||||||
|
<th>阅读量</th>
|
||||||
</thead>
|
</thead>
|
||||||
{{range $i,$v := .articles}}
|
{{range $i,$v := .articles}}
|
||||||
<tr>
|
<tr>
|
||||||
<td>{{$v.Title}}</td>
|
<td>{{$v.Title}}</td>
|
||||||
<td>{{$v.Author}}</td>
|
<td>{{$v.Author}}</td>
|
||||||
|
<td>{{$v.ReadCount}}</td>
|
||||||
<td><a href="/article/edit?id={{$v.Id}}">修改</a></td>
|
<td><a href="/article/edit?id={{$v.Id}}">修改</a></td>
|
||||||
<td><a href="/article/delete?id={{$v.Id}}">删除</a></td>
|
<td><a href="/article/delete?id={{$v.Id}}">删除</a></td>
|
||||||
</tr>
|
</tr>
|
||||||
|
10
beego_demo1/views/test_orm/orm4.html
Normal file
10
beego_demo1/views/test_orm/orm4.html
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>Title</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>qs接口</h1>
|
||||||
|
</body>
|
||||||
|
</html>
|
Loading…
Reference in New Issue
Block a user