@@ -0,0 +1,54 @@
|
||||
package topasist
|
||||
|
||||
import (
|
||||
topser "91porn-server/common/top"
|
||||
"91porn-server/common/top/dailytop"
|
||||
"91porn-server/common/top/monthtop"
|
||||
"91porn-server/common/top/weektop"
|
||||
"91porn-server/common/top/yeartop"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
// Incr 增加
|
||||
func Incr(tp topser.TopName, id string, count float64) {
|
||||
// 如果是一个空的id
|
||||
if primitive.NilObjectID.Hex() == id {
|
||||
return
|
||||
}
|
||||
// 日榜
|
||||
dailytop.Incr(tp, id, count)
|
||||
// 周榜
|
||||
weektop.Incr(tp, id, count)
|
||||
// 月榜
|
||||
monthtop.Incr(tp, id, count)
|
||||
// 年榜
|
||||
yeartop.Incr(tp, id, count)
|
||||
}
|
||||
|
||||
// Decr 减少
|
||||
func Decr(tp topser.TopName, id string, count float64) {
|
||||
// 如果是一个空的id
|
||||
if primitive.NilObjectID.Hex() == id {
|
||||
return
|
||||
}
|
||||
// 日榜
|
||||
dailytop.Decr(tp, id, count)
|
||||
// 周榜
|
||||
weektop.Decr(tp, id, count)
|
||||
// 月榜
|
||||
monthtop.Decr(tp, id, count)
|
||||
// 年榜
|
||||
yeartop.Decr(tp, id, count)
|
||||
}
|
||||
|
||||
// Remove 移除排行榜
|
||||
func Remove(tp topser.TopName, id string) {
|
||||
// 日榜
|
||||
dailytop.Remove(tp, id)
|
||||
// 周榜
|
||||
weektop.Remove(tp, id)
|
||||
// 月榜
|
||||
monthtop.Remove(tp, id)
|
||||
// 年榜
|
||||
yeartop.Remove(tp, id)
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
package dailytop
|
||||
|
||||
import (
|
||||
"91porn-server/common/log"
|
||||
"91porn-server/common/redis"
|
||||
services "91porn-server/common/top"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
// getCacheKeys 获取需要操作的key
|
||||
func getCacheKeys(tp services.TopName) []string {
|
||||
t := time.Now().Unix()
|
||||
d := t / 86400
|
||||
var keys []string
|
||||
var i int64
|
||||
for i = 0; i < 2; i++ {
|
||||
keys = append(keys, fmt.Sprintf("daliy_top_%s_%d", tp, d-i))
|
||||
}
|
||||
|
||||
return keys
|
||||
}
|
||||
|
||||
// Incr 增加
|
||||
func Incr(tp services.TopName, id string, count float64) {
|
||||
keys := getCacheKeys(tp)
|
||||
for _, key := range keys {
|
||||
if !redis.Handler.Exists(key) {
|
||||
_, _ = redis.Handler.ZIncrBy(key, count, id)
|
||||
_, _ = redis.Handler.ExpireKey(key, time.Hour*24*3)
|
||||
continue
|
||||
}
|
||||
_, _ = redis.Handler.ZIncrBy(key, count, id)
|
||||
}
|
||||
}
|
||||
|
||||
// Decr 减少
|
||||
func Decr(tp services.TopName, id string, count float64) {
|
||||
keys := getCacheKeys(tp)
|
||||
for _, key := range keys {
|
||||
if !redis.Handler.Exists(key) {
|
||||
_, _ = redis.Handler.ZIncrBy(key, -count, id)
|
||||
_, _ = redis.Handler.ExpireKey(key, time.Hour*24*3)
|
||||
continue
|
||||
}
|
||||
_, _ = redis.Handler.ZIncrBy(key, -count, id)
|
||||
}
|
||||
}
|
||||
|
||||
// Remove 移除
|
||||
func Remove(tp services.TopName, id string) {
|
||||
keys := getCacheKeys(tp)
|
||||
for _, key := range keys {
|
||||
log.Info("Remove key", log.Any("key", key), log.Any("id", id))
|
||||
redis.Handler.ZRem(key, id)
|
||||
}
|
||||
}
|
||||
|
||||
// GetTop 获取排行榜头部
|
||||
func GetTop(tp services.TopName, count int64) ([]string, map[string]int64) {
|
||||
keys := getCacheKeys(tp)
|
||||
key := keys[len(keys)-1]
|
||||
srt, topVal := redis.Handler.ZRevRangeWithScores2(key, 0, count)
|
||||
var ids []string
|
||||
scores := make(map[string]int64)
|
||||
for _, id := range srt {
|
||||
score, ok := topVal[id]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
ids = append(ids, id)
|
||||
scores[id] = int64(score)
|
||||
}
|
||||
|
||||
return ids, scores
|
||||
}
|
||||
|
||||
// GetTopByPage 获取排行榜头部(改成获取前一天的)
|
||||
func GetTopByPage(tp services.TopName, skip, size int64) ([]string, map[string]int64, bool) {
|
||||
keys := getCacheKeys(tp)
|
||||
k := len(keys) - 2 // 获取倒数第二个,也就是前一天
|
||||
if k < 0 {
|
||||
return []string{}, map[string]int64{}, false
|
||||
}
|
||||
key := keys[k]
|
||||
hasNext := false
|
||||
count, err := redis.Handler.ZCard(key)
|
||||
if err != nil {
|
||||
return nil, nil, false
|
||||
}
|
||||
if skip >= count {
|
||||
return nil, nil, false
|
||||
}
|
||||
if count > skip+size {
|
||||
hasNext = true
|
||||
}
|
||||
|
||||
srt, topVal := redis.Handler.ZRevRangeWithScores2(key, skip, size)
|
||||
var ids []string
|
||||
scores := make(map[string]int64)
|
||||
for _, id := range srt {
|
||||
score, ok := topVal[id]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
ids = append(ids, id)
|
||||
scores[id] = int64(score)
|
||||
}
|
||||
|
||||
return ids, scores, hasNext
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
package monthtop
|
||||
|
||||
import (
|
||||
"91porn-server/common/log"
|
||||
"91porn-server/common/redis"
|
||||
services "91porn-server/common/top"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
// getCacheKeys 获取需要操作的key
|
||||
func getCacheKeys(tp services.TopName) []string {
|
||||
t := time.Now().Unix()
|
||||
m := t / 2592000
|
||||
var keys []string
|
||||
var i int64
|
||||
for i = 0; i < 2; i++ {
|
||||
keys = append(keys, fmt.Sprintf("month_top_%s_%d", tp, m-i))
|
||||
}
|
||||
|
||||
return keys
|
||||
}
|
||||
|
||||
// Incr 增加
|
||||
func Incr(tp services.TopName, id string, count float64) {
|
||||
keys := getCacheKeys(tp)
|
||||
for _, key := range keys {
|
||||
if !redis.Handler.Exists(key) {
|
||||
_, _ = redis.Handler.ZIncrBy(key, count, id)
|
||||
_, _ = redis.Handler.ExpireKey(key, time.Hour*24*61)
|
||||
continue
|
||||
}
|
||||
_, _ = redis.Handler.ZIncrBy(key, count, id)
|
||||
}
|
||||
}
|
||||
|
||||
// Decr 减少
|
||||
func Decr(tp services.TopName, id string, count float64) {
|
||||
keys := getCacheKeys(tp)
|
||||
for _, key := range keys {
|
||||
if !redis.Handler.Exists(key) {
|
||||
_, _ = redis.Handler.ZIncrBy(key, -count, id)
|
||||
_, _ = redis.Handler.ExpireKey(key, time.Hour*24*61)
|
||||
continue
|
||||
}
|
||||
_, _ = redis.Handler.ZIncrBy(key, -count, id)
|
||||
}
|
||||
}
|
||||
|
||||
// Remove 移除
|
||||
func Remove(tp services.TopName, id string) {
|
||||
keys := getCacheKeys(tp)
|
||||
for _, key := range keys {
|
||||
log.Info("Remove key", log.Any("key", key), log.Any("id", id))
|
||||
redis.Handler.ZRem(key, id)
|
||||
}
|
||||
}
|
||||
|
||||
// GetTop 获取排行榜头部
|
||||
func GetTop(tp services.TopName, count int64) ([]string, map[string]int64) {
|
||||
keys := getCacheKeys(tp)
|
||||
key := keys[len(keys)-1]
|
||||
srt, topVal := redis.Handler.ZRevRangeWithScores2(key, 0, count)
|
||||
var ids []string
|
||||
scores := make(map[string]int64)
|
||||
for _, id := range srt {
|
||||
score, ok := topVal[id]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
ids = append(ids, id)
|
||||
scores[id] = int64(score)
|
||||
}
|
||||
|
||||
return ids, scores
|
||||
}
|
||||
|
||||
// GetTopByPage 获取排行榜头部
|
||||
func GetTopByPage(tp services.TopName, skip, size int64) ([]string, map[string]int64, bool) {
|
||||
keys := getCacheKeys(tp)
|
||||
key := keys[len(keys)-1]
|
||||
hasNext := false
|
||||
count, err := redis.Handler.ZCard(key)
|
||||
if err != nil {
|
||||
return nil, nil, false
|
||||
}
|
||||
if skip >= count {
|
||||
return nil, nil, false
|
||||
}
|
||||
if count > skip+size {
|
||||
hasNext = true
|
||||
}
|
||||
|
||||
srt, topVal := redis.Handler.ZRevRangeWithScores2(key, skip, size)
|
||||
var ids []string
|
||||
scores := make(map[string]int64)
|
||||
for _, id := range srt {
|
||||
score, ok := topVal[id]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
ids = append(ids, id)
|
||||
scores[id] = int64(score)
|
||||
}
|
||||
|
||||
return ids, scores, hasNext
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package topser
|
||||
|
||||
import "fmt"
|
||||
|
||||
type TopName string
|
||||
|
||||
const (
|
||||
//TypeMovie TopName = "movie"
|
||||
//TypePics TopName = "pics"
|
||||
//TypePost TopName = "post"
|
||||
//TypeCollections TopName = "collections"
|
||||
//TypeActress TopName = "actress"
|
||||
typeMedia = "media:%v"
|
||||
typeVideo = "video:%v"
|
||||
TypeRecommendUser TopName = "RecommendUser"
|
||||
)
|
||||
|
||||
func TypeMedia(mediaType string) TopName {
|
||||
return TopName(fmt.Sprintf(typeMedia, mediaType))
|
||||
}
|
||||
|
||||
func TypeVideo(newsType string) TopName {
|
||||
return TopName(fmt.Sprintf(typeVideo, newsType))
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
package weektop
|
||||
|
||||
import (
|
||||
"91porn-server/common/log"
|
||||
"91porn-server/common/redis"
|
||||
services "91porn-server/common/top"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
// getCacheKeys 获取需要操作的key
|
||||
func getCacheKeys(tp services.TopName) []string {
|
||||
t := time.Now().Unix()
|
||||
d := t / 86400
|
||||
var keys []string
|
||||
var i int64
|
||||
for i = 0; i < 7; i++ {
|
||||
keys = append(keys, fmt.Sprintf("week_top_%s_%d", tp, d-i))
|
||||
}
|
||||
|
||||
return keys
|
||||
}
|
||||
|
||||
// Incr 增加
|
||||
func Incr(tp services.TopName, id string, count float64) {
|
||||
keys := getCacheKeys(tp)
|
||||
for _, key := range keys {
|
||||
if !redis.Handler.Exists(key) {
|
||||
_, _ = redis.Handler.ZIncrBy(key, count, id)
|
||||
_, _ = redis.Handler.ExpireKey(key, time.Hour*24*8)
|
||||
continue
|
||||
}
|
||||
_, _ = redis.Handler.ZIncrBy(key, count, id)
|
||||
}
|
||||
}
|
||||
|
||||
// Decr 减少
|
||||
func Decr(tp services.TopName, id string, count float64) {
|
||||
keys := getCacheKeys(tp)
|
||||
for _, key := range keys {
|
||||
if !redis.Handler.Exists(key) {
|
||||
_, _ = redis.Handler.ZIncrBy(key, -count, id)
|
||||
_, _ = redis.Handler.ExpireKey(key, time.Hour*24*8)
|
||||
continue
|
||||
}
|
||||
_, _ = redis.Handler.ZIncrBy(key, -count, id)
|
||||
}
|
||||
}
|
||||
|
||||
// Remove 移除
|
||||
func Remove(tp services.TopName, id string) {
|
||||
keys := getCacheKeys(tp)
|
||||
for _, key := range keys {
|
||||
log.Info("Remove key", log.Any("key", key), log.Any("id", id))
|
||||
redis.Handler.ZRem(key, id)
|
||||
}
|
||||
}
|
||||
|
||||
// GetTop 获取排行榜头部
|
||||
func GetTop(tp services.TopName, count int64) ([]string, map[string]int64) {
|
||||
keys := getCacheKeys(tp)
|
||||
key := keys[len(keys)-1]
|
||||
srt, topVal := redis.Handler.ZRevRangeWithScores2(key, 0, count)
|
||||
var ids []string
|
||||
scores := make(map[string]int64)
|
||||
for _, id := range srt {
|
||||
score, ok := topVal[id]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
ids = append(ids, id)
|
||||
scores[id] = int64(score)
|
||||
}
|
||||
|
||||
return ids, scores
|
||||
}
|
||||
|
||||
// GetTopByPage 获取排行榜头部
|
||||
func GetTopByPage(tp services.TopName, skip, size int64) ([]string, map[string]int64, bool) {
|
||||
keys := getCacheKeys(tp)
|
||||
key := keys[len(keys)-1]
|
||||
hasNext := false
|
||||
srt, topVal := redis.Handler.ZRevRangeWithScores2(key, skip, size+1)
|
||||
var ids []string
|
||||
if len(srt) > int(size) {
|
||||
hasNext = true
|
||||
srt = srt[:size]
|
||||
}
|
||||
scores := make(map[string]int64)
|
||||
for _, id := range srt {
|
||||
score, ok := topVal[id]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
ids = append(ids, id)
|
||||
scores[id] = int64(score)
|
||||
}
|
||||
|
||||
return ids, scores, hasNext
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
package yeartop
|
||||
|
||||
import (
|
||||
"91porn-server/common/log"
|
||||
"91porn-server/common/redis"
|
||||
services "91porn-server/common/top"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
// getCacheKeys 获取需要操作的key
|
||||
func getCacheKeys(tp services.TopName) []string {
|
||||
y := time.Now().Year()
|
||||
var keys []string
|
||||
var i int
|
||||
for i = 0; i < 2; i++ {
|
||||
keys = append(keys, fmt.Sprintf("year_top_%s_%d", tp, y-i))
|
||||
}
|
||||
|
||||
return keys
|
||||
}
|
||||
|
||||
// Incr 增加
|
||||
func Incr(tp services.TopName, id string, count float64) {
|
||||
keys := getCacheKeys(tp)
|
||||
for _, key := range keys {
|
||||
if !redis.Handler.Exists(key) {
|
||||
_, _ = redis.Handler.ZIncrBy(key, count, id)
|
||||
continue
|
||||
}
|
||||
_, _ = redis.Handler.ZIncrBy(key, count, id)
|
||||
}
|
||||
}
|
||||
|
||||
// Decr 减少
|
||||
func Decr(tp services.TopName, id string, count float64) {
|
||||
keys := getCacheKeys(tp)
|
||||
for _, key := range keys {
|
||||
if !redis.Handler.Exists(key) {
|
||||
_, _ = redis.Handler.ZIncrBy(key, -count, id)
|
||||
continue
|
||||
}
|
||||
_, _ = redis.Handler.ZIncrBy(key, -count, id)
|
||||
}
|
||||
}
|
||||
|
||||
// Remove 移除
|
||||
func Remove(tp services.TopName, id string) {
|
||||
keys := getCacheKeys(tp)
|
||||
for _, key := range keys {
|
||||
log.Info("Remove key", log.Any("key", key), log.Any("id", id))
|
||||
redis.Handler.ZRem(key, id)
|
||||
}
|
||||
}
|
||||
|
||||
// GetTop 获取排行榜头部
|
||||
func GetTop(tp services.TopName, count int64) ([]string, map[string]int64) {
|
||||
keys := getCacheKeys(tp)
|
||||
key := keys[len(keys)-1]
|
||||
srt, topVal := redis.Handler.ZRevRangeWithScores2(key, 0, count)
|
||||
var ids []string
|
||||
scores := make(map[string]int64)
|
||||
for _, id := range srt {
|
||||
score, ok := topVal[id]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
ids = append(ids, id)
|
||||
scores[id] = int64(score)
|
||||
}
|
||||
|
||||
return ids, scores
|
||||
}
|
||||
|
||||
// GetTopByPage 获取排行榜头部
|
||||
func GetTopByPage(tp services.TopName, skip, size int64) ([]string, map[string]int64, bool) {
|
||||
keys := getCacheKeys(tp)
|
||||
key := keys[len(keys)-1]
|
||||
hasNext := false
|
||||
count, err := redis.Handler.ZCard(key)
|
||||
if err != nil {
|
||||
return nil, nil, false
|
||||
}
|
||||
if skip >= count {
|
||||
return nil, nil, false
|
||||
}
|
||||
if count > skip+size {
|
||||
hasNext = true
|
||||
}
|
||||
|
||||
srt, topVal := redis.Handler.ZRevRangeWithScores2(key, skip, size)
|
||||
var ids []string
|
||||
scores := make(map[string]int64)
|
||||
for _, id := range srt {
|
||||
score, ok := topVal[id]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
ids = append(ids, id)
|
||||
scores[id] = int64(score)
|
||||
}
|
||||
|
||||
return ids, scores, hasNext
|
||||
}
|
||||
Reference in New Issue
Block a user