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 }
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 } }
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 }
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 }
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 }
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 !") } } }
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 } }
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) } }
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) } }
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()) } }
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 } }
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 }
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 }
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) } } }
func Regain(err interface{}) { if r := recover(); r != nil { log.Redf("%v", err) } }