👨‍💻
Dokumentasi Golang (PT Phincon)
  • 🚀Dokumentasi Bootcamp (PT Phincon)
  • Command di Golang
  • Static Type di Golang
  • Variable and Constant
    • Variable daI Constant
    • Deklarasi Variabel
    • Iota (Auto Increment)
    • Blank Identifier (Variable Underscore)
    • Access Modifier (Public or Private)
  • Tipe Data Primitif
    • Integer dan Unsigned Integer
    • Float dan Complex
    • String
    • Boolean
  • Tipe Data Aggregate
    • Array
    • Struct
  • Tipe Data Reference
    • Slice
    • Map
    • Function in Golang
      • Function
      • Function vs Method
      • Function vs Procedure
      • Private vs Public Function
      • Function Init dan Main
      • Function dengan Return Multiple Value
      • Variadic Function
      • Function sebagai Parameter
      • Anonymous Function
      • Input Function dari CLI
      • Exercise (Function)
  • Default Value setiap Tipe Data
  • Type Declaration di Golang
    • Type Declaration
    • Type Declaration Built-In di Golang
  • Kebab Case, Camel Case, Snake Case & Pascal Case di Golang
  • Konversi Tipe Data
  • If-Else dan Switch
    • If Else
    • Switch
    • Exercise
  • Looping
    • Looping For
    • Looping for range
    • Infinite Looping
    • Penerapan Looping pada Sorting
  • Operator di Golang
    • Arithmetic Operator
    • Assignment Operator
    • Relational Operator
    • Logic Operator
  • Interface
  • Interface Kosong atau Any
  • Nil
  • Pointer
    • Pass By Value dan Pass By Reference
    • Pointer (Pass By Reference)
    • Operator Address (& dan *)
    • Default Value Pointer
    • Tricky Case
    • Pointer pada Parameter di Function
    • Pointer pada Method
  • Package
    • Fmt
    • Rand
    • Os
    • Strings
      • To Lower
      • Contains
      • Split
      • Trim
      • Atoi
      • Itoa
      • EqualFold
    • Random Generator
    • Time
      • Get Current Time By Location
      • Time Sleep
      • Time Since
      • Timer & After
      • AfterFunc
      • Ticker & Tick
  • Go dan JSON
    • JSON vs XML
    • Unmarshal vs Marshal
    • Marshal (Go Object -> JSON)
    • Unmarshal (JSON -> Go Object)
    • Streaming Decoder & Encoder
    • Tag
    • JSON Go Return Byte
  • Go dan CSV
    • Insert Data ke File CSV
    • Insert 1.000.000 Data ke File CSV
  • Goroutine
    • Concurrency vs Parrarel
    • Go Routine Sederhana
    • Go Routine vs Synchronous
    • Wait Group
    • Defer
    • Channel
    • Buffered Channel
    • Select Channel
    • Deadlock - All goroutines are asleep
    • Race Condition
    • Mutex (Mutual Exclusion)
    • RW Mutex vs Mutex
    • Once
    • Pool
    • Atomic
    • Go Max Procs
    • Exit
    • Exercise 1 : Go Routine + Context + Channel
    • Exercise 2 : Worker (Go Routine + Channel + Context)
    • Exercise 3 : Random Worker (Go Routine + Channel + Context)
    • Exercise : Implementasi Goroutine dan Channel pada File CSV
  • Go Context
    • Pengenalan Context
    • Context Background & TODO
    • Context With Value
    • Context WithDeadline dan Context WithTimeout
    • Context WithCancel dan Context Done
  • Pengenalan HTTP
  • Go Native HTTP
    • HTTP Server
    • HTTP Server Multi Handler
    • HTTP Server dengan Serve Mux
    • HTTP Response Writer
    • HTTP Test
    • Routing
    • Konsep Middleware
    • Middleware
    • Get Query Parameter
    • Get Path Parameter
    • Request JSON
    • Request Form
    • Get dan Set Header
    • Get dan Set Cookie
    • Redirect
    • Serve File
    • Upload File
    • Download File
    • Hit Endpoint dengan Curl di Terminal
  • Go Gin Framework
    • HTTP Server
    • Router
    • Middleware
    • Get Query Parameter
    • Get Path Parameter
    • Request JSON
    • Request Form
    • Get dan Set Header
    • Get dan Set Cookie
    • Redirect
    • Serve File
    • Upload File
    • Download File
  • Golang dan Database
    • Instalasi MySQL dengan Docker Desktop
    • Instalasi PostgreSQL dengan Docker
    • Basic SQL
    • SQL Join
    • SQL Relation
    • Golang Database Driver
    • Golang dan SQL
  • Go Unit Test
    • Method di Package Testing
    • Package Assert & Require
    • Running Sub Test
    • Table Test
    • Generate Coverage Unit Testing dalam Bentuk HTML
  • Sonar Qube dan Sonar Scanner
  • Logging
  • Golang dan Redis
  • Golang dan RabbitMQ
    • Instalasi RabbitMQ dengan Docker
    • Instalasi Package RabbitMQ di Golang
    • Publisher dan Consumer
    • Publisher dan Multi Consumer
    • Setting RabbitMQ (Durable, Auto Delete dan Ack)
  • Git Command
  • Git Clone dengan SSH
  • Anotasi dan Package di Java Spring Boot
Powered by GitBook
On this page
  • Installasi Redis melalui Docker
  • Contoh penggunaan redis pada CMD
  • Package Redis di Golang
  • Contoh code golang dengan Redis

Golang dan Redis

https://redis.io/

PreviousLoggingNextGolang dan RabbitMQ

Last updated 1 year ago

Redis bisa digunakan untuk caching dan message broker. Cache digunakan untuk menyimpan informasi yang sifatnya sementara.

Installasi Redis melalui Docker

Untuk meng-install Redis di local komputer, pastikan sudah meng-install docker terlebih dahulu. Pertama jalankan docker, lalu install redis melalui CMD dengan command berikut.

docker pull redis

Untuk pengguna docker dekstop dapat install Redis dengan cara search Redis Image di bagian search docker, lalu klik pull. Begitu installasi selesai, image akan langsung muncul di Tab Image.

Contoh penggunaan redis pada CMD

Package Redis di Golang

Contoh code golang dengan Redis

Redis dapat digunakan untuk menyimpan value dari tipe data apapun di Golang. Cara yang mudah yaitu dengan mengkonversi value dengan suatu tipe data menjadi byte. Data byte tersebut dapat disimpan ke dalam Redis dengan tipe data Redis String. Cara konversi dari tipe data tertentu menjadi byte dapat menggunakan fungsi Marshal dari package "encoding/json".

main.go
package main

import (
	"context"
	"encoding/json"
	"fmt"
	"strconv"
	"time"

	"github.com/redis/go-redis/v9"
)

var ctx = context.Background()

type rdb struct {
	redisClient *redis.Client
}

// inisialisasi redis
func (r *rdb) InitiateRedis() {
	r.redisClient = redis.NewClient(&redis.Options{
		// localhost:6379
        	Addr:     "localhost:6379",
		ClientName: "",
		Username: "",
	        Password: "",
	        DB:       0,
		// Dial timeout for establishing new connections.
		DialTimeout: 5*time.Second,
    	})
	if r.redisClient != nil {
		fmt.Println("Redis initialization")
	}
}

