Ejemplo n.º 1
0
func GetCache(key string, to interface{}) error {
	if cc == nil {
		return errors.New("cc is nil")
	}

	defer func() {
		if r := recover(); r != nil {
			log.Redf("get cache error caught: %v\n", r)
			cc = nil
		}
	}()

	data := cc.Get(key)
	if data == nil {
		return errors.New("Cache不存在")
	}
	// log.Pinkln(data)
	err := Decode(data.([]byte), to)
	if err != nil {
		log.Warnln("获取Cache失败", key, err)
	} else {
		log.Greenln("获取Cache成功", key)
	}

	return err
}
Ejemplo n.º 2
0
func InitCache() {
	cacheConfig := beego.AppConfig.String("cache")

	if "redis" == cacheConfig {
		initRedis()
	} else {
		initMemcache()
	}

	log.Greenln("[cache] use", cacheConfig)
}
Ejemplo n.º 3
0
func GetCache(key string, to interface{}) error {
	data := cc.Get(key)
	if data == nil {
		return errors.New("Cache不存在")
	}
	// log.Pinkln(data)
	err := Decode(data.([]byte), to)
	if err != nil {
		log.Warnln("获取Cache失败", key, err)
	} else {
		log.Greenln("获取Cache成功", key)
	}

	return err
}
Ejemplo n.º 4
0
func TestTemplate(t *testing.T) {
	log.Debug = true
	header := `{{define "header.tpl"}}this is a header template{{end}}`
	index := `{{define "index.tpl"}}this is index file. {{template "header.tpl"}}{{end}}`
	Convey("Template sections", t, func() {
		templates["header.tpl"] = header
		templates["index.tpl"] = index

		getTemplateNames()
		log.Greenln(templatesName)

		So(templatesName, ShouldContain, "header.tpl")
		So(templatesName, ShouldContain, "index.tpl")

		b := bytes.NewBuffer(make([]byte, 0))
		t, _ := parseFiles(templatesName...)
		t.ExecuteTemplate(b, "index.tpl", nil)
		result := fmt.Sprintf("%s", b)

		So(result, ShouldEqual, "this is index file. this is a header template")

	})

}
Ejemplo n.º 5
0
func TestRouter(t *testing.T) {
	log.Debug = true
	Convey("Router sections", t, func() {
		e := Web()
		e.Router("/:hello/:world/123", 123, 456)
		e.Router("/:hello/123", 1234)
		e.Router("/", "index")
		node1 := getController("/param1/param2/123").(uriMode)
		ctrl1 := node1.fun
		param1 := node1.argsMap
		node2 := getController("/param1/123").(uriMode)
		ctrl2 := node2.fun
		param2 := node2.argsMap
		node3 := getController("/").(uriMode)
		ctrl3 := node3.fun
		param3 := node3.argsMap
		log.Greenln(ctrl1)
		log.Greenln(param1)
		log.Greenln(ctrl2)
		log.Greenln(param2)
		log.Greenln(ctrl3)
		log.Greenln(param3)

		getArgs("/param1/param2/123", "/:hello/:world/123")
		uri := parseURI("/test?hello=world")
		log.Greenln(uri)

		So(ctrl1, ShouldContain, 123)
		So(ctrl1, ShouldContain, 456)
		So(ctrl2, ShouldContain, 1234)
		So(ctrl3, ShouldContain, "index")
		So(param1["hello"], ShouldEqual, "param1")
		So(param1["world"], ShouldEqual, "param2")
		So(uri, ShouldEqual, "/test")
	})

}
Ejemplo n.º 6
0
func TestConfig(t *testing.T) {
	Convey("Test Config sections", t, func() {
		conf := NewConfig("etc/test.ini")
		content, err := conf.readConfigFile()
		if err != nil {
			log.Redln(err)
		} else {
			log.Greenln("raw content")
			log.Pinkln("==============")

			log.Blueln(content)
			content = conf.filterComment()

			log.Greenln("filter comment")
			log.Pinkln("==============")

			log.Blueln(content)
			arraylize := conf.arraylize()

			log.Greenln("arraylize")
			log.Pinkln("==============")
			count := len(arraylize)
			log.Bluef("[%d]\n", count)
			for i := 0; i < count; i++ {
				log.Bluef("[%d]\t%s\n", i, arraylize[i])
			}

			log.Greenln("parse items")
			log.Pinkln("==============")
			conf.parseItems()

			log.Greenln("warning stack")
			log.Pinkln("==============")
			log.Blueln(conf.GetWarnings())

			log.Greenln("mistake value")
			log.Pinkln("==============")
			log.Blueln(conf.GetString("mysql", "host"))

			log.Greenln("about bool")
			log.Pinkln("==============")
			conf.SetBool("test", "dev", true)
			dev, _ := conf.GetBool("_", "dev")
			log.Blueln("%v", dev)

			content = conf.serialize()
			log.Greenln("serialize value")
			log.Pinkln("==============")
			log.Blueln(content)

			log.Greenln("hex")
			log.Pinkln("==============")
			hex, _ := conf.GetInt("_", "hex")
			log.Blueln(hex)

			log.Greenln("get empty")
			log.Pinkln("==============")
			_, err = conf.GetInt("_", "heloo")
			if err != nil {
				log.Blueln(err)
			}

			log.Greenln("save config file")
			log.Pinkln("==============")
			err := conf.Save("tmp/test.ini")
			if err == nil {
				log.Blueln("done!")
			}

		}

		val1, _ := conf.Get("_", "port")
		So(val1, ShouldEqual, 80)

		val2, _ := conf.GetString("_", "appname")
		So(val2, ShouldEqual, "my application")

		val3, _ := conf.GetString("mysql", "password")
		So(val3, ShouldEqual, "\"liju")

		val4, _ := conf.GetString("mysql", "host")
		So(val4, ShouldEqual, `"192.168.1.11" = GHJ`)

		val5, _ := conf.GetBool("_", "dev")
		So(val5, ShouldEqual, true)

		val6, _ := conf.GetFloat("_", "pi")
		So(val6, ShouldEqual, 3.14)

		val7, _ := conf.GetInt("_", "hex")
		So(val7, ShouldEqual, 0x24)

		val8 := conf.GetIntDefault("section", "key", 100)
		So(val8, ShouldEqual, 100)

		val9 := conf.GetStringDefault("mysql", "key", "")
		So(val9, ShouldEqual, "")
	})
}