func decrUserActiveWeight() { logger.Debugln("start decr user active weight...") loginTime := time.Now().Add(-72 * time.Hour) userList, err := logic.DefaultUser.FindNotLoginUsers(loginTime) if err != nil { logger.Errorln("获取最近未登录用户失败:", err) return } logger.Debugln("need dealing users:", len(userList)) for _, user := range userList { divide := 5 if err == nil { hours := (loginTime.Sub(user.LoginTime) / 24).Hours() if hours < 24 { divide = 2 } else if hours < 48 { divide = 3 } else if hours < 72 { divide = 4 } } logger.Debugln("decr user weight, username:"******"divide:", divide) logic.DefaultUser.DecrUserWeight("username", user.Username, divide) } logger.Debugln("end decr user active weight...") }
func (this *views) Flush() { logger.Debugln("start views flush") this.locker.Lock() defer this.locker.Unlock() // TODO:量大时,考虑copy一份,然后异步 入库,以免堵塞 锁 太久 for _, view := range this.data { view.flush() } this.data = make(map[string]*view) this.users = make(map[string]bool) logger.Debugln("end views flush") }
func (this *UploaderLogic) uploadLocalFile(localFile, key string) (err error) { this.genUpToken() var ret io.PutRet var extra = &io.PutExtra{ // Params: params, // MimeType: mieType, // Crc32: crc32, // CheckCrc: CheckCrc, } // ret 变量用于存取返回的信息,详情见 io.PutRet // uptoken 为业务服务器生成的上传口令 // key 为文件存储的标识(文件名) // localFile 为本地文件名 // extra 为上传文件的额外信息,详情见 io.PutExtra,可选 err = io.PutFile(nil, &ret, this.uptoken, key, localFile, extra) if err != nil { //上传产生错误 logger.Errorln("io.PutFile failed:", err) return } //上传成功,处理返回值 logger.Debugln(ret.Hash, ret.Key) return }
// SaveReading 保存晨读 func (ReadingLogic) SaveReading(ctx context.Context, form url.Values, username string) (errMsg string, err error) { reading := &model.MorningReading{} err = schemaDecoder.Decode(reading, form) if err != nil { logger.Errorln("reading SaveReading error", err) errMsg = err.Error() return } reading.Moreurls = strings.TrimSpace(reading.Moreurls) if strings.Contains(reading.Moreurls, "\n") { reading.Moreurls = strings.Join(strings.Split(reading.Moreurls, "\n"), ",") } reading.Username = username logger.Debugln(reading.Rtype, "id=", reading.Id) if reading.Id != 0 { _, err = MasterDB.Update(reading) } else { _, err = MasterDB.Insert(reading) } if err != nil { errMsg = "内部服务器错误" logger.Errorln("reading save:", errMsg, ":", err) return } return }
func (UserLogic) DecrUserWeight(field string, value interface{}, divide int) { if divide <= 0 { return } strSql := fmt.Sprintf("UPDATE user_active SET weight=weight/%d WHERE %s=?", divide, field) if result, err := MasterDB.Exec(strSql, value); err != nil { logger.Errorln("UserActive update Error:", err) } else { n, _ := result.RowsAffected() logger.Debugln(strSql, "affected num:", n) } }
// EchoCache 用于 echo 框架的缓存中间件。支持自定义 cache 数量 func EchoCache(cacheMaxEntryNum ...int) echo.MiddlewareFunc { if len(cacheMaxEntryNum) > 0 { LruCache = nosql.NewLRUCache(cacheMaxEntryNum[0]) } return func(next echo.HandlerFunc) echo.HandlerFunc { return func(ctx echo.Context) error { req := ctx.Request() if req.Method() == "GET" { cacheKey := getCacheKey(ctx) if cacheKey != "" { ctx.Set(nosql.CacheKey, cacheKey) value, compressor, ok := LruCache.GetAndUnCompress(cacheKey) if ok { cacheData, ok := compressor.(*nosql.CacheData) if ok { // 1分钟更新一次 if time.Now().Sub(cacheData.StoreTime) >= time.Minute { // TODO:雪崩问题处理 goto NEXT } logger.Debugln("cache hit:", cacheData.StoreTime, "now:", time.Now()) return ctx.JSONBlob(http.StatusOK, value) } } } } NEXT: if err := next(ctx); err != nil { return err } return nil } } }
func (this *UploaderLogic) uploadMemoryFile(r gio.Reader, key string) (err error) { this.genUpToken() var ret io.PutRet var extra = &io.PutExtra{ // Params: params, // MimeType: mieType, // Crc32: crc32, // CheckCrc: CheckCrc, } // ret 变量用于存取返回的信息,详情见 io.PutRet // uptoken 为业务服务器端生成的上传口令 // key 为文件存储的标识 // r 为io.Reader类型,用于从其读取数据 // extra 为上传文件的额外信息,可为空, 详情见 io.PutExtra, 可选 err = io.Put(nil, &ret, this.uptoken, key, r, extra) // 上传产生错误 if err != nil { logger.Errorln("io.Put failed:", err) errInfo := make(map[string]interface{}) err = json.Unmarshal([]byte(err.Error()), &errInfo) if err != nil { logger.Errorln("io.Put Unmarshal failed:", err) return } code, ok := errInfo["code"] if ok && code == 614 { err = nil } return } // 上传成功,处理返回值 logger.Debugln(ret.Hash, ret.Key) return }