Skip to content

Commit

Permalink
fix bug: get query returns empty struct if cache hit .
Browse files Browse the repository at this point in the history
  • Loading branch information
ruizeng committed Jan 19, 2016
1 parent fec21ea commit ec653d2
Showing 1 changed file with 61 additions and 30 deletions.
91 changes: 61 additions & 30 deletions services/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,50 @@ func NewRegistry() (*Registry, error) {
}, nil
}

func setVendor(target *models.Vendor, src *models.Vendor) {
target.ID = src.ID
target.VendorName = src.VendorName
target.VendorDescription = src.VendorDescription
target.VendorKey = src.VendorKey
target.CreatedAt = src.CreatedAt
target.UpdatedAt = src.UpdatedAt
}

func setProduct(target *models.Product, src *models.Product) {
target.ID = src.ID
target.ProductName = src.ProductName
target.ProductDescription = src.ProductDescription
target.ProductKey = src.ProductKey
target.ProductConfig = src.ProductConfig
target.CreatedAt = src.CreatedAt
target.UpdatedAt = src.UpdatedAt
}

func setApplication(target *models.Application, src *models.Application) {
target.ID = src.ID
target.AppName = src.AppName
target.AppDescription = src.AppDescription
target.AppKey = src.AppKey
target.ReportUrl = src.ReportUrl
target.AppToken = src.AppToken
target.AppDomain = src.AppDomain
target.CreatedAt = src.CreatedAt
target.UpdatedAt = src.UpdatedAt
}

func setDevice(target *models.Device, src *models.Device) {
target.ID = src.ID
target.ProductID = src.ProductID
target.DeviceIdentifier = src.DeviceIdentifier
target.DeviceSecret = src.DeviceIdentifier
target.DeviceKey = src.DeviceKey
target.DeviceName = src.DeviceName
target.DeviceDescription = src.DeviceDescription
target.DeviceVersion = src.DeviceVersion
target.CreatedAt = src.CreatedAt
target.UpdatedAt = src.UpdatedAt
}

// SaveVendor will create a vendor if the ID field is not initialized
// if ID field is initialized, it will update the conresponding vendor.
func (r *Registry) SaveVendor(vendor *models.Vendor, reply *models.Vendor) error {
Expand Down Expand Up @@ -63,12 +107,7 @@ func (r *Registry) SaveVendor(vendor *models.Vendor, reply *models.Vendor) error
cache.Delete(cacheKey)
}

reply.ID = vendor.ID
reply.VendorName = vendor.VendorName
reply.VendorDescription = vendor.VendorDescription
reply.VendorKey = vendor.VendorKey
reply.CreatedAt = vendor.CreatedAt
reply.UpdatedAt = vendor.UpdatedAt
setVendor(reply, vendor)

return nil
}
Expand Down Expand Up @@ -107,13 +146,7 @@ func (r *Registry) SaveProduct(product *models.Product, reply *models.Product) e
cache.Delete(cacheKey)
}

reply.ID = product.ID
reply.ProductName = product.ProductName
reply.ProductDescription = product.ProductDescription
reply.ProductKey = product.ProductKey
reply.ProductConfig = product.ProductConfig
reply.CreatedAt = product.CreatedAt
reply.UpdatedAt = product.UpdatedAt
setProduct(reply, product)

return nil
}
Expand Down Expand Up @@ -151,15 +184,7 @@ func (r *Registry) SaveApplication(app *models.Application, reply *models.Applic
cache.Delete(cacheKey)
}

reply.ID = app.ID
reply.AppName = app.AppName
reply.AppDescription = app.AppDescription
reply.AppKey = app.AppKey
reply.ReportUrl = app.ReportUrl
reply.AppToken = app.AppToken
reply.AppDomain = app.AppDomain
reply.CreatedAt = app.CreatedAt
reply.UpdatedAt = app.UpdatedAt
setApplication(reply, app)

return nil
}
Expand All @@ -180,7 +205,8 @@ func (r *Registry) ValidateApplication(key string, reply *models.Application) er
cache := getCache()
cacheKey := fmt.Sprintf("Application:%v", id)
if cacheValue, ok := cache.Get(cacheKey); ok {
reply = cacheValue.(*models.Application)
app := cacheValue.(*models.Application)
setApplication(reply, app)
} else {
err = db.First(reply, id).Error
if err != nil {
Expand Down Expand Up @@ -208,7 +234,8 @@ func (r *Registry) FindVendor(id int32, reply *models.Vendor) error {
cache := getCache()
cacheKey := fmt.Sprintf("Vendor:%v", id)
if cacheValue, ok := cache.Get(cacheKey); ok {
reply = cacheValue.(*models.Vendor)
vendor := cacheValue.(*models.Vendor)
setVendor(reply, vendor)
} else {
err = db.First(reply, id).Error
if err != nil {
Expand Down Expand Up @@ -262,7 +289,8 @@ func (r *Registry) FindProduct(id int32, reply *models.Product) error {
cache := getCache()
cacheKey := fmt.Sprintf("Product:%v", id)
if cacheValue, ok := cache.Get(cacheKey); ok {
reply = cacheValue.(*models.Product)
product := cacheValue.(*models.Product)
setProduct(reply, product)
} else {
err = db.First(reply, id).Error
if err != nil {
Expand All @@ -286,7 +314,8 @@ func (r *Registry) FindApplication(id int32, reply *models.Application) error {
cache := getCache()
cacheKey := fmt.Sprintf("Application:%v", id)
if cacheValue, ok := cache.Get(cacheKey); ok {
reply = cacheValue.(*models.Application)
app := cacheValue.(*models.Application)
setApplication(reply, app)
} else {
err = db.First(reply, id).Error
if err != nil {
Expand Down Expand Up @@ -316,7 +345,8 @@ func (r *Registry) ValidateProduct(key string, reply *models.Product) error {
cache := getCache()
cacheKey := fmt.Sprintf("Product:%v", id)
if cacheValue, ok := cache.Get(cacheKey); ok {
reply = cacheValue.(*models.Product)
product := cacheValue.(*models.Product)
setProduct(reply, product)
} else {
err = db.First(reply, id).Error
if err != nil {
Expand Down Expand Up @@ -384,7 +414,7 @@ func (r *Registry) RegisterDevice(args *rpcs.ArgsDeviceRegister, reply *models.D
if _, ok := cache.Get(cacheKey); ok {
cache.Delete(cacheKey)
}

// device has aleady been saved. just update version info.
reply.DeviceVersion = args.DeviceVersion
err = db.Save(reply).Error
Expand All @@ -407,7 +437,8 @@ func (r *Registry) FindDeviceByIdentifier(identifier string, reply *models.Devic
cache := getCache()
cacheKey := fmt.Sprintf("Device:%v", identifier)
if cacheValue, ok := cache.Get(identifier); ok {
reply = cacheValue.(*models.Device)
device := cacheValue.(*models.Device)
setDevice(reply, device)
} else {
err = db.Where(&models.Device{
DeviceIdentifier: identifier,
Expand Down Expand Up @@ -470,7 +501,7 @@ func (r *Registry) UpdateDeviceInfo(args *rpcs.ArgsDeviceUpdate, reply *models.D
if err != nil {
return err
}

//delete cache
cache := getCache()
cacheKey := fmt.Sprintf("Device:%v", args.DeviceIdentifier)
Expand Down

0 comments on commit ec653d2

Please sign in to comment.