當前位置: 首頁>>代碼示例>>Golang>>正文


Golang redis.Conn類代碼示例

本文整理匯總了Golang中github.com/ninjasphere/redigo/redis.Conn的典型用法代碼示例。如果您正苦於以下問題:Golang Conn類的具體用法?Golang Conn怎麽用?Golang Conn使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了Conn類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: 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
}
開發者ID:kpernyer,項目名稱:sphere-go-homecloud,代碼行數:32,代碼來源:baseModel.go

示例2: 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
}
開發者ID:kpernyer,項目名稱:sphere-go-homecloud,代碼行數:7,代碼來源:baseModel.go

示例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)
}
開發者ID:kpernyer,項目名稱:sphere-go-homecloud,代碼行數:59,代碼來源:baseModel.go

示例4: deleteRelationshipWithDevice

func (m *ThingModel) deleteRelationshipWithDevice(deviceID string, conn redis.Conn) error {

	defer syncFS()

	_, err := conn.Do("HDEL", "device-thing", deviceID)

	return err
}
開發者ID:ninjasphere,項目名稱:sphere-go-homecloud,代碼行數:8,代碼來源:ThingModel.go

示例5: 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
}
開發者ID:kpernyer,項目名稱:sphere-go-homecloud,代碼行數:8,代碼來源:ModuleModel.go

示例6: 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
}
開發者ID:kpernyer,項目名稱:sphere-go-homecloud,代碼行數:8,代碼來源:ModuleModel.go

示例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
}
開發者ID:ninjasphere,項目名稱:sphere-go-homecloud,代碼行數:57,代碼來源:ThingModel.go

示例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
}
開發者ID:kpernyer,項目名稱:sphere-go-homecloud,代碼行數:10,代碼來源:baseModel.go

示例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
}
開發者ID:kpernyer,項目名稱:sphere-go-homecloud,代碼行數:11,代碼來源:baseModel.go

示例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
}
開發者ID:kpernyer,項目名稱:sphere-go-homecloud,代碼行數:13,代碼來源:ModuleModel.go

示例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

}
開發者ID:mash0304,項目名稱:sphere-go-homecloud,代碼行數:60,代碼來源:RoomModel.go

示例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
}
開發者ID:ninjasphere,項目名稱:sphere-go-homecloud,代碼行數:16,代碼來源:ThingModel.go

示例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
}
開發者ID:kpernyer,項目名稱:sphere-go-homecloud,代碼行數:17,代碼來源:ChannelModel.go

示例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
}
開發者ID:kpernyer,項目名稱:sphere-go-homecloud,代碼行數:18,代碼來源:ChannelModel.go

示例15: GetDeviceIDForThing

func (m *ThingModel) GetDeviceIDForThing(thingID string, conn redis.Conn) (*string, error) {

	allRels, err := redis.Strings(redis.Values(conn.Do("HGETALL", "device-thing")))

	if err != nil {
		return nil, err
	}

	if len(allRels) == 0 {
		return nil, RecordNotFound
	}

	for i := 0; i < len(allRels); i += 2 {
		if allRels[i+1] == thingID {
			return &allRels[i], nil
		}
	}

	return nil, RecordNotFound
}
開發者ID:ninjasphere,項目名稱:sphere-go-homecloud,代碼行數:20,代碼來源:ThingModel.go


注:本文中的github.com/ninjasphere/redigo/redis.Conn類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。