添加日志、缓存操作

This commit is contained in:
朱毅骏 2021-07-14 16:50:45 +08:00
parent eeda01e0ab
commit bfe7ed570f
14 changed files with 168 additions and 10 deletions

View 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"
}

View 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"
}

View File

@ -79,7 +79,8 @@ func (t *OrmController2update) Post() {
id, _ := t.GetInt64("id")
title := t.GetString("title")
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)
if err != nil {
log.Errorf("更新失败")

View File

@ -11,16 +11,47 @@ type OrmController3 struct {
beego.Controller
}
/**
复杂查询
*/
func (t *OrmController3) Get() {
o := orm.NewOrm()
//
//o.QueryTable("article")
article := models.Article{}
articles := []models.Article{}
var articles []models.Article
qs := o.QueryTable(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"
}

View 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"
}

View File

@ -37,5 +37,7 @@ func main() {
beego.BConfig.WebConfig.EnableXSRF = true
beego.BConfig.WebConfig.XSRFKey = "9029zyjlpydzxzyq9029"
beego.BConfig.WebConfig.XSRFExpire = 3600
//开启sql打印
orm.Debug = true
beego.Run()
}

View File

@ -3,9 +3,10 @@ package models
import "github.com/astaxie/beego/orm"
type Article struct {
Id int `orm:"pk"`
Title string `orm:"title"`
Author string `orm:"author"`
Id int `orm:"pk"`
Title string `orm:"column(title)"`
Author string `orm:"column(author)"`
ReadCount int `orm:"column(read_count)"`
}
func (a *Article) TableName() string {

View File

@ -2,6 +2,8 @@ package routers
import (
"beego_demo1/controllers"
"beego_demo1/controllers/cache"
"beego_demo1/controllers/log"
"beego_demo1/controllers/test_orm"
"github.com/astaxie/beego"
)
@ -42,4 +44,8 @@ func init() {
//exper表达式
beego.Router("/orm3", &test_orm.OrmController3{})
beego.Router("/orm4", &test_orm.OrmController4{})
//log
beego.Router("/log", &log.InternalLogController{})
beego.Router("/memory", &cache.MemoryController{})
}

View File

@ -25,12 +25,13 @@ CREATE TABLE `article` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`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,
`read_count` int DEFAULT 0,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8 COLLATE = utf8_unicode_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of article
-- ----------------------------
INSERT INTO `article` VALUES (1, '西游记', '吴承恩');
INSERT INTO `article` VALUES (1, '西游记', '吴承恩',0);
SET FOREIGN_KEY_CHECKS = 1;

10
beego_demo1/views/cache/cache.html vendored Normal file
View File

@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>缓存</h1>
</body>
</html>

View File

@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>log</title>
</head>
<body>
<h1>日志模块</h1>
</body>
</html>

View File

@ -11,6 +11,7 @@
<input type="hidden" name="id" value="{{.article.Id}}"><br>
文章标题:<input type="text" name="title" value="{{.article.Title}}"><br>
文章作者:<input type="text" name="author" value="{{.article.Author}}"><br>
阅读量:<input type="text" name="readCount" value="{{.article.ReadCount}}"><br>
<input type="submit" value="提交">
</form>
</body>

View File

@ -12,11 +12,13 @@
<thead>
<th>标题</th> &nbsp;&nbsp;
<th>作者</th>&nbsp;&nbsp;
<th>阅读量</th>&nbsp;&nbsp;
</thead>
{{range $i,$v := .articles}}
<tr>
<td>{{$v.Title}}</td> &nbsp;&nbsp;
<td>{{$v.Author}}</td> &nbsp;&nbsp;
<td>{{$v.ReadCount}}</td> &nbsp;&nbsp;
<td><a href="/article/edit?id={{$v.Id}}">修改</a></td> &nbsp;&nbsp;
<td><a href="/article/delete?id={{$v.Id}}">删除</a></td>&nbsp;&nbsp;
</tr>

View 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>