Browse Source

搜索缓存修改

WH01243 2 years ago
parent
commit
a1cd05239f

+ 10 - 1
Makefile

@@ -11,4 +11,13 @@ genApi:
 
 tidy:
 	go mod tidy
-	@echo "===========> tidy finish"
+	@echo "===========> tidy finish"go test -coverprofile=coverage
+
+coverage:
+	cd service &&
+	@echo "===========> coverage finish"
+
+
+coverageHtml:
+	cd service &&  go tool cover -html=coverage -o coverage.html
+	@echo "===========> coverageHtml finish"

+ 5 - 1
api/messagecenter/internal/logic/updatereadbyidlogic.go

@@ -27,7 +27,11 @@ func NewUpdateReadByIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Up
 }
 
 func (l *UpdateReadByIdLogic) UpdateReadById(req *types.ReadStateReq) (*types.CommonRes, error) {
-	readStateReq := &messagecenter.ReadStateReq{MessageId: quitl.Int64All(encrypt.SE.Decode4Hex(req.MessageId))}
+	readStateReq := &messagecenter.ReadStateReq{
+		MessageId: quitl.Int64All(encrypt.SE.Decode4Hex(req.MessageId)),
+		EntUserId: req.EntUserId,
+		NewUserId: req.NewUserId,
+	}
 	resp, err := l.svcCtx.Message.UpdateReadById(l.ctx, readStateReq)
 	if err != nil {
 		return nil, err

+ 2 - 0
api/messagecenter/internal/types/types.go

@@ -79,4 +79,6 @@ type ShuntReq struct {
 
 type ReadStateReq struct {
 	MessageId string `json:"messageId"`
+	EntUserId int64  `header:"entUserId,optional"`
+	NewUserId int64  `header:"newUserId"`
 }

+ 2 - 0
api/messagecenter/messagecenter.api

@@ -69,6 +69,8 @@ type ShuntReq {
 }
 type ReadStateReq {
 	MessageId string `json:"messageId"`
+	EntUserId int64  `header:"entUserId,optional"`
+	NewUserId int64  `header:"newUserId"`
 }
 service messagecenter-api {
 	@handler messageCount

+ 6 - 1
entity/db.go

@@ -14,5 +14,10 @@ type MysqlMainStruct struct {
 }
 
 var (
-	Mysql *mysql.Mysql
+	Mysql        *mysql.Mysql
+	SurvivalTime int
 )
+
+type RedisStuct struct {
+	Addr []string `json:"addr"`
+}

+ 34 - 1
entity/util.go

@@ -1,6 +1,11 @@
 package entity
 
-import quitl "app.yhyue.com/moapp/jybase/common"
+import (
+	quitl "app.yhyue.com/moapp/jybase/common"
+	"app.yhyue.com/moapp/jybase/redis"
+	"encoding/json"
+	"fmt"
+)
 
 const (
 	Date_Full_Layout          = "2006-01-02 15:04:05"
@@ -14,6 +19,7 @@ const (
 	SUCCESS_CODE = int64(0)
 	ERROR_CODE   = int64(1)
 )
+const redisModule = "msgCount"
 
 func SafeConvert2String(obj interface{}) string {
 	if obj != nil {
@@ -36,3 +42,30 @@ func Inhandle(data *[]map[string]interface{}) (messId string) {
 	}
 	return
 }
+
+type SubPush struct {
+	Data  map[string]interface{} `json:"data"`
+	Count int                    `json:"count"`
+}
+
+// 获取redis key
+func todayKey(userType int64, userId int64) string {
+	return fmt.Sprintf("%s_%d_%d", "messageCount", userType, userId)
+}
+func GetData(userType int64, userId int64) (*SubPush, error) {
+	pc_a, err := redis.GetNewBytes(redisModule, todayKey(userType, userId))
+	if err != nil {
+		return nil, err
+	}
+	if pc_a == nil {
+		return nil, nil
+	}
+	var p *SubPush
+	if err := json.Unmarshal(*pc_a, &p); err != nil {
+		return nil, err
+	}
+	return p, nil
+}
+func SetData(userType int64, userId int64, data map[string]interface{}, survivalTime int) {
+	redis.Put(redisModule, todayKey(userType, userId), data, survivalTime)
+}

+ 1 - 0
go.mod

@@ -5,6 +5,7 @@ go 1.16
 require (
 	app.yhyue.com/moapp/jybase v0.0.0-20220722023023-2a57d1ee061f
 	bp.jydev.jianyu360.cn/BaseService/gateway v1.3.4
+	bp.jydev.jianyu360.cn/BaseService/userCenter v0.0.0-20220421015128-4a36f3eac5c5
 	github.com/go-sql-driver/mysql v1.6.0
 	github.com/zeromicro/go-zero v1.3.5
 	google.golang.org/grpc v1.47.0

+ 3 - 0
go.sum

@@ -11,8 +11,10 @@ bp.jydev.jianyu360.cn/BaseService/gateway v1.3.4 h1:zl5eZrKDBENVVBUiPpzyQQ0/SBdG
 bp.jydev.jianyu360.cn/BaseService/gateway v1.3.4/go.mod h1:BMLd/5wb3BIEGhnEgF9y1sJN9P5/Dw9kYsoiE9V8I9g=
 bp.jydev.jianyu360.cn/BaseService/resourceCenter v0.0.0-20220418005748-8ba5d936dd53/go.mod h1:E5lcDI3k4FESLxiAetCfWQTq8qfpy9cv0yN1oKoEO34=
 bp.jydev.jianyu360.cn/BaseService/resourceCenter v0.0.0-20220419023723-0b32d4a41751/go.mod h1:6KL5LMEku83uRbre0W/bj5kXG2I6pJGBFtktmtp51yM=
+bp.jydev.jianyu360.cn/BaseService/resourceCenter v0.0.0-20220420075831-0b59892e9982 h1:+56m/dW9ZW0YEXgAakeIIxqUFefufzp5XckU4iEuIFM=
 bp.jydev.jianyu360.cn/BaseService/resourceCenter v0.0.0-20220420075831-0b59892e9982/go.mod h1:wsHNO91h37H+xE4ZNny0yd7mtpODeDJxbVYhIRMR+qw=
 bp.jydev.jianyu360.cn/BaseService/userCenter v0.0.0-20220418072311-2062bed1e700/go.mod h1:KjcrxTzM96tBc6G4B8tlLBn1lrVy5UJYF8+eTdP4xAE=
+bp.jydev.jianyu360.cn/BaseService/userCenter v0.0.0-20220421015128-4a36f3eac5c5 h1:ovbEGGgn3rJM70KjqvLyUn5geZx8QTlanrjFfjYyNGI=
 bp.jydev.jianyu360.cn/BaseService/userCenter v0.0.0-20220421015128-4a36f3eac5c5/go.mod h1:GT0QC4aaKDuXxAvaU4G02XjCc31TU1ctqBGqxQYOfC4=
 cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
 cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
@@ -157,6 +159,7 @@ github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMo
 github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
 github.com/fsnotify/fsnotify v1.5.1 h1:mZcQUHVQUQWoPXXtuf9yuEXKudkV2sx1E06UadKWpgI=
 github.com/fsnotify/fsnotify v1.5.1/go.mod h1:T3375wBYaZdLLcVNkcVbzGHY7f1l/uK5T5Ai1i3InKU=
+github.com/garyburd/redigo v1.6.2 h1:yE/pwKCrbLpLpQICzYTeZ7JsTA/C53wFTJHaEtRqniM=
 github.com/garyburd/redigo v1.6.2/go.mod h1:NR3MbYisc3/PwhQ00EMzDiPmrwpPxAn5GI05/YaO1SY=
 github.com/ghodss/yaml v0.0.0-20150909031657-73d445a93680/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
 github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=

+ 3 - 0
rpc/messagecenter/etc/db.yaml

@@ -0,0 +1,3 @@
+redis:
+    addr:
+        - other=127.0.0.1:6379

+ 2 - 2
rpc/messagecenter/etc/messagecenter.yaml

@@ -13,9 +13,9 @@ MysqlMain:
   passWord: =PDT49#80Z!RVv52_z
   maxOpenConns: 5
   maxIdleConns: 5
-
 TestConf:
   Etcd:
     Hosts:
       - 127.0.0.1:2379
-    Key: messagecenter.rpc
+    Key: messagecenter.rpc
+SurvivalTime: 86400

+ 13 - 4
rpc/messagecenter/init/init.go

@@ -2,6 +2,7 @@ package init
 
 import (
 	"app.yhyue.com/moapp/jybase/mysql"
+	"app.yhyue.com/moapp/jybase/redis"
 	"bp.jydev.jianyu360.cn/SocialPlatform/messageCenter/entity"
 	"bp.jydev.jianyu360.cn/SocialPlatform/messageCenter/rpc/messagecenter/internal/config"
 	"flag"
@@ -9,18 +10,19 @@ import (
 	"github.com/zeromicro/go-zero/core/conf"
 	"github.com/zeromicro/go-zero/core/logx"
 	"log"
+	"strings"
 )
 
 var configF = flag.String("ff", "etc/messagecenter.yaml", "the config file")
+var C config.Config
 
-//
 var logFile = flag.String("lf", "etc/logs.yaml", "the config file")
 var logc entity.Logc
 
-var C config.Config
+var dbFile = flag.String("df", "etc/db.yaml", "the db file")
+var DB config.Db
 
 func init() {
-	log.Println(11111111)
 	conf.MustLoad(*configF, &C)
 	//初始化mysql
 	mm := C.MysqlMain
@@ -37,7 +39,13 @@ func init() {
 		entity.Mysql.Init()
 
 	}
-
+	//初始化数据库配置
+	conf.MustLoad(*dbFile, &DB)
+	rm := DB.Redis
+	if len(rm.Addr) > 0 {
+		logx.Info("--初始化 redis--")
+		redis.InitRedisBySize(strings.Join(rm.Addr, ","), 100, 30, 300)
+	}
 	//初始化日志信息
 	conf.MustLoad(*logFile, &logc)
 	if len(logc.Level) > 0 {
@@ -51,4 +59,5 @@ func init() {
 			logx.Info(v, "--日志记录")
 		}
 	}
+	entity.SurvivalTime = C.SurvivalTime
 }

+ 8 - 3
rpc/messagecenter/internal/config/config.go

@@ -7,7 +7,12 @@ import (
 
 type Config struct {
 	zrpc.RpcServerConf
-	WebRpcPort int64
-	MysqlMain  entity.MysqlMainStruct
-	TestConf   zrpc.RpcClientConf
+	WebRpcPort   int64
+	MysqlMain    entity.MysqlMainStruct
+	TestConf     zrpc.RpcClientConf
+	SurvivalTime int
+}
+
+type Db struct {
+	Redis entity.RedisStuct `json:"redis"`
 }

+ 1 - 1
rpc/messagecenter/internal/logic/countlogic.go

@@ -29,7 +29,7 @@ func NewCountLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CountLogic
 func (l *CountLogic) Count(in *messagecenter.CountReq) (*messagecenter.CountResp, error) {
 	// todo: add your logic here and delete this line
 	m := service.MessaggeService{}
-	count, last, err := m.Count(in.NewUserId, in.UserType, in.EntUserId)
+	count, last, err := m.Count(in.NewUserId, in.UserType, in.EntUserId, false)
 	if err != nil {
 		return nil, err
 	}

+ 0 - 2
rpc/messagecenter/messagecenter.go

@@ -20,10 +20,8 @@ func main() {
 			log.Println("ListenAndServe: ", err)
 		}
 	}()
-
 	ctx := svc.NewServiceContext(IC.C)
 	srv := server.NewMessageCenterServer(ctx)
-
 	s := zrpc.MustNewServer(IC.C.RpcServerConf, func(grpcServer *grpc.Server) {
 		messagecenter.RegisterMessageCenterServer(grpcServer, srv)
 	})

+ 2 - 0
rpc/messagecenter/messagecenter.proto

@@ -111,6 +111,8 @@ message SaveAutoReplyReq {
 }
 message ReadStateReq {
   int64         messageId = 1;
+  int64         entUserId=2;
+  int64         newUserId=3;
 }
 
 message CurrencyResp {

+ 63 - 43
rpc/messagecenter/messagecenter/messagecenter.pb.go

@@ -1165,6 +1165,8 @@ type ReadStateReq struct {
 	unknownFields protoimpl.UnknownFields
 
 	MessageId int64 `protobuf:"varint,1,opt,name=messageId,proto3" json:"messageId,omitempty"`
+	EntUserId int64 `protobuf:"varint,2,opt,name=entUserId,proto3" json:"entUserId,omitempty"`
+	NewUserId int64 `protobuf:"varint,3,opt,name=newUserId,proto3" json:"newUserId,omitempty"`
 }
 
 func (x *ReadStateReq) Reset() {
@@ -1206,6 +1208,20 @@ func (x *ReadStateReq) GetMessageId() int64 {
 	return 0
 }
 
+func (x *ReadStateReq) GetEntUserId() int64 {
+	if x != nil {
+		return x.EntUserId
+	}
+	return 0
+}
+
+func (x *ReadStateReq) GetNewUserId() int64 {
+	if x != nil {
+		return x.NewUserId
+	}
+	return 0
+}
+
 type CurrencyResp struct {
 	state         protoimpl.MessageState
 	sizeCache     protoimpl.SizeCache
@@ -1419,54 +1435,58 @@ var file_messageCenter_proto_rawDesc = []byte{
 	0x74, 0x65, 0x6e, 0x74, 0x12, 0x14, 0x0a, 0x05, 0x61, 0x70, 0x70, 0x49, 0x64, 0x18, 0x06, 0x20,
 	0x01, 0x28, 0x09, 0x52, 0x05, 0x61, 0x70, 0x70, 0x49, 0x64, 0x12, 0x1c, 0x0a, 0x09, 0x6e, 0x6f,
 	0x77, 0x46, 0x6f, 0x72, 0x6d, 0x61, 0x74, 0x18, 0x07, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x6e,
-	0x6f, 0x77, 0x46, 0x6f, 0x72, 0x6d, 0x61, 0x74, 0x22, 0x2c, 0x0a, 0x0c, 0x52, 0x65, 0x61, 0x64,
+	0x6f, 0x77, 0x46, 0x6f, 0x72, 0x6d, 0x61, 0x74, 0x22, 0x68, 0x0a, 0x0c, 0x52, 0x65, 0x61, 0x64,
 	0x53, 0x74, 0x61, 0x74, 0x65, 0x52, 0x65, 0x71, 0x12, 0x1c, 0x0a, 0x09, 0x6d, 0x65, 0x73, 0x73,
 	0x61, 0x67, 0x65, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x09, 0x6d, 0x65, 0x73,
-	0x73, 0x61, 0x67, 0x65, 0x49, 0x64, 0x22, 0x4a, 0x0a, 0x0c, 0x43, 0x75, 0x72, 0x72, 0x65, 0x6e,
-	0x63, 0x79, 0x52, 0x65, 0x73, 0x70, 0x12, 0x1d, 0x0a, 0x0a, 0x65, 0x72, 0x72, 0x6f, 0x72, 0x5f,
-	0x63, 0x6f, 0x64, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x03, 0x52, 0x09, 0x65, 0x72, 0x72, 0x6f,
-	0x72, 0x43, 0x6f, 0x64, 0x65, 0x12, 0x1b, 0x0a, 0x09, 0x65, 0x72, 0x72, 0x6f, 0x72, 0x5f, 0x6d,
-	0x73, 0x67, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x65, 0x72, 0x72, 0x6f, 0x72, 0x4d,
-	0x73, 0x67, 0x32, 0xe0, 0x04, 0x0a, 0x0d, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x43, 0x65,
-	0x6e, 0x74, 0x65, 0x72, 0x12, 0x3a, 0x0a, 0x05, 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x12, 0x17, 0x2e,
-	0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x63, 0x65, 0x6e, 0x74, 0x65, 0x72, 0x2e, 0x43, 0x6f,
-	0x75, 0x6e, 0x74, 0x52, 0x65, 0x71, 0x1a, 0x18, 0x2e, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65,
-	0x63, 0x65, 0x6e, 0x74, 0x65, 0x72, 0x2e, 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x52, 0x65, 0x73, 0x70,
-	0x12, 0x3b, 0x0a, 0x08, 0x55, 0x73, 0x65, 0x72, 0x4c, 0x69, 0x73, 0x74, 0x12, 0x16, 0x2e, 0x6d,
-	0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x63, 0x65, 0x6e, 0x74, 0x65, 0x72, 0x2e, 0x55, 0x73, 0x65,
-	0x72, 0x52, 0x65, 0x71, 0x1a, 0x17, 0x2e, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x63, 0x65,
-	0x6e, 0x74, 0x65, 0x72, 0x2e, 0x55, 0x73, 0x65, 0x72, 0x52, 0x65, 0x73, 0x70, 0x12, 0x44, 0x0a,
-	0x0b, 0x46, 0x69, 0x6e, 0x64, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x12, 0x19, 0x2e, 0x6d,
-	0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x63, 0x65, 0x6e, 0x74, 0x65, 0x72, 0x2e, 0x4d, 0x65, 0x73,
-	0x73, 0x61, 0x67, 0x65, 0x52, 0x65, 0x71, 0x1a, 0x1a, 0x2e, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67,
-	0x65, 0x63, 0x65, 0x6e, 0x74, 0x65, 0x72, 0x2e, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x52,
-	0x65, 0x73, 0x70, 0x12, 0x4b, 0x0a, 0x0b, 0x53, 0x61, 0x76, 0x65, 0x4d, 0x65, 0x73, 0x73, 0x61,
-	0x67, 0x65, 0x12, 0x1c, 0x2e, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x63, 0x65, 0x6e, 0x74,
-	0x65, 0x72, 0x2e, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x45, 0x6e, 0x74, 0x69, 0x74, 0x79,
+	0x73, 0x61, 0x67, 0x65, 0x49, 0x64, 0x12, 0x1c, 0x0a, 0x09, 0x65, 0x6e, 0x74, 0x55, 0x73, 0x65,
+	0x72, 0x49, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x03, 0x52, 0x09, 0x65, 0x6e, 0x74, 0x55, 0x73,
+	0x65, 0x72, 0x49, 0x64, 0x12, 0x1c, 0x0a, 0x09, 0x6e, 0x65, 0x77, 0x55, 0x73, 0x65, 0x72, 0x49,
+	0x64, 0x18, 0x03, 0x20, 0x01, 0x28, 0x03, 0x52, 0x09, 0x6e, 0x65, 0x77, 0x55, 0x73, 0x65, 0x72,
+	0x49, 0x64, 0x22, 0x4a, 0x0a, 0x0c, 0x43, 0x75, 0x72, 0x72, 0x65, 0x6e, 0x63, 0x79, 0x52, 0x65,
+	0x73, 0x70, 0x12, 0x1d, 0x0a, 0x0a, 0x65, 0x72, 0x72, 0x6f, 0x72, 0x5f, 0x63, 0x6f, 0x64, 0x65,
+	0x18, 0x02, 0x20, 0x01, 0x28, 0x03, 0x52, 0x09, 0x65, 0x72, 0x72, 0x6f, 0x72, 0x43, 0x6f, 0x64,
+	0x65, 0x12, 0x1b, 0x0a, 0x09, 0x65, 0x72, 0x72, 0x6f, 0x72, 0x5f, 0x6d, 0x73, 0x67, 0x18, 0x01,
+	0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x65, 0x72, 0x72, 0x6f, 0x72, 0x4d, 0x73, 0x67, 0x32, 0xe0,
+	0x04, 0x0a, 0x0d, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x43, 0x65, 0x6e, 0x74, 0x65, 0x72,
+	0x12, 0x3a, 0x0a, 0x05, 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x12, 0x17, 0x2e, 0x6d, 0x65, 0x73, 0x73,
+	0x61, 0x67, 0x65, 0x63, 0x65, 0x6e, 0x74, 0x65, 0x72, 0x2e, 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x52,
+	0x65, 0x71, 0x1a, 0x18, 0x2e, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x63, 0x65, 0x6e, 0x74,
+	0x65, 0x72, 0x2e, 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x52, 0x65, 0x73, 0x70, 0x12, 0x3b, 0x0a, 0x08,
+	0x55, 0x73, 0x65, 0x72, 0x4c, 0x69, 0x73, 0x74, 0x12, 0x16, 0x2e, 0x6d, 0x65, 0x73, 0x73, 0x61,
+	0x67, 0x65, 0x63, 0x65, 0x6e, 0x74, 0x65, 0x72, 0x2e, 0x55, 0x73, 0x65, 0x72, 0x52, 0x65, 0x71,
+	0x1a, 0x17, 0x2e, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x63, 0x65, 0x6e, 0x74, 0x65, 0x72,
+	0x2e, 0x55, 0x73, 0x65, 0x72, 0x52, 0x65, 0x73, 0x70, 0x12, 0x44, 0x0a, 0x0b, 0x46, 0x69, 0x6e,
+	0x64, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x12, 0x19, 0x2e, 0x6d, 0x65, 0x73, 0x73, 0x61,
+	0x67, 0x65, 0x63, 0x65, 0x6e, 0x74, 0x65, 0x72, 0x2e, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65,
+	0x52, 0x65, 0x71, 0x1a, 0x1a, 0x2e, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x63, 0x65, 0x6e,
+	0x74, 0x65, 0x72, 0x2e, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x52, 0x65, 0x73, 0x70, 0x12,
+	0x4b, 0x0a, 0x0b, 0x53, 0x61, 0x76, 0x65, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x12, 0x1c,
+	0x2e, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x63, 0x65, 0x6e, 0x74, 0x65, 0x72, 0x2e, 0x4d,
+	0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x45, 0x6e, 0x74, 0x69, 0x74, 0x79, 0x1a, 0x1e, 0x2e, 0x6d,
+	0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x63, 0x65, 0x6e, 0x74, 0x65, 0x72, 0x2e, 0x53, 0x61, 0x76,
+	0x65, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x52, 0x65, 0x73, 0x70, 0x12, 0x52, 0x0a, 0x11,
+	0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x43, 0x68, 0x61, 0x74, 0x53, 0x65, 0x73, 0x73, 0x69, 0x6f,
+	0x6e, 0x12, 0x1d, 0x2e, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x63, 0x65, 0x6e, 0x74, 0x65,
+	0x72, 0x2e, 0x43, 0x68, 0x61, 0x74, 0x53, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x71,
 	0x1a, 0x1e, 0x2e, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x63, 0x65, 0x6e, 0x74, 0x65, 0x72,
-	0x2e, 0x53, 0x61, 0x76, 0x65, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x52, 0x65, 0x73, 0x70,
-	0x12, 0x52, 0x0a, 0x11, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x43, 0x68, 0x61, 0x74, 0x53, 0x65,
-	0x73, 0x73, 0x69, 0x6f, 0x6e, 0x12, 0x1d, 0x2e, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x63,
-	0x65, 0x6e, 0x74, 0x65, 0x72, 0x2e, 0x43, 0x68, 0x61, 0x74, 0x53, 0x65, 0x73, 0x73, 0x69, 0x6f,
+	0x2e, 0x43, 0x68, 0x61, 0x74, 0x53, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x73, 0x70,
+	0x12, 0x52, 0x0a, 0x10, 0x43, 0x6c, 0x6f, 0x73, 0x65, 0x43, 0x68, 0x61, 0x74, 0x53, 0x65, 0x73,
+	0x73, 0x69, 0x6f, 0x6e, 0x12, 0x1e, 0x2e, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x63, 0x65,
+	0x6e, 0x74, 0x65, 0x72, 0x2e, 0x43, 0x6c, 0x6f, 0x73, 0x65, 0x53, 0x65, 0x73, 0x73, 0x69, 0x6f,
 	0x6e, 0x52, 0x65, 0x71, 0x1a, 0x1e, 0x2e, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x63, 0x65,
 	0x6e, 0x74, 0x65, 0x72, 0x2e, 0x43, 0x68, 0x61, 0x74, 0x53, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e,
-	0x52, 0x65, 0x73, 0x70, 0x12, 0x52, 0x0a, 0x10, 0x43, 0x6c, 0x6f, 0x73, 0x65, 0x43, 0x68, 0x61,
-	0x74, 0x53, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x12, 0x1e, 0x2e, 0x6d, 0x65, 0x73, 0x73, 0x61,
-	0x67, 0x65, 0x63, 0x65, 0x6e, 0x74, 0x65, 0x72, 0x2e, 0x43, 0x6c, 0x6f, 0x73, 0x65, 0x53, 0x65,
-	0x73, 0x73, 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x71, 0x1a, 0x1e, 0x2e, 0x6d, 0x65, 0x73, 0x73, 0x61,
-	0x67, 0x65, 0x63, 0x65, 0x6e, 0x74, 0x65, 0x72, 0x2e, 0x43, 0x68, 0x61, 0x74, 0x53, 0x65, 0x73,
-	0x73, 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x73, 0x70, 0x12, 0x4f, 0x0a, 0x10, 0x53, 0x61, 0x76, 0x65,
-	0x41, 0x75, 0x74, 0x6f, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x4d, 0x73, 0x67, 0x12, 0x1f, 0x2e, 0x6d,
-	0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x63, 0x65, 0x6e, 0x74, 0x65, 0x72, 0x2e, 0x53, 0x61, 0x76,
-	0x65, 0x41, 0x75, 0x74, 0x6f, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x52, 0x65, 0x71, 0x1a, 0x1a, 0x2e,
-	0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x63, 0x65, 0x6e, 0x74, 0x65, 0x72, 0x2e, 0x4d, 0x65,
-	0x73, 0x73, 0x61, 0x67, 0x65, 0x52, 0x65, 0x73, 0x70, 0x12, 0x4a, 0x0a, 0x0e, 0x55, 0x70, 0x64,
-	0x61, 0x74, 0x65, 0x52, 0x65, 0x61, 0x64, 0x42, 0x79, 0x49, 0x64, 0x12, 0x1b, 0x2e, 0x6d, 0x65,
-	0x73, 0x73, 0x61, 0x67, 0x65, 0x63, 0x65, 0x6e, 0x74, 0x65, 0x72, 0x2e, 0x52, 0x65, 0x61, 0x64,
-	0x53, 0x74, 0x61, 0x74, 0x65, 0x52, 0x65, 0x71, 0x1a, 0x1b, 0x2e, 0x6d, 0x65, 0x73, 0x73, 0x61,
-	0x67, 0x65, 0x63, 0x65, 0x6e, 0x74, 0x65, 0x72, 0x2e, 0x43, 0x75, 0x72, 0x72, 0x65, 0x6e, 0x63,
-	0x79, 0x52, 0x65, 0x73, 0x70, 0x42, 0x11, 0x5a, 0x0f, 0x2e, 0x2f, 0x6d, 0x65, 0x73, 0x73, 0x61,
-	0x67, 0x65, 0x63, 0x65, 0x6e, 0x74, 0x65, 0x72, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
+	0x52, 0x65, 0x73, 0x70, 0x12, 0x4f, 0x0a, 0x10, 0x53, 0x61, 0x76, 0x65, 0x41, 0x75, 0x74, 0x6f,
+	0x52, 0x65, 0x70, 0x6c, 0x79, 0x4d, 0x73, 0x67, 0x12, 0x1f, 0x2e, 0x6d, 0x65, 0x73, 0x73, 0x61,
+	0x67, 0x65, 0x63, 0x65, 0x6e, 0x74, 0x65, 0x72, 0x2e, 0x53, 0x61, 0x76, 0x65, 0x41, 0x75, 0x74,
+	0x6f, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x52, 0x65, 0x71, 0x1a, 0x1a, 0x2e, 0x6d, 0x65, 0x73, 0x73,
+	0x61, 0x67, 0x65, 0x63, 0x65, 0x6e, 0x74, 0x65, 0x72, 0x2e, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67,
+	0x65, 0x52, 0x65, 0x73, 0x70, 0x12, 0x4a, 0x0a, 0x0e, 0x55, 0x70, 0x64, 0x61, 0x74, 0x65, 0x52,
+	0x65, 0x61, 0x64, 0x42, 0x79, 0x49, 0x64, 0x12, 0x1b, 0x2e, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67,
+	0x65, 0x63, 0x65, 0x6e, 0x74, 0x65, 0x72, 0x2e, 0x52, 0x65, 0x61, 0x64, 0x53, 0x74, 0x61, 0x74,
+	0x65, 0x52, 0x65, 0x71, 0x1a, 0x1b, 0x2e, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x63, 0x65,
+	0x6e, 0x74, 0x65, 0x72, 0x2e, 0x43, 0x75, 0x72, 0x72, 0x65, 0x6e, 0x63, 0x79, 0x52, 0x65, 0x73,
+	0x70, 0x42, 0x11, 0x5a, 0x0f, 0x2e, 0x2f, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x63, 0x65,
+	0x6e, 0x74, 0x65, 0x72, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
 }
 
 var (

+ 5 - 18
rpc/messagecenter/test/message_test.go

@@ -6,7 +6,6 @@ import (
 	"bp.jydev.jianyu360.cn/SocialPlatform/messageCenter/rpc/messagecenter/messagecenter"
 	"context"
 	"flag"
-	"fmt"
 	"github.com/zeromicro/go-zero/core/conf"
 	"github.com/zeromicro/go-zero/zrpc"
 	"log"
@@ -23,6 +22,7 @@ func init() {
 
 //数量查询
 func Test_Count(t *testing.T) {
+	log.Println(encrypt.SE.Encode2Hex("831"))
 	ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
 	TestSystem := messagecenter.NewMessageCenter(zrpc.MustNewClient(c.TestConf))
 	req := &messagecenter.CountReq{
@@ -90,21 +90,8 @@ func Test_SaveMessage(t *testing.T) {
 
 //
 func Test_test1(t *testing.T) {
-	a := encrypt.SE.Encode2Hex("1")
-	fmt.Println(a)
-	fmt.Println(encrypt.SE.Decode4Hex(a))
-	fmt.Println(encrypt.SE.Decode4Hex("4657465d5d"))
-	fmt.Println(encrypt.SE.Decode4Hex("4157435b52"))
-
-	/*for d := 0; d <10 ; d++ {
-		c:=0
-		for i := 0; i <= 10000; i++ {
-			b:=rand.New(rand.NewSource(time.Now().UnixNano())).Int63n(100)
-			if b<=50{
-				c++
-			}
-		}
-		fmt.Println(c)
-	}*/
-
+	a := encrypt.SE.EncodeString("0")
+	log.Println(a)
+	log.Println(encrypt.SE.DecodeString("0"))
+	log.Println(encrypt.SE.DecodeString(""))
 }

+ 86 - 8
service/message_mail_box.go

@@ -1,23 +1,30 @@
 package service
 
 import (
+	quitl "app.yhyue.com/moapp/jybase/common"
+	util "bp.jydev.jianyu360.cn/SocialPlatform/messageCenter/entity"
+	"bp.jydev.jianyu360.cn/SocialPlatform/messageCenter/rpc/messagecenter/messagecenter"
 	"database/sql"
 	"fmt"
 	"log"
 	"time"
-
-	quitl "app.yhyue.com/moapp/jybase/common"
-	util "bp.jydev.jianyu360.cn/SocialPlatform/messageCenter/entity"
-	"bp.jydev.jianyu360.cn/SocialPlatform/messageCenter/rpc/messagecenter/messagecenter"
 )
 
 type MessaggeService struct{}
 
 //未读消息查询
-func (b MessaggeService) Count(newUserId, userType, entUserId int64) (count int, last map[string]interface{}, err error) {
+func (b MessaggeService) Count(newUserId, userType, entUserId int64, isClean bool) (count int, last map[string]interface{}, err error) {
 	v := make([]interface{}, 0)
 	sqlStr := ""
 	if userType == 1 {
+		//查询缓存里边是否有数据
+		pc_a, err := util.GetData(userType, entUserId)
+		if err == nil && pc_a != nil {
+			// 缓存有值
+			if !isClean {
+				return pc_a.Count, pc_a.Data, err
+			}
+		}
 		sqlStr = fmt.Sprintf("select  count(b.id) from   %s a  "+
 			"LEFT  JOIN  %s  b ON a.id=b.own_id "+
 			"where  b.type=5 "+
@@ -27,6 +34,12 @@ func (b MessaggeService) Count(newUserId, userType, entUserId int64) (count int,
 			"order by create_time", util.SOCIALIZE_CHAT_SESSION, util.SOCIALIZE_MESSAGE_MAILBOX, entUserId)
 
 	} else {
+		pc_a, err := util.GetData(userType, newUserId)
+		if err == nil && pc_a != nil {
+			if !isClean {
+				return pc_a.Count, pc_a.Data, err
+			}
+		}
 		sqlStr = fmt.Sprintf("select  count(b.id) from    %s  b "+
 			"where   b.send_user_id != %d    "+
 			"AND   b.own_id=%d  "+
@@ -52,6 +65,17 @@ func (b MessaggeService) Count(newUserId, userType, entUserId int64) (count int,
 	lastData := util.Mysql.SelectBySql(sqlStr)
 	if len(*lastData) > 0 {
 		last = (*lastData)[0]
+		if userType == 1 {
+			util.SetData(userType, entUserId, map[string]interface{}{"data": last, "count": count}, util.SurvivalTime)
+		} else {
+			util.SetData(userType, newUserId, map[string]interface{}{"data": last, "count": count}, util.SurvivalTime)
+		}
+	} else {
+		if userType == 1 {
+			util.SetData(userType, entUserId, map[string]interface{}{"data": map[string]interface{}{}, "count": count}, util.SurvivalTime)
+		} else {
+			util.SetData(userType, newUserId, map[string]interface{}{"data": map[string]interface{}{}, "count": count}, util.SurvivalTime)
+		}
 	}
 	return
 }
@@ -73,7 +97,7 @@ func (b MessaggeService) UserList(in *messagecenter.UserReq) (data *[]map[string
 		data = util.Mysql.SelectBySql(userSql)
 		userMessageId := util.Inhandle(data)
 		//客服最后一次信息查询
-		customerSql := fmt.Sprintf("SELECT   max( c.id )   as messageId  FROM   %s c "+
+		customerSql := fmt.Sprintf("SELECT   MAX( c.id )   as messageId  FROM   %s c "+
 			"LEFT JOIN %s d ON  IF   ( c.send_user_type = 1, d.id = c.send_user_id, d.id = c.receive_user_id ) "+
 			"WHERE   c.own_id = %d  "+
 			"AND ( c.type = 4 OR c.type = 5  or c.type=6 or  c.type=7 ) "+
@@ -218,6 +242,9 @@ func (b MessaggeService) SaveMessage(in *messagecenter.MessageEntity) (fool bool
 	}
 	fool = util.Mysql.ExecTx("聊天信息保存", func(tx *sql.Tx) bool {
 		//先插入信息表
+		create_time := nowForm.Format(util.Date_Full_Layout)
+		userType := int64(1)
+		userId := int64(0)
 		message := map[string]interface{}{
 			"appid":         in.Appid,
 			"title":         in.Title,
@@ -225,12 +252,19 @@ func (b MessaggeService) SaveMessage(in *messagecenter.MessageEntity) (fool bool
 			"item":          in.Item,
 			"type":          in.Type,
 			"link":          in.Link,
-			"create_time":   nowForm.Format(util.Date_Full_Layout),
+			"create_time":   create_time,
 			"create_person": in.SendId,
 		}
+		data := map[string]interface{}{
+			"item":        in.Item,
+			"type":        in.Type,
+			"create_time": create_time,
+			"content":     in.Content,
+		}
 		ok := util.Mysql.InsertByTx(tx, util.SOCIALIZE_MESSAGE, message)
 		receiveOk := int64(0)
 		messageId = ok
+		data["id"] = ok
 		//在插入邮箱表socialize_message_mailbox
 		messageMailBox := map[string]interface{}{
 			"appid":         in.Appid,
@@ -294,6 +328,8 @@ func (b MessaggeService) SaveMessage(in *messagecenter.MessageEntity) (fool bool
 				messageMailBox["own_id"] = in.ReceiveId
 				messageMailBox["send_user_id"] = in.NewUserId
 				messageMailBox["receive_user_id"] = in.ReceiveId
+				userType = 1
+				userId = in.EntUserId
 			} else {
 				//客服发送(用户接收信息)
 				messageMailBox["own_type"] = 2
@@ -308,6 +344,8 @@ func (b MessaggeService) SaveMessage(in *messagecenter.MessageEntity) (fool bool
 					messageMailBox["isread"] = 1
 					messageMailBox["read_time"] = nowForm.Format(util.Date_Full_Layout)
 				}
+				userType = 2
+				userId = in.NewUserId
 			}
 		} else {
 			messageMailBox["own_type"] = 2
@@ -316,6 +354,8 @@ func (b MessaggeService) SaveMessage(in *messagecenter.MessageEntity) (fool bool
 			messageMailBox["own_id"] = in.ReceiveId
 			messageMailBox["send_user_id"] = in.NewUserId
 			messageMailBox["receive_user_id"] = in.ReceiveId
+			userType = 2
+			userId = in.ReceiveId
 		}
 		receiveOk = util.Mysql.InsertByTx(tx, util.SOCIALIZE_MESSAGE_MAILBOX, messageMailBox)
 		messageMailBox = map[string]interface{}{
@@ -361,6 +401,14 @@ func (b MessaggeService) SaveMessage(in *messagecenter.MessageEntity) (fool bool
 			messageMailBox["receive_user_id"] = in.ReceiveId
 		}
 		receiveOk = util.Mysql.InsertByTx(tx, util.SOCIALIZE_MESSAGE_MAILBOX, messageMailBox)
+		if ok > 1 && receiveOk > 1 {
+			pc_a, err := util.GetData(userType, userId)
+			count := 1
+			if err == nil && pc_a != nil {
+				count += pc_a.Count
+			}
+			util.SetData(userType, userId, map[string]interface{}{"data": data, "count": count}, util.SurvivalTime)
+		}
 		return ok > 1 && receiveOk > 1
 	})
 	return fool, "", in.Content, messageId, nowForm.Unix()
@@ -440,7 +488,8 @@ func (b MessaggeService) FindMessage(in *messagecenter.MessageReq) *[]map[string
 	}
 	go func() {
 		updateMap := map[string]interface{}{}
-		if len(*data) > 0 {
+		//if len(*data) > 0 && data != nil {
+		if true {
 			//未读信息修改
 			switch in.MsgType {
 			case 2: //点对点聊天
@@ -471,6 +520,8 @@ func (b MessaggeService) FindMessage(in *messagecenter.MessageReq) *[]map[string
 				util.Mysql.UpdateOrDeleteBySql(sqlStr)
 				break
 			}
+			//redis缓存处理
+			b.Count(in.NewUserId, in.UserType, in.EntUserId, true)
 		}
 	}()
 	return data
@@ -559,6 +610,33 @@ func (b MessaggeService) UpdateReadById(in *messagecenter.ReadStateReq) bool {
 			"isread":    0,
 		}
 		fool := util.Mysql.Update(util.SOCIALIZE_MESSAGE_MAILBOX, updateMap, map[string]interface{}{"read_time": time.Now().Local().Format(util.Date_Full_Layout), "isread": 1})
+		if fool {
+			//查询此条信息拥有者
+			data := util.Mysql.FindOne(util.SOCIALIZE_MESSAGE_MAILBOX, updateMap, "receive_user_type", "")
+			if data != nil {
+				userType := int64(1)
+				userId := int64(0)
+				if (*data)["receive_user_type"] == 2 {
+					userType = 2
+					userId = in.EntUserId
+				} else {
+					userType = 1
+					userId = in.NewUserId
+				}
+				pc_a, err := util.GetData(userType, userId)
+				if fool {
+					if err == nil && pc_a != nil {
+						//id一致
+						if in.MessageId == pc_a.Data["id"] {
+							util.SetData(userType, userId, map[string]interface{}{"data": map[string]interface{}{}, "count": pc_a.Count - 1}, util.SurvivalTime)
+						} else {
+							util.SetData(userType, userId, map[string]interface{}{"data": data, "count": pc_a.Count - 1}, util.SurvivalTime)
+						}
+					}
+				}
+
+			}
+		}
 		return fool
 	})
 	return fool

+ 8 - 5
service/message_mail_box_test.go

@@ -2,10 +2,12 @@ package service
 
 import (
 	"app.yhyue.com/moapp/jybase/mysql"
+	"app.yhyue.com/moapp/jybase/redis"
 	"bp.jydev.jianyu360.cn/SocialPlatform/messageCenter/entity"
 	"bp.jydev.jianyu360.cn/SocialPlatform/messageCenter/rpc/messagecenter/messagecenter"
 	"log"
 	"reflect"
+	"strings"
 	"testing"
 )
 
@@ -19,6 +21,7 @@ func initMysql() {
 		MaxIdleConns: 5,
 	}
 	entity.Mysql.Init()
+	redis.InitRedisBySize(strings.Join([]string{"other=127.0.0.1:6379"}, ","), 100, 30, 300)
 }
 func init() {
 	initMysql()
@@ -230,9 +233,9 @@ func TestMessaggeService_FindMessage(t *testing.T) {
 			args: args{in: &messagecenter.MessageReq{
 				MsgType:   2,
 				UserType:  2,
-				SendId:    57683,
+				SendId:    10,
 				PageSize:  10,
-				NewUserId: 10,
+				NewUserId: 831,
 				EntId:     1,
 				LastId:    200000,
 			}},
@@ -392,12 +395,12 @@ func TestMessaggeService_SaveMessage(t *testing.T) {
 			args: args{
 				in: &messagecenter.MessageEntity{
 					Title:     "11",
-					Content:   "问在吗?",
+					Content:   "问在吗11111?",
 					Item:      8,
 					ItemType:  2,
 					Link:      "1111",
 					Appid:     "10000",
-					ReceiveId: 57683,
+					ReceiveId: 831,
 					OwnType:   int64(2),
 					Type:      1,
 					NewUserId: 10,
@@ -423,7 +426,7 @@ func TestMessaggeService_SaveMessage(t *testing.T) {
 			args: args{
 				in: &messagecenter.MessageEntity{
 					Title:     "11",
-					Content:   "问在吗?",
+					Content:   "问在吗111?",
 					Item:      8,
 					ItemType:  6,
 					Link:      "1111",