forked from go/golangs_learn
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
53 lines
881 B
53 lines
881 B
package datacache
|
|
|
|
import (
|
|
"os"
|
|
"videosrt/app/tool"
|
|
)
|
|
|
|
type AppCache struct {
|
|
RootDir string
|
|
Dir string
|
|
File string
|
|
}
|
|
|
|
func NewAppCahce(rootdir string , file string) *AppCache {
|
|
c := new(AppCache)
|
|
c.Dir = "data/json"
|
|
c.RootDir = rootdir
|
|
c.File = c.RootDir + "/" + c.Dir + "/" + file + ".json"
|
|
|
|
c.initDir()
|
|
return c
|
|
}
|
|
|
|
func (app *AppCache) Set(data interface{}) {
|
|
file := app.File
|
|
if err := SavetoJson(data , file); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
|
|
func (app *AppCache) Get(structs interface{}) interface{} {
|
|
file := app.File
|
|
err, data := GettoJson(file, structs)
|
|
if err != nil {
|
|
if os.IsNotExist(err) {
|
|
return structs
|
|
}
|
|
return data
|
|
}
|
|
return data
|
|
}
|
|
|
|
|
|
func (app *AppCache) initDir() {
|
|
fileDir := app.RootDir + "/" + app.Dir
|
|
if !tool.DirExists(fileDir) {
|
|
//创建目录
|
|
if err := tool.CreateDir(fileDir , false); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
}
|