优化统计相关程序

This commit is contained in:
GoEdgeLab
2023-03-12 10:20:56 +08:00
parent 9388ffce6c
commit d5a807b51f
11 changed files with 43 additions and 287 deletions

View File

@@ -20,8 +20,6 @@ const (
type RegionCityDAO dbs.DAO
var regionCityNameAndIdCacheMap = map[string]int64{} // city name @ province id => id
func NewRegionCityDAO() *RegionCityDAO {
return dbs.NewDAO(&RegionCityDAO{
DAOObject: dbs.DAOObject{
@@ -119,37 +117,6 @@ func (this *RegionCityDAO) FindCityIdWithName(tx *dbs.Tx, provinceId int64, city
FindInt64Col(0)
}
// FindCityIdWithNameCacheable 根据城市名查找城市ID并加入缓存
func (this *RegionCityDAO) FindCityIdWithNameCacheable(tx *dbs.Tx, provinceId int64, cityName string) (int64, error) {
key := cityName + "@" + numberutils.FormatInt64(provinceId)
SharedCacheLocker.RLock()
cityId, ok := regionCityNameAndIdCacheMap[key]
if ok {
SharedCacheLocker.RUnlock()
return cityId, nil
}
SharedCacheLocker.RUnlock()
cityId, err := this.Query(tx).
Attr("provinceId", provinceId).
Where("(name=:cityName OR customName=:cityName OR JSON_CONTAINS(codes, :cityNameJSON) OR JSON_CONTAINS(customCodes, :cityNameJSON))").
Param("cityName", cityName).
Param("cityNameJSON", strconv.Quote(cityName)). // 查询的需要是个JSON字符串所以这里加双引号
ResultPk().
FindInt64Col(0)
if err != nil {
return 0, err
}
if cityId > 0 {
SharedCacheLocker.Lock()
regionCityNameAndIdCacheMap[key] = cityId
SharedCacheLocker.Unlock()
}
return cityId, nil
}
// FindAllEnabledCities 获取所有城市信息
func (this *RegionCityDAO) FindAllEnabledCities(tx *dbs.Tx) (result []*RegionCity, err error) {
_, err = this.Query(tx).
@@ -179,12 +146,6 @@ func (this *RegionCityDAO) UpdateCityCustom(tx *dbs.Tx, cityId int64, customName
return err
}
defer func() {
SharedCacheLocker.Lock()
regionCityNameAndIdCacheMap = map[string]int64{}
SharedCacheLocker.Unlock()
}()
return this.Query(tx).
Pk(cityId).
Set("customName", customName).

View File

@@ -3,20 +3,4 @@ package regions
import (
_ "github.com/go-sql-driver/mysql"
_ "github.com/iwind/TeaGo/bootstrap"
"github.com/iwind/TeaGo/dbs"
"testing"
"time"
)
func TestRegionCityDAO_FindCityIdWithCityNameCacheable(t *testing.T) {
dbs.NotifyReady()
for i := 0; i < 5; i++ {
now := time.Now()
cityId, err := SharedRegionCityDAO.FindCityIdWithNameCacheable(nil, 1, "北京市")
if err != nil {
t.Fatal(err)
}
t.Log("cityId", cityId, time.Since(now).Seconds()*1000, "ms")
}
}

View File

@@ -24,7 +24,6 @@ const (
CountryChinaId = 1
)
var regionCountryNameAndIdCacheMap = map[string]int64{} // country name => id
var regionCountryIdAndNameCacheMap = map[int64]string{} // country id => name
type RegionCountryDAO dbs.DAO
@@ -120,30 +119,6 @@ func (this *RegionCountryDAO) FindCountryIdWithName(tx *dbs.Tx, countryName stri
FindInt64Col(0)
}
// FindCountryIdWithNameCacheable 根据国家名查找国家ID并可使用缓存
func (this *RegionCountryDAO) FindCountryIdWithNameCacheable(tx *dbs.Tx, countryName string) (int64, error) {
SharedCacheLocker.RLock()
provinceId, ok := regionCountryNameAndIdCacheMap[countryName]
if ok {
SharedCacheLocker.RUnlock()
return provinceId, nil
}
SharedCacheLocker.RUnlock()
countryId, err := this.FindCountryIdWithName(tx, countryName)
if err != nil {
return 0, err
}
if countryId > 0 {
SharedCacheLocker.Lock()
regionCountryNameAndIdCacheMap[countryName] = countryId
SharedCacheLocker.Unlock()
}
return countryId, nil
}
// CreateCountry 根据数据ID创建国家
func (this *RegionCountryDAO) CreateCountry(tx *dbs.Tx, name string, dataId string) (int64, error) {
var op = NewRegionCountryOperator()
@@ -205,7 +180,6 @@ func (this *RegionCountryDAO) UpdateCountryCustom(tx *dbs.Tx, countryId int64, c
defer func() {
SharedCacheLocker.Lock()
regionCountryNameAndIdCacheMap = map[string]int64{}
regionCountryIdAndNameCacheMap = map[int64]string{}
SharedCacheLocker.Unlock()
}()

View File

@@ -5,7 +5,6 @@ import (
_ "github.com/iwind/TeaGo/bootstrap"
"github.com/iwind/TeaGo/dbs"
"testing"
"time"
)
func TestRegionCountryDAO_FindCountryIdWithName(t *testing.T) {
@@ -26,19 +25,6 @@ func TestRegionCountryDAO_FindCountryIdWithName(t *testing.T) {
}
}
func TestRegionCountryDAO_FindCountryIdWithCountryNameCacheable(t *testing.T) {
dbs.NotifyReady()
for i := 0; i < 5; i++ {
var now = time.Now()
countryId, err := SharedRegionCountryDAO.FindCountryIdWithNameCacheable(nil, "中国")
if err != nil {
t.Fatal(err)
}
t.Log("countryId", countryId, time.Since(now).Seconds()*1000, "ms")
}
}
func TestRegionCountryDAO_FindSimilarCountries(t *testing.T) {
dbs.NotifyReady()

View File

@@ -17,8 +17,6 @@ const (
RegionProviderStateDisabled = 0 // 已禁用
)
var regionProviderNameAndIdCacheMap = map[string]int64{} // provider name => id
type RegionProviderDAO dbs.DAO
func NewRegionProviderDAO() *RegionProviderDAO {
@@ -88,35 +86,6 @@ func (this *RegionProviderDAO) FindProviderIdWithName(tx *dbs.Tx, providerName s
FindInt64Col(0)
}
// FindProviderIdWithNameCacheable 根据服务商名称查找服务商ID并保存进缓存
func (this *RegionProviderDAO) FindProviderIdWithNameCacheable(tx *dbs.Tx, providerName string) (int64, error) {
SharedCacheLocker.RLock()
providerId, ok := regionProviderNameAndIdCacheMap[providerName]
if ok {
SharedCacheLocker.RUnlock()
return providerId, nil
}
SharedCacheLocker.RUnlock()
providerId, err := this.Query(tx).
Where("(name=:providerName OR customName=:providerName OR JSON_CONTAINS(codes, :providerNameJSON) OR JSON_CONTAINS(customCodes, :providerNameJSON))").
Param("providerName", providerName).
Param("providerNameJSON", strconv.Quote(providerName)). // 查询的需要是个JSON字符串所以这里加双引号
ResultPk().
FindInt64Col(0)
if err != nil {
return 0, err
}
if providerId > 0 {
SharedCacheLocker.Lock()
regionProviderNameAndIdCacheMap[providerName] = providerId
SharedCacheLocker.Unlock()
}
return providerId, nil
}
// CreateProvider 创建Provider
func (this *RegionProviderDAO) CreateProvider(tx *dbs.Tx, name string) (int64, error) {
var op = NewRegionProviderOperator()
@@ -149,12 +118,6 @@ func (this *RegionProviderDAO) UpdateProviderCustom(tx *dbs.Tx, providerId int64
return err
}
defer func() {
SharedCacheLocker.Lock()
regionProviderNameAndIdCacheMap = map[string]int64{}
SharedCacheLocker.Unlock()
}()
return this.Query(tx).
Pk(providerId).
Set("customName", customName).

View File

@@ -3,31 +3,4 @@ package regions
import (
_ "github.com/go-sql-driver/mysql"
_ "github.com/iwind/TeaGo/bootstrap"
"github.com/iwind/TeaGo/dbs"
"testing"
"time"
)
func TestRegionProviderDAO_FindProviderIdWithProviderNameCacheable(t *testing.T) {
dbs.NotifyReady()
for i := 0; i < 5; i++ {
now := time.Now()
providerId, err := SharedRegionProviderDAO.FindProviderIdWithNameCacheable(nil, "电信")
if err != nil {
t.Fatal(err)
}
t.Log("providerId", providerId, time.Since(now).Seconds()*1000, "ms")
}
t.Log("=====")
for i := 0; i < 5; i++ {
now := time.Now()
providerId, err := SharedRegionProviderDAO.FindProviderIdWithNameCacheable(nil, "胡乱填的")
if err != nil {
t.Fatal(err)
}
t.Log("providerId", providerId, time.Since(now).Seconds()*1000, "ms")
}
}

View File

@@ -18,8 +18,6 @@ const (
RegionProvinceStateDisabled = 0 // 已禁用
)
var regionProvinceNameAndIdCacheMap = map[string]int64{} // province name @ country id => province id
type RegionProvinceDAO dbs.DAO
func NewRegionProvinceDAO() *RegionProvinceDAO {
@@ -98,31 +96,6 @@ func (this *RegionProvinceDAO) FindProvinceIdWithName(tx *dbs.Tx, countryId int6
FindInt64Col(0)
}
// FindProvinceIdWithNameCacheable 根据省份名查找省份ID并可使用缓存
func (this *RegionProvinceDAO) FindProvinceIdWithNameCacheable(tx *dbs.Tx, countryId int64, provinceName string) (int64, error) {
var key = provinceName + "@" + numberutils.FormatInt64(countryId)
SharedCacheLocker.RLock()
provinceId, ok := regionProvinceNameAndIdCacheMap[key]
if ok {
SharedCacheLocker.RUnlock()
return provinceId, nil
}
SharedCacheLocker.RUnlock()
provinceId, err := this.FindProvinceIdWithName(tx, countryId, provinceName)
if err != nil {
return 0, err
}
if provinceId > 0 {
SharedCacheLocker.Lock()
regionProvinceNameAndIdCacheMap[key] = provinceId
SharedCacheLocker.Unlock()
}
return provinceId, nil
}
// CreateProvince 创建省份
func (this *RegionProvinceDAO) CreateProvince(tx *dbs.Tx, countryId int64, name string, dataId string) (int64, error) {
var op = NewRegionProvinceOperator()
@@ -175,13 +148,6 @@ func (this *RegionProvinceDAO) UpdateProvinceCustom(tx *dbs.Tx, provinceId int64
return err
}
// 清空缓存
defer func() {
SharedCacheLocker.Lock()
regionProvinceNameAndIdCacheMap = map[string]int64{}
SharedCacheLocker.Unlock()
}()
return this.Query(tx).
Pk(provinceId).
Set("customName", customName).

View File

@@ -4,32 +4,8 @@ import (
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/dbs"
"testing"
"time"
)
func TestRegionProvinceDAO_FindProvinceIdWithNameCacheable(t *testing.T) {
dbs.NotifyReady()
for i := 0; i < 5; i++ {
now := time.Now()
provinceId, err := SharedRegionProvinceDAO.FindProvinceIdWithNameCacheable(nil, 1, "安徽省")
if err != nil {
t.Fatal(err)
}
t.Log(provinceId, time.Since(now).Seconds()*1000, "ms")
}
t.Log("====")
for i := 0; i < 5; i++ {
now := time.Now()
provinceId, err := SharedRegionProvinceDAO.FindProvinceIdWithNameCacheable(nil, 2, "安徽省")
if err != nil {
t.Fatal(err)
}
t.Log(provinceId, time.Since(now).Seconds()*1000, "ms")
}
}
func TestRegionProvinceDAO_FindProvinceIdWithName(t *testing.T) {
dbs.NotifyReady()