func main() {
	var redisTest = rdb{}
	redisTest.InitiateRedis()

	// set string data to redis
    	redisTest.RedisSetKey("username", "member_01")
    	redisTest.RedisSetKey("username", "member_02")
	redisTest.RedisSetKey("password", "Test123")

	// set integer data to redis
	num := 12313214
	str := strconv.Itoa(num)
	redisTest.RedisSetKey("token", str)

	// set array data to redis
	someArray := [3]string{"member_01", "member_02", "member_03"}
	byteArray, _ := json.Marshal(someArray)
	redisTest.RedisSetKey("user_array", byteArray)

	// set slice data to redis
	someSlice := []string{"member_01", "member_02", "member_03"}
	byteSlice, _ := json.Marshal(someSlice)
	redisTest.RedisSetKey("user_slice", byteSlice)

	// set map data to redis
	user := map[string]interface{}{
		"username": "member_01", "password": "Tes123", "token": 1214113141,
	}
	byteMap, _ := json.Marshal(user)
	redisTest.RedisSetKey("user_map", byteMap)

	// set struct data to redis
	type userdata struct {
		Username string
		Password string
		Token    int
	}
	user2 := userdata{"member_01", "Test123", 43234312432413}
	byteStruct, _ := json.Marshal(user2)
	redisTest.RedisSetKey("user_struct", byteStruct)

	// get value by key
	keys := []string{"username", "password", "token", "user_array", "user_slice", "user_map", "user_struct"}
	for _, key := range keys {
		value, err := redisTest.RedisGetKey(key)
		if err != nil {
			fmt.Printf("Error get value of key %s \n", value)
			continue
		}
		fmt.Printf("Value of username key %s is %s\n", key, value)
	}

	// delete value by key
	redisTest.RedisDeleteKey("username")
	redisTest.RedisDeleteKey("password")
}

// redis set key and value
func (r rdb) RedisSetKey(key string, value interface{}) {
	err := r.redisClient.Set(ctx, key, value, 5*time.Minute).Err()
    if err != nil {
        panic(err)
    }
	fmt.Printf("Key %s with value %v is set\n", key, value)
}

// redis get value by key
func (r rdb) RedisGetKey(key string) (value string, err error) {
	value, err = r.redisClient.Get(ctx, key).Result()
	if err == redis.Nil {
		err = fmt.Errorf("key doesn't exist : %s, data type : %T", redis.Nil, redis.Nil)
		return
	} else if err != nil {
		return
    } else {
		return
	}
}

// redis delete value by key
func (r rdb) RedisDeleteKey(key string) {
	if err := r.redisClient.Get(ctx, key).Err(); err != nil {
		if err = r.redisClient.Del(ctx, key).Err(); err != nil {
			fmt.Printf("Error delete : %s\n", err.Error())
		}
		fmt.Printf("Success delete key %s\n", key)
	}
}
Redis initialization
Key username with value member_01 is set
Key username with value member_02 is set
Key password with value Test123 is set
Key token with value 12313214 is set
Key user_array with value [91 34 109 101 109 98 101 114 95 48 49 34 44 34 109 101 109 98 101 114 95 48 50 34 44 34 109 101 109 98 101 114 95 48 51 34 93] is set
Key user_slice with value [91 34 109 101 109 98 101 114 95 48 49 34 44 34 109 101 109 98 101 114 95 48 50 34 44 34 109 101 109 98 101 114 95 48 51 34 93] is set
Key user_map with value [123 34 112 97 115 115 119 111 114 100 34 58 34 84 101 115 49 50 51 34 44 34 116 111 107 101 110 34 58 49 50 49 52 49 49 51 49 52 49 44 34 117 115 101 114 110 97 109 101 34 58 34 109 101 109 98 101 114 95 48 49 34 125] is set
Key user_struct with value [123 34 85 115 101 114 110 97 109 101 34 58 34 109 101 109 98 101 114 95 48 49 34 44 34 80 97 115 115 119 111 114 100 34 58 34 84 101 115 116 49 
50 51 34 44 34 84 111 107 101 110 34 58 52 51 50 51 52 51 49 50 52 51 50 52 49 51 125] is set
Value of username key username is member_02
Value of username key password is Test123
Value of username key token is 12313214
Value of username key user_array is ["member_01","member_02","member_03"]
Value of username key user_slice is ["member_01","member_02","member_03"]
Value of username key user_map is {"password":"Tes123","token":1214113141,"username":"member_01"}
Value of username key user_struct is {"Username":"member_01","Password":"Test123","Token":43234312432413}

Reference:

Untuk penggunaan redis di golang dapat menggunakan package berikut .

https://github.com/redis/go-redis
Page cover image
LogoApa Itu Cache? Pengertian Cache, Manfaat, dan Jenis-JenisnyaHostinger Tutorial
Sumber gambar : dokcumentasi bootcamp PT Phincon