Jelajahi Sumber

Merge branch 'master' of http://120.55.44.4:10080/gaoyagang/GtDataStore

songxiaohang 1 tahun lalu
induk
melakukan
c6d507d7c3

+ 1 - 1
app/cmd/events/etc/events.yaml

@@ -13,7 +13,7 @@ Log:
 GtServerIp: 172.16.69.188:8900
 
 RabbitMQ:
-  Url: amqp://admin:devmq*1120@47.96.12.136:5672/
+  Url: amqp://mq_admin:promq*1130@172.16.0.194:5672/
 
 DtDataStoreDB:
   DataSource: ws_data:c712f89fc4f8edaf30e41b828f4e3d26@tcp(172.16.0.194:4000)/ws_data?charset=utf8mb4&parseTime=True&loc=&loc=Asia%2FShanghai

+ 6 - 0
app/cmd/events/internal/logic/handler/single.go

@@ -11,6 +11,7 @@ import (
 	"metawant.greentech.com.cn/gaoyagang/gt-common/envitem"
 	"metawant.greentech.com.cn/gaoyagang/gt-common/identify"
 	"sync"
+	"time"
 )
 
 func EventSingle(ctx context.Context, task *job.Task, technologyName string) error {
@@ -75,10 +76,15 @@ func EventSingle(ctx context.Context, task *job.Task, technologyName string) err
 }
 
 func findSingleRecord(svcCtx *svc.ServiceContext, projectId int64) ([]model.DcEventBind, error) {
+	if data := eventBindCacheTable.GetCache(EVENT_SINGLE_ITEM); data != nil {
+		return data, nil
+	}
+
 	if binds, err := svcCtx.DcEventBind.FindByProjectIdRuleFlag(context.Background(), projectId, EVENT_SINGLE_ITEM); err != nil {
 		logx.Error("findSingleRecord not found record")
 		return nil, err
 	} else {
+		eventBindCacheTable.SetCache(EVENT_SINGLE_ITEM, binds, time.Now().Add(300*time.Second))
 		return binds, nil
 	}
 }

+ 32 - 0
app/cmd/events/internal/logic/handler/vars.go

@@ -2,6 +2,7 @@ package handler
 
 import (
 	"GtDataStore/app/cmd/events/internal/logic/job"
+	"GtDataStore/app/model"
 	"context"
 	"metawant.greentech.com.cn/gaoyagang/gt-common/rabbitMQ"
 	"time"
@@ -28,6 +29,13 @@ type (
 		Time       time.Time // 新值时间
 		MsgTime    time.Time // 消息时间
 	}
+
+	EventBindCache struct {
+		Expire time.Time
+		Data   []model.DcEventBind
+	}
+
+	EventBindCacheMap map[string]EventBindCache
 )
 
 var (
@@ -43,8 +51,32 @@ var (
 		EVENT_SINGLE_ITEM: EventSingle,
 		EVENT_MULTI_ITEM:  EventMulti,
 	}
+
+	eventBindCacheTable = EventBindCacheMap{
+		EVENT_SINGLE_ITEM: EventBindCache{},
+		EVENT_MULTI_ITEM:  EventBindCache{},
+	}
 )
 
+func (t EventBindCacheMap) GetCache(technologyName string) []model.DcEventBind {
+	if cache, ok := t[technologyName]; ok {
+		if time.Now().After(cache.Expire) {
+			t[technologyName] = EventBindCache{}
+			return nil
+		}
+		return cache.Data
+	} else {
+		return nil
+	}
+}
+
+func (t EventBindCacheMap) SetCache(technologyName string, data []model.DcEventBind, expire time.Time) {
+	t[technologyName] = EventBindCache{
+		Expire: expire,
+		Data:   data,
+	}
+}
+
 func init() {
 	eventChan = make(chan eventInfo, 50)
 }

+ 17 - 0
app/cmd/events/internal/logic/handler/vars_test.go

@@ -0,0 +1,17 @@
+package handler
+
+import (
+	"GtDataStore/app/model"
+	"testing"
+	"time"
+)
+
+func TestEventBindCacheMap_SetCache(t *testing.T) {
+	eventBindCacheTable.SetCache("xxxx", make([]model.DcEventBind, 5), time.Now().Add(10*time.Second))
+
+	for {
+		time.Sleep(1 * time.Second)
+
+		t.Log(eventBindCacheTable.GetCache("xxxx"))
+	}
+}

