示例#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
}
示例#2
0
func SetCache(key string, value interface{}, timeout int64) error {
	data, err := Encode(value)
	if err != nil {
		return err
	}
	if cc == nil {
		return errors.New("cc is nil")
	}

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

	err = cc.Put(key, data, timeout)
	if err != nil {
		log.Warnln("Cache失败,key:", key)
		return err
	} else {
		log.Blueln("Cache成功,key:", key)
		return nil
	}
}
示例#3
0
func IncrBy(key string, incr int) int {
	var err error
	var val int
	defer utils.Recover("redis get falure")
	val, err = redis.IncrBy(key, incr)
	if err != nil {
		log.Redf("decode failure", err)
	}
	return val
}
示例#4
0
func Delete(key string) error {
	var err error
	defer utils.Recover("redis get falure")
	err = redis.Delete(key)

	if err != nil {
		log.Redf("decode failure", err)
	}
	return err
}
示例#5
0
func Get(key string, to interface{}) error {
	var err error
	defer utils.Recover("redis get falure")
	data := redis.Get(key)

	if data == nil {
		to = nil
		return errors.New("key point value is nil ")
	}
	err = utils.Decode(data.([]byte), to)
	if err != nil {
		log.Redf("decode failure", err)
	}
	return err
}
示例#6
0
func InitRedis() {
	cacheConfig := beego.AppConfig.String("cache")

	log.Greenf("cacheConfig:%v \n", cacheConfig)

	var cc cache.Cache
	if "redis" == cacheConfig {
		var err error

		defer utils.Recover("redis init falure")

		cc, err = cache.NewCache("redis", `{"conn":"`+beego.AppConfig.String("redis_host")+`"}`)

		if err != nil {
			log.Redf("%v", err)
		}
		cache, ok := cc.(*caches.MyRedisCache)
		if ok {
			redis = cache
		} else {
			log.Redf("parse cache to MyRedisCache failure !")
		}
	}
}
示例#7
0
文件: inject.go 项目: elago/ela
func (inj *injection) parseInjectionObject(mid interface{}) interface{} {
	t := reflect.TypeOf(mid)
	if t.Kind() == reflect.Func {
		result, err := inj.injectFuc(mid)
		if err != nil {
			log.Redf("injection failed: %s :L51\n", err)
			return nil
		} else {
			mid = result[0]
			return inj.parseInjectionObject(mid)
		}
	} else {
		return mid
	}
}
示例#8
0
func initRedis() {
	// cc = &cache.Cache{}
	var err error

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

	cc, err = cache.NewCache("redis", `{"conn":"`+beego.AppConfig.String("redis_host")+`"}`)

	if err != nil {
		log.Redln(err)
	}
}
示例#9
0
文件: error.go 项目: elago/ela
func servError(ctx *Context, err string, status int, useDefault bool) {
	inj := newInjection()

	controller := getController(fmt.Sprintf("@%d", status))

	if controller == nil {
		ctx.SetHeader("Content-Type", "text/html")
		ctx.SetStatus(status)
		ctx.Write(err)
		return
	}

	routerElement := controller.(uriMode)
	f := routerElement.fun

	if f != nil && !useDefault {
		functions := f

		defer func() {
			if r := recover(); r != nil {
				ctx.SetHeader("Content-Type", "text/html")
				ctx.SetStatus(status)
				ctx.Write(err)
			}
		}()

		ctx.SetStatus(status)

		// just get and execute first controller
		if len(functions) >= 1 {
			function := functions[0]
			inj.headMiddleware(ctx)
			inj.appendMiddleware(err)
			_, err := inj.injectFuc(function)
			if err != nil {
				log.Redf("injection failed: %s\n", err)
			}

		}

	} else {
		ctx.SetHeader("Content-Type", "text/html")
		ctx.SetStatus(status)
		ctx.Write(err)
	}
}
示例#10
0
文件: logger.go 项目: elago/ela
func responseLog(ctx Context) {
	content := "[%s] %s: complete %s %s %d\n"
	switch ctx.GetResponseWriter().Status() {
	case 301, 302:
		log.Bluef(content, tag, time.Now().Format(logTimeFormat), ctx.GetRequest().Method, ctx.GetRequest().RequestURI, ctx.GetResponseWriter().Status())
	case 304:
		log.Greenf(content, tag, time.Now().Format(logTimeFormat), ctx.GetRequest().Method, ctx.GetRequest().RequestURI, ctx.GetResponseWriter().Status())
	case 401, 403:
		log.Yellowf(content, tag, time.Now().Format(logTimeFormat), ctx.GetRequest().Method, ctx.GetRequest().RequestURI, ctx.GetResponseWriter().Status())
	case 404:
		log.Redf(content, tag, time.Now().Format(logTimeFormat), ctx.GetRequest().Method, ctx.GetRequest().RequestURI, ctx.GetResponseWriter().Status())
	case 500:
		log.Pinkf(content, tag, time.Now().Format(logTimeFormat), ctx.GetRequest().Method, ctx.GetRequest().RequestURI, ctx.GetResponseWriter().Status())
	default:
		log.Printf(content, tag, time.Now().Format(logTimeFormat), ctx.GetRequest().Method, ctx.GetRequest().RequestURI, ctx.GetResponseWriter().Status())
	}

}
示例#11
0
func DelCache(key string) 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
		}
	}()

	err := cc.Delete(key)
	if err != nil {
		return errors.New("Cache删除失败")
	} else {
		log.Pinkln("删除Cache成功 " + key)
		return nil
	}
}
示例#12
0
func Hset(key string, field string, val interface{}, expire int64) error {
	var err error
	data, err := utils.Encode(val)

	if err != nil {
		return err
	}

	if data == nil {
		return errors.New("cc is nil")
	}

	defer utils.Recover("redis set falure")

	err = redis.Hset(key, field, data, expire)
	if err != nil {
		log.Redf("%v", err)
	}

	return err
}
示例#13
0
func Set(key string, val interface{}, expire int64) error {
	var err error
	data, err := Encode(val)

	if err != nil {
		return err
	}

	if data == nil {
		return errors.New("cc is nil")
	}

	defer Regain("redis set falure")

	err = redis.Set(key, data, expire)
	if err != nil {
		log.Redf("%v", err)
	}

	return err
}
示例#14
0
文件: elaeagnus.go 项目: elago/ela
func (ela *Elaeagnus) servController(path string, ctx Context) {
	controller := getController(path)
	if controller == nil {
		servError(&ctx, "<h2>404, File Not Exist</h2>", 404, false)
		return
	}

	routerElement := controller.(uriMode)
	f := routerElement.fun
	params := routerElement.argsMap

	ctx.setURIParam(params)
	if f != nil {

		functions := f

		defer func() {
			if r := recover(); r != nil {

				var stack string
				for i := 1; ; i++ {
					_, file, line, ok := runtime.Caller(i)
					if !ok {
						break
					}
					stack = stack + fmt.Sprintln(fmt.Sprintf("%s:%d", file, line))
				}

				content := "<h2>500 Server Internal Error!</h2>\n\n" + fmt.Sprintf("%s", r) + "\n\n" + "<pre>" + stack + "</pre>"
				log.Redln(r)
				log.Yellowln(stack)

				servError(&ctx, content, 500, false)
				return
			}
		}()

		// execute before controllers
		if beforeController != nil && routerElement.withBefore {
			_, err := ela.injector.injectFuc(beforeController)
			if err != nil {
				log.Redf("injection failed: %s\n", err)
			}
		}

		// execute controllers
		for i := 0; i < len(functions); i++ {
			if !ctx.GetResponseWriter().HasFlushed() {
				function := functions[i]
				_, err := ela.injector.injectFuc(function)
				if err != nil {
					log.Redf("injection failed: %s\n", err)
				}
			}
		}

		// execute after controllers
		if afterController != nil && routerElement.withBefore {
			_, err := ela.injector.injectFuc(afterController)
			if err != nil {
				log.Redf("injection failed: %s\n", err)
			}
		}

	} else {
		// if static-alias does not exist, using default mode
		if staticExist(path) {
			staticServ(path, &ctx)
		} else {
			servError(&ctx, "<h2>404, File Not Exist</h2>", 404, false)
		}
	}
}
示例#15
0
func Regain(err interface{}) {
	if r := recover(); r != nil {
		log.Redf("%v", err)
	}
}