数据库缓存

This commit is contained in:
hoteas 2019-11-10 18:00:45 +08:00
parent 7d479f181a
commit 048c457303
4 changed files with 127 additions and 104 deletions

View File

@ -2,7 +2,10 @@
<project version="4">
<component name="ChangeListManager">
<list default="true" id="b2aca021-ff30-4cbf-8dc9-8cdd4f4c39dc" name="Default Changelist" comment="">
<change beforePath="$PROJECT_DIR$/dri/db/auto.go" beforeDir="false" afterPath="$PROJECT_DIR$/dri/db/auto.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/application.go" beforeDir="false" afterPath="$PROJECT_DIR$/application.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/cache_db.go" beforeDir="false" afterPath="$PROJECT_DIR$/cache_db.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/db.go" beforeDir="false" afterPath="$PROJECT_DIR$/db.go" afterDir="false" />
</list>
<option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
<option name="SHOW_DIALOG" value="false" />
@ -29,6 +32,7 @@
<property name="aspect.path.notification.shown" value="true" />
<property name="go.import.settings.migrated" value="true" />
<property name="go.sdk.automatically.set" value="true" />
<property name="go.tried.to.enable.integration.vgo.integrator" value="true" />
</component>
<component name="RunDashboard">
<option name="ruleStates">
@ -69,7 +73,7 @@
<option name="presentableId" value="Default" />
<updated>1573372557346</updated>
<workItem from="1573372558521" duration="18000" />
<workItem from="1573372583551" duration="3256000" />
<workItem from="1573372583551" duration="5432000" />
</task>
<task id="LOCAL-00001" summary="清理">
<created>1573372845218</created>
@ -99,7 +103,14 @@
<option name="project" value="LOCAL" />
<updated>1573375745411</updated>
</task>
<option name="localTasksCounter" value="5" />
<task id="LOCAL-00005" summary="自动设置数据库">
<created>1573375887561</created>
<option name="number" value="00005" />
<option name="presentableId" value="LOCAL-00005" />
<option name="project" value="LOCAL" />
<updated>1573375887561</updated>
</task>
<option name="localTasksCounter" value="6" />
<servers />
</component>
<component name="TypeScriptGeneratedFilesManager">

View File

@ -7,12 +7,12 @@ import (
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"path/filepath"
"strconv"
"log"
"strings"
)
@ -97,14 +97,10 @@ func (this *Application) Run(router Router) {
IsRun = true
}
ch := make(chan int)
if ObjToCeilInt(this.Port) != 0 {
go func() {
App[this.Port] = this
this.Server.Handler = this
//启动服务
@ -113,8 +109,6 @@ func (this *Application) Run(router Router) {
log.Println(err)
ch <- 1
}()
} else if ObjToCeilInt(this.TLSPort) != 0 {
go func() {
@ -127,8 +121,6 @@ func (this *Application) Run(router Router) {
log.Println(err)
ch <- 2
}()
} else {
log.Println("没有端口启用")
@ -136,7 +128,6 @@ func (this *Application) Run(router Router) {
}
value := <-ch
log.Println("启动服务失败 : ", value)
@ -158,6 +149,8 @@ func (this *Application) SetConnectDB(connect func(err ...*Error) *sql.DB) {
this.Db.DBCached = true
}
this.Db.Type = this.Config.GetString("dbType")
}
//设置配置文件路径全路径或者相对路径
@ -236,7 +229,6 @@ func (this *Application) SetConfig(configPath ...string) {
return
}
err = ioutil.WriteFile(this.configPath, out.Bytes(), os.ModeAppend)
if err != nil {

View File

@ -2,8 +2,8 @@ package hotime
import (
"encoding/json"
"time"
"strings"
"time"
)
type CacheDb struct {
@ -17,6 +17,7 @@ func (this *CacheDb) initDbTable() {
if this.isInit {
return
}
if this.Db.Type == "mysql" {
dbNames := this.Db.Query("SELECT DATABASE()")
@ -25,10 +26,37 @@ func (this *CacheDb) initDbTable() {
}
dbName := dbNames[0].GetString("DATABASE()")
res := this.Db.Query("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='" + dbName + "' AND TABLE_NAME='cached'")
if len(res) == 0 {
this.Db.Exec("CREATE TABLE `cached` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `ckey` varchar(60) DEFAULT NULL, `cvalue` varchar(2000) DEFAULT NULL, `time` bigint(20) DEFAULT NULL, `endtime` int(11) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=198740 DEFAULT CHARSET=utf8")
}
if len(res) != 0 {
this.isInit = true
return
}
_, e := this.Db.Exec("CREATE TABLE `cached` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `ckey` varchar(60) DEFAULT NULL, `cvalue` varchar(2000) DEFAULT NULL, `time` bigint(20) DEFAULT NULL, `endtime` bigint(20) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=198740 DEFAULT CHARSET=utf8")
if e.GetError() == nil {
this.isInit = true
}
}
if this.Db.Type == "sqlite" {
res := this.Db.Query(`select * from sqlite_master where type = 'table' and name = 'cached'`)
if len(res) != 0 {
this.isInit = true
return
}
_, e := this.Db.Exec(`CREATE TABLE "cached" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"ckey" TEXT(60),
"cvalue" TEXT(2000),
"time" integer,
"endtime" integer
);`)
if e.GetError() == nil {
this.isInit = true
}
}
}

10
db.go
View File

@ -13,6 +13,7 @@ type HoTimeDB struct {
*sql.DB
contextBase
CacheIns
Type string
DBCached bool
LastQuery string
LastData []interface{}
@ -444,13 +445,9 @@ func (this *HoTimeDB) Select(table string, qu ...interface{}) []Map {
}
}
//无缓存则数据库取
res := this.Query(query, qs...)
if res == nil {
res = []Map{}
}
@ -461,9 +458,6 @@ func (this *HoTimeDB) Select(table string, qu ...interface{}) []Map {
this.Cache(table+":"+md5, res)
}
return res
}
@ -592,7 +586,6 @@ func (this *HoTimeDB) where(data Map) (string, []interface{}) {
}
} else {
//fmt.Println(v)
where += " " + ObjToStr(v)
@ -782,7 +775,6 @@ func (this *HoTimeDB) notIn(k string, v interface{}, where string, res []interfa
where += "`" + k + "` !=? "
res = append(res, v)
}
return where, res