|
@@ -376,37 +376,61 @@ func (r *Registry) FindDeviceByIdentifier(identifier string, reply *models.Devic
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-// FindDeviceById will find the device with given id
|
|
|
-func (r *Registry) FindDeviceByRecordId(args *rpcs.ArgsDeviceAuth, reply *models.Device) error {
|
|
|
+func (r *Registry) FindDeviceById(args uint64, reply *models.Device) error {
|
|
|
db, err := getDB()
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
- d := &models.Device{}
|
|
|
- d.RecordId = args.DeviceID
|
|
|
- err = db.Where(d).First(reply).Error
|
|
|
-
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
+ cache := getCache()
|
|
|
+ key := fmt.Sprintf("Device:%v", args)
|
|
|
+ if v, ok := cache.Get(key);ok {
|
|
|
+ device := v.(*models.Device)
|
|
|
+ setDevice(reply, device)
|
|
|
+ } else {
|
|
|
+ d := &models.Device{}
|
|
|
+ d.ID = uint(args)
|
|
|
+ err = db.Where(d).First(reply).Error
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ var storage models.Device
|
|
|
+ storage = *reply
|
|
|
+ cache.Set(key, &storage)
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-func (r *Registry) FindDeviceById(args uint64, reply *models.Device) error {
|
|
|
+// FindDeviceById will find the device with given id
|
|
|
+func (r *Registry) FindDeviceByRecordId(args *rpcs.ArgsDeviceAuth, reply *models.Device) error {
|
|
|
db, err := getDB()
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
- d := &models.Device{}
|
|
|
- d.ID = uint(args)
|
|
|
- err = db.Where(d).First(reply).Error
|
|
|
-
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
+ cache := getCache()
|
|
|
+ key := fmt.Sprintf("Device:%v", args.DeviceID)
|
|
|
+ if v, ok := cache.Get(key);ok {
|
|
|
+ device := v.(*models.Device)
|
|
|
+ setDevice(reply, device)
|
|
|
+ } else {
|
|
|
+ d := &models.Device{}
|
|
|
+ d.RecordId = args.DeviceID
|
|
|
+ err = db.Where(d).First(reply).Error
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ var storage models.Device
|
|
|
+ storage = *reply
|
|
|
+ cache.Set(key, &storage)
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+
|
|
|
// ValidateDevice will validate a device key and return the model if success.
|
|
|
func (r *Registry) ValidateDevice(key string, device *models.Device) error {
|
|
|
id, err := r.keygen.DecodeIDFromRandomKey(key)
|