+ 4 - 0
app/cmd/organization/internal/logic/handler/chest.go

@@ -45,10 +45,14 @@ func DeviceChest(ctx context.Context, task *job.Task, technologyName string) err
 }
 
 func findDeviceChest(svcCtx *svc.ServiceContext, projectId int64) ([]model.DcDeviceBind, error) {
+	if data := deviceBindCacheTable.GetCache(DEVICE_CHEST); data != nil {
+		return data, nil
+	}
 	if devices, err := svcCtx.DeviceBind.FindByProjectIdDeviceType(context.Background(), projectId, DEVICE_CHEST); err != nil {
 		logx.Infof("findDeviceChest not found devices")
 		return nil, err
 	} else {
+		deviceBindCacheTable.SetCache(DEVICE_CHEST, devices, time.Now().Add(300*time.Second))
 		return devices, nil
 	}
 }

+ 5 - 1
app/cmd/organization/internal/logic/handler/mf.go

@@ -45,10 +45,14 @@ func DeviceMf(ctx context.Context, task *job.Task, technologyName string) error
 }
 
 func findDeviceMf(svcCtx *svc.ServiceContext, projectId int64) ([]model.DcDeviceBind, error) {
-	if devices, err := svcCtx.DeviceBind.FindByProjectIdDeviceType(context.Background(), projectId, DEVICE_UF); err != nil {
+	if data := deviceBindCacheTable.GetCache(DEVICE_MF); data != nil {
+		return data, nil
+	}
+	if devices, err := svcCtx.DeviceBind.FindByProjectIdDeviceType(context.Background(), projectId, DEVICE_MF); err != nil {
 		logx.Infof("findDeviceMf not found devices")
 		return nil, err
 	} else {
+		deviceBindCacheTable.SetCache(DEVICE_MF, devices, time.Now().Add(300*time.Second))
 		return devices, nil
 	}
 }

+ 5 - 1
app/cmd/organization/internal/logic/handler/nf.go

@@ -45,10 +45,14 @@ func DeviceNf(ctx context.Context, task *job.Task, technologyName string) error
 }
 
 func findDeviceNf(svcCtx *svc.ServiceContext, projectId int64) ([]model.DcDeviceBind, error) {
-	if devices, err := svcCtx.DeviceBind.FindByProjectIdDeviceType(context.Background(), projectId, DEVICE_UF); err != nil {
+	if data := deviceBindCacheTable.GetCache(DEVICE_NF); data != nil {
+		return data, nil
+	}
+	if devices, err := svcCtx.DeviceBind.FindByProjectIdDeviceType(context.Background(), projectId, DEVICE_NF); err != nil {
 		logx.Infof("findDeviceNf not found devices")
 		return nil, err
 	} else {
+		deviceBindCacheTable.SetCache(DEVICE_NF, devices, time.Now().Add(300*time.Second))
 		return devices, nil
 	}
 }

+ 4 - 0
app/cmd/organization/internal/logic/handler/pump.go

@@ -45,10 +45,14 @@ func DevicePump(ctx context.Context, task *job.Task, technologyName string) erro
 }
 
 func findDevicePump(svcCtx *svc.ServiceContext, projectId int64) ([]model.DcDeviceBind, error) {
+	if data := deviceBindCacheTable.GetCache(DEVICE_PUMP); data != nil {
+		return data, nil
+	}
 	if devices, err := svcCtx.DeviceBind.FindByProjectIdDeviceType(context.Background(), projectId, DEVICE_PUMP); err != nil {
 		logx.Infof("findDevicePump not found devices")
 		return nil, err
 	} else {
+		deviceBindCacheTable.SetCache(DEVICE_PUMP, devices, time.Now().Add(300*time.Second))
 		return devices, nil
 	}
 }

+ 4 - 0
app/cmd/organization/internal/logic/handler/ro.go

@@ -45,10 +45,14 @@ func DeviceRo(ctx context.Context, task *job.Task, technologyName string) error
 }
 
 func findDeviceRo(svcCtx *svc.ServiceContext, projectId int64) ([]model.DcDeviceBind, error) {
+	if data := deviceBindCacheTable.GetCache(DEVICE_RO); data != nil {
+		return data, nil
+	}
 	if devices, err := svcCtx.DeviceBind.FindByProjectIdDeviceType(context.Background(), projectId, DEVICE_RO); err != nil {
 		logx.Infof("findDeviceRo not found devices")
 		return nil, err
 	} else {
+		deviceBindCacheTable.SetCache(DEVICE_RO, devices, time.Now().Add(300*time.Second))
 		return devices, nil
 	}
 }

+ 4 - 0
app/cmd/organization/internal/logic/handler/uf.go

@@ -45,10 +45,14 @@ func DeviceUf(ctx context.Context, task *job.Task, technologyName string) error
 }
 
 func findDeviceUf(svcCtx *svc.ServiceContext, projectId int64) ([]model.DcDeviceBind, error) {
+	if data := deviceBindCacheTable.GetCache(DEVICE_UF); data != nil {
+		return data, nil
+	}
 	if devices, err := svcCtx.DeviceBind.FindByProjectIdDeviceType(context.Background(), projectId, DEVICE_UF); err != nil {
 		logx.Infof("findDeviceUf not found devices")
 		return nil, err
 	} else {
+		deviceBindCacheTable.SetCache(DEVICE_UF, devices, time.Now().Add(300*time.Second))
 		return devices, nil
 	}
 }

+ 4 - 0
app/cmd/organization/internal/logic/handler/valve.go

@@ -45,10 +45,14 @@ func DeviceValve(ctx context.Context, task *job.Task, technologyName string) err
 }
 
 func findDeviceValve(svcCtx *svc.ServiceContext, projectId int64) ([]model.DcDeviceBind, error) {
+	if data := deviceBindCacheTable.GetCache(DEVICE_VALVE); data != nil {
+		return data, nil
+	}
 	if devices, err := svcCtx.DeviceBind.FindByProjectIdDeviceType(context.Background(), projectId, DEVICE_VALVE); err != nil {
 		logx.Infof("findDeviceValve not found devices")
 		return nil, err
 	} else {
+		deviceBindCacheTable.SetCache(DEVICE_VALVE, devices, time.Now().Add(300*time.Second))
 		return devices, nil
 	}
 }

+ 39 - 0
app/cmd/organization/internal/logic/handler/vars.go

@@ -2,6 +2,7 @@ package handler
 
 import (
 	"GtDataStore/app/cmd/organization/internal/logic/job"
+	"GtDataStore/app/model"
 	"context"
 	"time"
 )
@@ -16,6 +17,15 @@ const (
 	DEVICE_CHEST = "chest"
 )
 
+type (
+	DeviceBindCache struct {
+		Expire time.Time
+		Data   []model.DcDeviceBind
+	}
+
+	DeviceBindCacheMap map[string]DeviceBindCache
+)
+
 var (
 	DeviceIntervalTable = map[string]time.Duration{
 		DEVICE_UF:    1 * time.Second,
@@ -36,4 +46,33 @@ var (
 		DEVICE_PUMP:  DevicePump,
 		DEVICE_CHEST: DeviceChest,
 	}
+
+	deviceBindCacheTable = DeviceBindCacheMap{
+		DEVICE_UF:    DeviceBindCache{},
+		DEVICE_MF:    DeviceBindCache{},
+		DEVICE_NF:    DeviceBindCache{},
+		DEVICE_RO:    DeviceBindCache{},
+		DEVICE_VALVE: DeviceBindCache{},
+		DEVICE_PUMP:  DeviceBindCache{},
+		DEVICE_CHEST: DeviceBindCache{},
+	}
 )
+
+func (t DeviceBindCacheMap) GetCache(technologyName string) []model.DcDeviceBind {
+	if cache, ok := t[technologyName]; ok {
+		if time.Now().After(cache.Expire) {
+			t[technologyName] = DeviceBindCache{}
+			return nil
+		}
+		return cache.Data
+	} else {
+		return nil
+	}
+}
+
+func (t DeviceBindCacheMap) SetCache(technologyName string, data []model.DcDeviceBind, expire time.Time) {
+	t[technologyName] = DeviceBindCache{
+		Expire: expire,
+		Data:   data,
+	}
+}