Skip to content

Latest commit

 

History

History
82 lines (66 loc) · 2.86 KB

README.md

File metadata and controls

82 lines (66 loc) · 2.86 KB

cache elasticsearch adapter

Go Reference Go Build Status Go Report Card GitHub release (latest SemVer)

This library is created by implementing gocache and require elasticsearch v7.

Installation

go get -d github.com/morkid/gocache-elasticsearch/v7

Available versions:

Example usage

package main

import (
    "time"
    "fmt"
    cache "github.com/morkid/gocache-elasticsearch/v7"
    "github.com/elastic/go-elasticsearch/v7"
)

func latency() {
    // network latency simulation
    // just for testing
    time.Sleep(1 * time.Second)
}

func main() {
    config := elasticsearch.Config{
        Addresses: []string{
            "http://localhost:9200",
        },
    }
    es, err := elasticsearch.NewClient(config)
    if nil != err {
        panic(err)
    }

    adapterConfig := cache.ElasticCacheConfig{
        Client:    es,
        Index:     "example",
        ExpiresIn: 10 * time.Second,
    }

    adapter := *cache.NewElasticCache(config)
    adapter.Set("foo", "bar")

    if adapter.IsValid("foo") {
        value, err := adapter.Get("foo")
        if nil != err {
            fmt.Println(err)
        } else if value != "bar" {
            fmt.Println("value not equals to bar")
        } else {
            fmt.Println(value)
        }
        adapter.Clear("foo")

        latency()
        if adapter.IsValid("foo") {
            fmt.Println("Failed to remove key foo")
        }
    }
}

License

Published under the MIT License.