push_app_info_repository.go 1.8 KB
package repository

import (
	"github.com/astaxie/beego/orm"
	"openapi/pkg/domain"
	"openapi/pkg/infrastructure/bgorm/model"
	"sync"
)

var Cache sync.Map

type AppInfoRepository struct {
}

func (repository *AppInfoRepository) FindOne(queryOptions map[string]interface{}) (*domain.AppInfo, error) {
	o := orm.NewOrm()
	model := new(models.PushAppInfo)
	qs := o.QueryTable(model).Filter("project_key", queryOptions["project_key"])
	err := qs.One(model)
	if err != nil {
		return nil, err
	}
	var appInfo *domain.AppInfo
	var ok bool
	if appInfo, ok = getCache(queryOptions["project_key"]); ok {
		return appInfo, nil
	}
	appInfo, err = repository.transformBgormModelToDomainModel(model)
	setCache(queryOptions["project_key"], appInfo)
	return appInfo, err
}

func (repository *AppInfoRepository) Find(queryOptions map[string]interface{}) (rsp []*domain.AppInfo, err error) {
	return
}

func (repository *AppInfoRepository) transformBgormModelToDomainModel(model *models.PushAppInfo) (*domain.AppInfo, error) {
	return &domain.AppInfo{
		Id:               model.Id,
		AppKey:           model.AppKey,
		AppMasterSecret:  model.AppMasterSecret,
		AppId:            model.AppId,
		ProjectName:      model.ProjectName,
		ProjectKey:       model.ProjectKey,
		ProjectMasterKey: model.ProjectMasterKey,
	}, nil
}

func NewAppInfoRepository(transactionContext interface{}) (*AppInfoRepository, error) {
	if transactionContext == nil {
		return &AppInfoRepository{}, nil
	} else {
		return &AppInfoRepository{}, nil
	}
}

//设置缓存
func setCache(key interface{}, value interface{}) {
	Cache.Store(key, value)
}

//设置缓存
func getCache(key interface{}) (*domain.AppInfo, bool) {
	v, ok := Cache.Load(key)
	if !ok {
		return nil, false
	}
	if appInfo, ok := v.(*domain.AppInfo); ok {
		return appInfo, true
	}
	return nil, false
}