Skip to content

morkid/gocache-elasticsearch

Repository files navigation

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.

About

simple elasticsearch cache adapter for golang

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages