LoginSignup
0
0

More than 1 year has passed since last update.

Golang で Redis のデータを更新 (Update)

Last updated at Posted at 2018-05-21

Golang で Redis のデータを更新します。
次のプログラムで作成したデータを更新します。
Golang で Redis のデータを作成 (Create)

プログラム

redis_update.go
// ----------------------------------------------------------------
//
//	redis_update.go
//
//					May/21/2018
//
// ----------------------------------------------------------------
package main
 
import (
	"fmt"
	"net"
	"os"
	"strconv"
	"strings"
	"encoding/json"
	"time"
)

// ----------------------------------------------------------------
func main () {

	fmt.Println ("*** 開始 ***")

	key_in := os.Args[1]
	population_in,_ := strconv.Atoi (os.Args[2])

	fmt.Printf ("id_in = %s\t" , key_in)
	fmt.Printf ("population_in = %d\n" , population_in)

	hostname := "localhost"
	port := "6379"

	conn, err := net.Dial ("tcp",hostname + ":" + port)
	if err != nil {
		fmt.Println(err)
		return
		}

	json_str := mcached_socket_read_proc (conn,key_in)

	if 0 < len (json_str) {
		json_str_new := json_update_proc (json_str,population_in)
		fmt.Println (json_str_new)
		redis_socket_write_proc (conn,key_in,json_str_new)
		}

	conn.Close ()

	fmt.Println ("*** 終了 ***")
}

// ----------------------------------------------------------------
func socket_read_proc (conn net.Conn,key_in string) string {
	str_received := ""
	_, err := conn.Write([]byte("get " + key_in + "\r\n"))
	if err != nil {
		fmt.Println(err)
		return str_received
		}

	buf := make([]byte, 1024)
	nn, err := conn.Read(buf[:])
	if err != nil {
		fmt.Println(err)
		return str_received
		}

	str_received = string(buf[0:nn])

	return	str_received
}

// ----------------------------------------------------------------
func mcached_socket_read_proc (conn net.Conn,key_in string) string {
	json_str := ""
	str_received := socket_read_proc (conn,key_in)

	lines := strings.Split(str_received,"\n")

	if (! strings.Contains(lines[0],"END")) {
		json_str = lines[1]
		}

	return	json_str
}

// ----------------------------------------------------------------
func redis_socket_write_proc (conn net.Conn,key_in string,json_str string) {
	fmt.Println (key_in)
	fmt.Println (json_str)

	comm_aa := "set " + key_in + " '" + json_str + "'\r\n"
	conn.Write([]byte(comm_aa))


	buf := make ([]byte,1024)
	conn.Read (buf[:])

	fmt.Println (string(buf[0:10]))
}

// ----------------------------------------------------------------
func json_update_proc (json_str string,population_in int) string {

	var unit_aa map[string]interface{}
	json.Unmarshal ([]byte(json_str), &unit_aa)
	fmt.Printf ("%s\t",unit_aa["name"])
	fmt.Printf ("%f\t",unit_aa["population"])
	fmt.Printf ("%s\n",unit_aa["date_mod"])

	unit_aa["population"] = population_in
	unit_aa["date_mod"] = get_current_date_proc ()

	output, _ := json.Marshal(unit_aa)

	json_str = string(output)

	return	json_str
}

// ----------------------------------------------------------------
func get_current_date_proc () string {
	now := time.Now ()
	fmt.Printf ("%s\t" ,now)
	fmt.Printf ("%d-%d-%d\n" ,now.Year (),now.Month(),now.Day())
	today := strconv.Itoa (now.Year()) + "-" +
		fmt.Sprintf ("%d",now.Month()) + "-" +
		strconv.Itoa (now.Day())

	return	today
}

// ----------------------------------------------------------------

実行コマンドの例

go run redis_update.go t1854 39412800

確認したバージョン

$ go version
go version go1.19.2 linux/amd64
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0