Git Product home page Git Product logo

Comments (3)

mxplusb avatar mxplusb commented on May 15, 2024 1

Yeah, that's more or less what I was thinking. I can look at a PR in the next week or some, once I have some cycles for it. I'm more or less going to do the same thing, just add a few niceties for CLI interactions.

from bigcache.

janisz avatar janisz commented on May 15, 2024

Quick and dirty implementation:

package main

import (
	"io/ioutil"
	"log"
	"net/http"
	"time"

	"github.com/allegro/bigcache"
)

func main() {

	config := bigcache.Config{
		Shards:             1024,
		LifeWindow:         10 * time.Minute,
		MaxEntriesInWindow: 1000 * 10 * 60,
		MaxEntrySize:       500,
		Verbose:            true,
		HardMaxCacheSize:   8192,
		OnRemove:           nil,
	}

	cache, initErr := bigcache.NewBigCache(config)
	if initErr != nil {
		log.Fatal(initErr)
	}

	http.HandleFunc("/api/v1/", func(w http.ResponseWriter, r *http.Request) {
		key := r.URL.Path[len("/api/v1/"):]
		switch r.Method {
		case http.MethodGet:
			entry, err := cache.Get(key)
			if err != nil {
				w.Write([]byte(err.Error()))
				w.WriteHeader(http.StatusInternalServerError)
				return
			}
			w.Write(entry)

		case http.MethodPut:
			entry, err := ioutil.ReadAll(r.Body)
			if err != nil {
				w.Write([]byte(err.Error()))
				w.WriteHeader(http.StatusInternalServerError)
				return
			}

			if err := cache.Set(key, []byte(entry)); err != nil {
				w.Write([]byte(err.Error()))
				w.WriteHeader(http.StatusInternalServerError)
				return
			}
			w.Write(entry)

		default:
			w.Write([]byte("Invalide Method"))
			w.WriteHeader(http.StatusBadRequest)
		}
	})
	log.Fatal("ListenAndServe: ", http.ListenAndServe(":9090", nil))
}

➜ ~ curl -X PUT -d value localhost:9090/api/v1/key
value%
➜ ~ curl localhost:9090/api/v1/key
value%

from bigcache.

janisz avatar janisz commented on May 15, 2024

Cool, I'm looking forward to merge your PR

from bigcache.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.