本文整理匯總了Golang中github.com/ninjasphere/redigo/redis.Conn.Do方法的典型用法代碼示例。如果您正苦於以下問題:Golang Conn.Do方法的具體用法?Golang Conn.Do怎麽用?Golang Conn.Do使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/ninjasphere/redigo/redis.Conn
的用法示例。
在下文中一共展示了Conn.Do方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: fetchIds
func (m *baseModel) fetchIds(conn redis.Conn) ([]string, error) {
ids, err := redis.Strings(conn.Do("SMEMBERS", m.idType+"s"))
m.log.Debugf("Found %d %s id(s)", m.idType, len(ids))
return ids, err
}
示例2: fetch
func (m *baseModel) fetch(id string, obj interface{}, syncing bool, conn redis.Conn) error {
m.log.Debugf("Fetching %s %s", m.idType, id)
/* if _, ok := m.entityLocks[id]; !ok {
m.entityLocks[id] = &sync.Mutex{}
}
m.entityLocks[id].Lock()
m.log.Debugf("Locked %s %s", m.idType, id)
defer m.entityLocks[id].Unlock()*/
item, err := redis.Values(conn.Do("HGETALL", m.idType+":"+id))
if err != nil {
return err
}
if len(item) == 0 {
return RecordNotFound
}
if err := redis.ScanStruct(item, obj); err != nil {
return err
}
if m.onFetch != nil {
err = m.onFetch(obj, syncing, conn)
}
return err
}
示例3: save
func (m *baseModel) save(id string, obj interface{}, conn redis.Conn) (bool, error) {
m.log.Debugf("Saving %s %s", m.idType, id)
defer syncFS()
existing := reflect.New(m.objType)
err := m.fetch(id, existing.Interface(), false, conn)
if err != nil && err != RecordNotFound {
return false, err
}
brandNew := err == RecordNotFound
if err == nil {
if m.isUnchanged(existing.Interface(), obj) {
m.log.Debugf("%s %s was unchanged.", m.idType, id)
return false, nil
// XXX: Should this be return RecordUnchanged?
}
}
args := redis.Args{}
args = args.Add(m.idType + ":" + id)
args = args.AddFlat(obj)
_, err = conn.Do("HMSET", args...)
if err != nil {
return false, fmt.Errorf("Failed to save object %s error:%s", id, err)
}
_, err = conn.Do("SADD", m.idType+"s", id)
if err != nil {
return false, fmt.Errorf("Failed to add object %s to list of ids error:%s", id, err)
}
if m.afterSave != nil {
err = m.afterSave(obj, conn)
if err != nil {
return true, fmt.Errorf("Error during afterSave callback: %s", err)
}
}
if m.sendEvent != nil {
if brandNew {
m.sendEvent("created", id)
} else {
m.sendEvent("updated", id)
}
}
return true, m.markUpdated(id, time.Now(), conn)
}
示例4: SetConfig
func (m *ModuleModel) SetConfig(moduleID string, config string, conn redis.Conn) error {
m.syncing.Wait()
//defer m.sync()
defer syncFS()
_, err := conn.Do("HSET", "module:"+moduleID, "config", config)
return err
}
示例5: deleteRelationshipWithDevice
func (m *ThingModel) deleteRelationshipWithDevice(deviceID string, conn redis.Conn) error {
defer syncFS()
_, err := conn.Do("HDEL", "device-thing", deviceID)
return err
}
示例6: DeleteConfig
func (m *ModuleModel) DeleteConfig(moduleID string, conn redis.Conn) error {
m.syncing.Wait()
//defer m.sync()
defer syncFS()
_, err := conn.Do("HDEL", "module:"+moduleID, "config")
return err
}
示例7: afterSave
func (m *ThingModel) afterSave(thing *model.Thing, conn redis.Conn) error {
defer syncFS()
m.log.Debugf("afterSave - thing received id:%s with device:%s", thing.ID, thing.DeviceID)
existingDeviceID, err := m.GetDeviceIDForThing(thing.ID, conn)
if err != nil && err != RecordNotFound {
return fmt.Errorf("Failed to get existing device relationship error:%s", err)
}
if existingDeviceID != thing.DeviceID {
if thing.DeviceID == nil {
// Theres no device, so remove the existing relationship if it's there
deviceID, err := m.GetDeviceIDForThing(thing.ID, conn)
if err == nil {
err = m.deleteRelationshipWithDevice(*deviceID, conn)
}
if err != nil {
return fmt.Errorf("Failed to remove existing device relationship error:%s", err)
}
} else {
// See if another thing is already attached to the device
existingThingID, err := m.GetThingIDForDevice(thing.ID, conn)
if existingThingID != nil {
// Remove the existing relationship
err = m.deleteRelationshipWithDevice(*thing.DeviceID, conn)
if err != nil {
return fmt.Errorf("Failed to remove existing relationship to device %s. Currently attached to thing %s, we wanted it to be attached to %s. Error:%s", *thing.DeviceID, *existingThingID, thing.ID, err)
}
}
_, err = conn.Do("HSET", "device-thing", *thing.DeviceID, thing.ID)
if err != nil {
return fmt.Errorf("Failed to update device relationship. error: %s", err)
}
}
if err == nil {
err = m.markUpdated(thing.ID, time.Now(), conn)
if err != nil {
return fmt.Errorf("Failed to mark thing updated. error: %s", err)
}
}
}
return nil
}
示例8: markUpdated
func (m *baseModel) markUpdated(id string, t time.Time, conn redis.Conn) error {
defer syncFS()
ts, err := t.MarshalText()
if err != nil {
return err
}
_, err = conn.Do("HSET", m.idType+"s:updated", id, ts)
return err
}
示例9: getLastUpdated
func (m *baseModel) getLastUpdated(id string, conn redis.Conn) (*time.Time, error) {
timeString, err := redis.String(conn.Do("HGET", m.idType+"s:updated", id))
if err != nil || timeString == "" {
return nil, err
}
t := time.Time{}
err = t.UnmarshalText([]byte(timeString))
return &t, err
}
示例10: GetConfig
func (m *ModuleModel) GetConfig(moduleID string, conn redis.Conn) (*string, error) {
m.syncing.Wait()
exists, err := redis.Bool(conn.Do("HEXISTS", "module:"+moduleID, "config"))
if exists {
item, err := conn.Do("HGET", "module:"+moduleID, "config")
config, err := redis.String(item, err)
return &config, err
}
return nil, err
}
示例11: MoveThing
func (m *RoomModel) MoveThing(from *string, to *string, thing string, conn redis.Conn) error {
m.syncing.Wait()
//defer m.sync()
var err error
if to != nil {
_, err := m.Fetch(*to, conn) // Ensure the room we are putting it into actually exists
if err != nil {
return err
}
}
// Don't do a damn thing.
if from == to {
return nil
}
defer syncFS()
if to != nil && from == nil {
// we need to add it
dest := "room:" + *to + ":things"
m.log.Debugf("Adding thing %s to room %s", thing, *to)
_, err = conn.Do("SADD", dest, thing)
} else if from != nil && to == nil {
// we need to remove it
src := "room:" + *from + "s:things"
m.log.Debugf("Removing thing %s from room %s", thing, *from)
_, err = conn.Do("SREM", src, thing)
} else {
// need to move it
src := "room:" + *from + "s:things"
dest := "room:" + *to + ":things"
m.log.Debugf("Moving thing %s from room %s to room %s", thing, *from, *to)
result, err2 := conn.Do("SMOVE", src, dest, thing)
if err2 != nil {
return err2
}
if result == 0 {
// Wasn"t in src, we should add it to dest
_, err = conn.Do("SADD", dest, thing)
}
}
return err
}
示例12: GetThingIDForDevice
func (m *ThingModel) GetThingIDForDevice(deviceID string, conn redis.Conn) (*string, error) {
item, err := conn.Do("HGET", "device-thing", deviceID)
if err != nil {
return nil, err
}
if item == nil {
return nil, RecordNotFound
}
thingID, err := redis.String(item, err)
return &thingID, err
}
示例13: Create
func (m *ChannelModel) Create(deviceID string, channel *model.Channel, conn redis.Conn) error {
m.syncing.Wait()
//defer m.sync()
defer syncFS()
channel.DeviceID = deviceID
if _, err := m.save(deviceID+"-"+channel.ID, channel, conn); err != nil {
return err
}
if _, err := conn.Do("SADD", "device:"+deviceID+":channels", channel.ID); err != nil {
return err
}
return nil
}
示例14: Delete
func (m *ChannelModel) Delete(deviceID string, channelID string, conn redis.Conn) error {
m.syncing.Wait()
//defer m.sync()
defer syncFS()
err := m.delete(deviceID+"-"+channelID, conn)
if err != nil {
return err
}
_, err = conn.Do("SREM", "device:"+deviceID+":channels", channelID)
// TODO: announce deletion via MQTT
// publish(Ninja.topics.room.goodbye.room(roomId)
// publish(Ninja.topics.location.calibration.delete, {zone: roomId})
return err
}
示例15: delete
func (m *baseModel) delete(id string, conn redis.Conn) error {
m.log.Debugf("Deleting %s %s", m.idType, id)
existing := reflect.New(m.objType).Interface()
existingErr := m.fetch(id, existing, false, conn)
if existingErr != nil && existingErr != RecordNotFound {
return fmt.Errorf("Failed fetching existing %s before delete. error:%s", m.idType, existingErr)
}
if existingErr == RecordNotFound {
lastUpdated, _ := m.getLastUpdated(id, conn)
if lastUpdated == nil {
// Hasn't ever existed...
return existingErr
}
// At this point we may have a RecordNotFound, but we may as well delete again anyway, just in case
m.log.Infof("%s id:%s appears to be already deleted, but we'll try again anyway.", m.idType, id)
}
defer syncFS()
conn.Send("MULTI")
conn.Send("SREM", m.idType+"s", id)
conn.Send("DEL", m.idType+":"+id)
_, err := conn.Do("EXEC")
if m.afterDelete != nil && existingErr == nil {
err = m.afterDelete(existing, conn)
if err != nil {
return fmt.Errorf("Failed on afterDelete: %s", err)
}
}
if m.sendEvent != nil {
m.sendEvent("deleted", id)
}
return m.markUpdated(id, time.Now(), conn)
}