コード例 #1
0
func CreateRedisRecord(network, location string) RedisRecorder {
	conn := redis.Redis{
		Network:  network,
		Location: location,
	}
	if !conn.Connect() {
		fmt.Println("Redis connection fail!\nNetwork: ", network, " Location: ", location)
		os.Exit(1)
	}
	fmt.Println("Redis Recorder Connected")
	return RedisRecorder{conn: &conn}
}
コード例 #2
0
ファイル: context.go プロジェクト: chenrufeng/redis-persist
// LEVELDB恢复到REDIS的具体实现
func restore(ud interface{}, key string, cli *redis.Redis) (err error) {
	context := ud.(*Context)
	db := context.db
	var chunk []byte
	if chunk, err = db.Get([]byte(key)); err != nil {
		Error("query key %s failed:%v", key, err)
		return
	}

	if chunk == nil {
		err = errors.New("key doesn't exist on leveldb")
		return
	}

	var leveldb_data map[string]string
	err = json.Unmarshal(chunk, &leveldb_data)
	if err != nil {
		Error("chunk is not json:%s", key)
		return
	}
	redis_data := make(map[string]string)
	err = cli.Hgetall(key, redis_data)
	if err != nil {
		Error("hgetall key %s failed:%v", key, err)
		return
	}

	if redis_data["version"] >= leveldb_data["version"] && len(redis_data) > 0 {
		Info("redis_data[version]:%s >= leveldb_data[version]:%s", redis_data["version"], leveldb_data["version"])
		return
	}
	leveldb_array := make([]interface{}, len(leveldb_data)*2+1)
	leveldb_array[0] = key
	i := 1
	for k, v := range leveldb_data {
		leveldb_array[i] = k
		leveldb_array[i+1] = v
		i = i + 2
	}
	_, err = cli.Exec("hmset", leveldb_array...)
	if err != nil {
		Error("hmset key %s failed:%v", key, err)
		return
	}
	return
}