Redis caching layer for Cloudflare KV in Golang

Why redis-cloudflare-kv?

Cloudflare KV is a highly distributed, eventually consistent, key-value store that spans Cloudflare’s global edge. It allows you to store billions of key-value pairs and read them with some average latency (2 to 3 s…


This content originally appeared on DEV Community and was authored by Dinesh

Why redis-cloudflare-kv?

  • Cloudflare KV is a highly distributed, eventually consistent, key-value store that spans Cloudflare's global edge. It allows you to store billions of key-value pairs and read them with some average latency (2 to 3 seconds) anywhere in the world.
  • Cloudflare KV Read operation is very slow when compared to Redis which offers memory efficiency, fast operating speed, and high availability. So it's better to use Redis as a caching layer for Cloudflare KV to provide fast, economic scaling for your application.
  • redis-cloudflare-kv checks if there is a cached result for a query in Redis.
  • If not found in the cache, it will retrieve data from Cloudflare KV and on the successful result, cache it in Redis for future queries.

Application Architecture

Installation

You need a working Go environment.

go get github.com/dineshsonachalam/redis-cloudflare-kv

Quickstart

package main

import (
    "fmt"
    "log"
    "os"

    rediscloudflarekv "github.com/dineshsonachalam/redis-cloudflare-kv"
)

func main() {
    // Construct a new KV Client object
    kvClient := rediscloudflarekv.New(
        // REDIS_URL -> TCP Connection:  redis://<user>:<password>@<host>:<port>/<db_number>
        //              UNIX Connection: unix://<user>:<password>@</path/to/redis.sock>?db=<db_number>
        os.Getenv("REDIS_URL"),
        os.Getenv("CLOUDFLARE_ACCESS_KEY"),
        os.Getenv("CLOUDFLARE_EMAIL_ADDRESS"),
        os.Getenv("CLOUDFLARE_ACCOUNT_ID"),
    )
    namespaceID := os.Getenv("TEST_NAMESPACE_ID")
    key1 := "opensource.facebook.react"
    value1 := "A declarative, efficient, and flexible JavaScript library for building user interfaces."
    key2 := "opensource.facebook.react-native"
    value2 := "A framework for building native apps with React."
    key3 := "opensource.facebook.flow"
    value3 := "Adds static typing to JavaScript to improve developer productivity and code quality."
    key4 := "opensource.facebook.docusaurus"
    value4 := "Easy to maintain open source documentation websites."

    // Write writes a value identified by a key in Redis and Cloudflare KV
    status, err := kvClient.Write(key1, []byte(value1), namespaceID)
    if !status && err != nil {
        log.Fatal(err)
    }
    fmt.Printf("Write operation is successful for key: %v\n", key1)

    // Read returns the value associated with the given key
    // If the key is not available in the Redis server,
    // it searches for the key in Cloudflare KV and if the key is available, it writes the key/value in the Redis.
    // Then it returns the value associated with the given key.
    value, err := kvClient.Read(key1, namespaceID)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("Read operation is successful. Key: %v, Value: %v\n", key1, string(value))

    // Delete deletes a key/value in Redis and Cloudflare KV
    status, err = kvClient.Delete(key1, namespaceID)
    if !status && err != nil {
        log.Fatal(err)
    }
    fmt.Printf("Delete operation is successful for key: %v\n", key1)

    kvClient.Write(key2, []byte(value2), namespaceID)
    kvClient.Write(key3, []byte(value3), namespaceID)
    kvClient.Write(key4, []byte(value4), namespaceID)

    // ListKeysByPrefix returns keys that matches the prefix
    // If there are no key's that matches the prefix in the Redis
    // We search for the prefix pattern in Cloudflare KV, if there are keys
    // that matches the prefix, we return the keys.
    keys, err := kvClient.ListKeysByPrefix("opensource.facebook", namespaceID)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("ListKeysByPrefix operation is successful. Keys: %v\n", keys)
}

// Output:
// dineshsonachalam@macbook examples % go run main.go
// Write operation is successful for key: opensource.facebook.react
// Read operation is successful. Key: opensource.facebook.react, Value: A declarative, efficient, and flexible JavaScript library for building user interfaces.
// Delete operation is successful for key: opensource.facebook.react
// ListKeysByPrefix operation is successful. Keys: [opensource.facebook.docusaurus opensource.facebook.react-native opensource.facebook.flow]

License

MIT © dineshsonachalam


This content originally appeared on DEV Community and was authored by Dinesh


Print Share Comment Cite Upload Translate Updates
APA

Dinesh | Sciencx (2021-08-18T16:29:38+00:00) Redis caching layer for Cloudflare KV in Golang. Retrieved from https://www.scien.cx/2021/08/18/redis-caching-layer-for-cloudflare-kv-in-golang/

MLA
" » Redis caching layer for Cloudflare KV in Golang." Dinesh | Sciencx - Wednesday August 18, 2021, https://www.scien.cx/2021/08/18/redis-caching-layer-for-cloudflare-kv-in-golang/
HARVARD
Dinesh | Sciencx Wednesday August 18, 2021 » Redis caching layer for Cloudflare KV in Golang., viewed ,<https://www.scien.cx/2021/08/18/redis-caching-layer-for-cloudflare-kv-in-golang/>
VANCOUVER
Dinesh | Sciencx - » Redis caching layer for Cloudflare KV in Golang. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/08/18/redis-caching-layer-for-cloudflare-kv-in-golang/
CHICAGO
" » Redis caching layer for Cloudflare KV in Golang." Dinesh | Sciencx - Accessed . https://www.scien.cx/2021/08/18/redis-caching-layer-for-cloudflare-kv-in-golang/
IEEE
" » Redis caching layer for Cloudflare KV in Golang." Dinesh | Sciencx [Online]. Available: https://www.scien.cx/2021/08/18/redis-caching-layer-for-cloudflare-kv-in-golang/. [Accessed: ]
rf:citation
» Redis caching layer for Cloudflare KV in Golang | Dinesh | Sciencx | https://www.scien.cx/2021/08/18/redis-caching-layer-for-cloudflare-kv-in-golang/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.